Keystone is CMS for Nodejs based projects, featuring customizable API, access control, authentication etc. Its is self hosted CMS.

Let's learn setup Access controls in a List.

Lists

Lists are types such as Users,Posts,Products , that will appear in Admin UI of Keystone. We can create Lists using Create List .

I assume that you had a Keystone project in hand and have a list of users and Admin Authentication enabled.

Adding Access control

Let's define two functions to check for login user and Admin.

 const isAdmin = ({ authentication: { item: user } }) => {    return !!user && !!user.isAdmin }  const isLoggedIn = ({ authentication: { item: user } }) => {    return !!user }

Limit access in a list

We can use the access key in a list to control the read,write,update,delete privileges. In our User example, we can set delete privilege to Admin only by appending the access

.... access: {     read: true,     update: isAdmin,     create: isLoggedIn,     delete: isAdmin,   }

Complete Code

Here is the complete code for the Nuxt - Keystone App

const { Keystone } = require('@keystonejs/keystone'); const { Password, Text, Relationship, Checkbox } = require('@keystonejs/fields'); const { GraphQLApp } = require('@keystonejs/app-graphql'); const { AdminUIApp } = require('@keystonejs/app-admin-ui'); const { NuxtApp } = require('@keystonejs/app-nuxt'); const { MongooseAdapter: Adapter } = require('@keystonejs/adapter-mongoose'); const adapterConfig = { mongoUri: 'mongodb://localhost/key-app' }; const { PasswordAuthStrategy } = require('@keystonejs/auth-password') const PROJECT_NAME = 'key-app';  const keystone = new Keystone({   adapter: new Adapter(adapterConfig), });   const isAdmin = ({ authentication: { item: user } }) => {    return !!user && !!user.isAdmin }  const isLoggedIn = ({ authentication: { item: user } }) => {    return !!user }  keystone.createList('Todo', {   schemaDoc: 'A list of things which need to be done',   fields: {     name: { type: Text, schemaDoc: 'This is the thing you need to do' },      assignedTo: {       type: Relationship,       ref: 'User',       many: false,       isRequired: true      }   },   access: {     read: true,     create: isLoggedIn,     delete: isAdmin   } });  keystone.createList('User', {   schemaDoc: 'A list of users',   fields: {     name: {       type: Text,       isUnique: true,       isRequired: true     },     isAdmin: {       type: Checkbox,       isRequired: true     },     password: {       type: Password,       isRequired: true     }   },   access: {     read: true,     update: isAdmin,     create: isAdmin,     delete: isAdmin,   } });  const authStrategy = keystone.createAuthStrategy(   {     type: PasswordAuthStrategy,     list: 'User',     config: {       identityField: 'name',       secretField: 'password'     }   } ) module.exports = {   keystone,   apps: [     new GraphQLApp(),     new AdminUIApp({       name: PROJECT_NAME, authStrategy,       isAccessAllowed: isLoggedIn     }),     new NuxtApp({       srcDir: 'src',       buildDir: 'dist',     }),   ], }; 

To run the CMS cd into the folder and run yarn dev

Following keystone post may help you learn more


This free site is ad-supported. Learn more