3

everyone!

I'm learning about KeystoneJS referring its manual. In this manual, I've stuck at an area of how to add access controls from the back-end to users which has labeled as admin. So, I'm referring the given examples in the KeystoneJS Manual

The given example in the following code is unclear how to figure out this with my code

const isAdmin = ({ session }: { session: Session }) => session?.data.isAdmin;

as

const isAdmin = ({ session }) => session?.data.isAdmin;

But when I try to add access controls to Post list, it won't work for the users that I've already created in the database as admin.

The problem is, when I start the server, it is loading normally the admin route. I don't see any error message in the console if I've made something wrong with the code

So, if someone could have any idea about how could I resolve this issue, please guide me. I'll provide all the code down below how I've configured my application.

This is my keystone.js file

// Requiring configuration modules
const { config } = require("@keystone-6/core");
const { withAuth, session} = require("./auth");

// Requiring models
const User = require("./models/User");
const Post = require("./models/Post");

// Initializing configuration and settings up
export default config(
    withAuth({
        db: {
            provider: 'mysql',
            url: 'mysql://root:Kist%40Ride%23007@localhost:3306/keystone',
        },
        lists: {
            User,
            Post
        },
        session,
        ui: {
            isAccessAllowed: (context) => !!context.session?.data,
        },
    })  
);

This is my auth.js file

const { createAuth } = require("@keystone-6/auth");
const { statelessSessions  } = require("@keystone-6/core/session");

const { withAuth } = createAuth({
    listKey: 'User',
    identityField: 'email',
    sessionData: 'isAdmin',
    secretField: 'password',
    initFirstItem: {
        fields: ['name', 'email', 'password']
    }
});

let sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
let sessionMaxAge = 60 * 3;

const session = statelessSessions({
    maxAge: sessionMaxAge,
    secret: sessionSecret
});

module.exports = {
    withAuth,
    session
};

This is my post.js file for Post list

const { list } = require("@keystone-6/core");
const { allowAll } = require("@keystone-6/core/access");
const { password, text, relationship, checkbox } = require("@keystone-6/core/fields");

module.exports = list({
    access: allowAll,
    fields: {
        name: text({
            validation: {
                isRequired: true
            }
        }),
        email: text({
            validation: {
                isRequired: true
            },
            isIndexed: 'unique'
        }),
        password: password({
            validation: {
                isRequired: true
            }
        }),
        isAdmin: checkbox(),
        posts: relationship({
            ref: 'Post',
            many: true
        }),
    }
});

This is my user.js file for User list

const { list } = require("@keystone-6/core");
const { allowAll } = require("@keystone-6/core/access");
const { document } = require("@keystone-6/fields-document");
const { text, timestamp, relationship, select, checkbox } = require("@keystone-6/core/fields");

const isAdmin = ({ session }) => session?.data.isAdmin;

module.exports = list({
    access: {
        operation: {
            create: isAdmin
        }
    },
    fields: {
        title: text(),
        isPublished: checkbox(),
        publishedAt: timestamp(),
        author: relationship({
            ref: 'User',
            many: false,
        }),
        content: document({
            formatting: true,
            links: true,
            dividers: true,
            layouts: [
                [1, 1],
                [1, 1, 1],
                [2, 1],
                [1, 2],
                [1, 2, 1],
            ],
        }),
        status: select({
            options: [{
                label: 'Published',
                value: 'published'
            }, {
                label: 'Draft',
                value: 'draft'
            }],
            defaultValue: 'draft',
            ui:{
                displayMode: 'segmented-control'
            }
        }),
    }
});

Note that, I'm using JavaScript instead of TypeScript for KeystoneJS

I hope someone could help me to resolve this problem.

Thank you!

Shadow
  • 33,525
  • 10
  • 51
  • 64
mastercordy
  • 161
  • 1
  • 2
  • 16
  • 1
    You say adding access control to posts "won't work" but I don't understand what problem is occurring. What were you expecting to happen? What happens instead? Note that access control alone doesn't disable parts of the Admin UI. See the list config, eg. `ui.hideCreate` (https://keystonejs.com/docs/config/lists#ui). – Molomby Jul 03 '23 at 02:17
  • @Molomby Thanks for your advice. I already figured it out. I wanted to allow only for admin to **CREATE UPDATE and DELETE** for the post list in keystone. – mastercordy Jul 03 '23 at 02:26
  • @Molomby Brother, if you don't mind please answer my question. I've 2 lists in my system 1 is user and the other one is post. So, if I logged into the system without admin rights, I wanted to add the user id automatically to the relationship field in KeystoneJS. how could I be able to do that? In the admin area it allows us to add the user manually when we creating the post. I don't want that to happen in this field. Thank you – mastercordy Jul 03 '23 at 02:49

0 Answers0