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!