This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (122 loc) · 3.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const { Keystone } = require('@keystonejs/keystone');
const { PasswordAuthStrategy } = require('@keystonejs/auth-password');
const { Text, Checkbox, Password } = require('@keystonejs/fields');
const { GraphQLApp } = require('@keystonejs/app-graphql');
const { AdminUIApp } = require('@keystonejs/app-admin-ui');
const initialiseData = require('./initial-data');
const dotenv = require('dotenv').config()
const { createdAt, updatedAt } = require('@keystonejs/list-plugins');
const { MongooseAdapter: Adapter } = require('@keystonejs/adapter-mongoose');
const PROJECT_NAME = 'Lars Gerber';
const adapterConfig = { mongoUri: process.env.MONGO_URI };
const keystone = new Keystone({
adapter: new Adapter(adapterConfig),
onConnect: process.env.CREATE_TABLES !== 'true' && initialiseData,
cookie: {
secure: false, // Default to true in production
maxAge: 1000 * 60 * 60 * 24 * 30, // 30 days
sameSite: false,
},
cookieSecret: process.env.COOKIE_SECRET,
});
// Access control functions
const userIsAdmin = ({ authentication: { item: user } }) => Boolean(user && user.isAdmin);
const userOwnsItem = ({ authentication: { item: user } }) => {
if (!user) {
return false;
}
// Instead of a boolean, you can return a GraphQL query:
// https://www.keystonejs.com/api/access-control#graphqlwhere
return { id: user.id };
};
const userIsAdminOrOwner = auth => {
const isAdmin = access.userIsAdmin(auth);
const isOwner = access.userOwnsItem(auth);
return isAdmin ? isAdmin : isOwner;
};
const access = { userIsAdmin, userOwnsItem, userIsAdminOrOwner };
keystone.createList('User', {
fields: {
name: {
type: Text,
access: {
read: true,
auth: false,
}
},
email: {
type: Text,
isUnique: true,
access: {
read: access.userIsAdminOrOwner,
update: access.userIsAdminOrOwner,
create: access.userIsAdmin,
delete: access.userIsAdmin,
auth: true,
},
},
isAdmin: {
type: Checkbox,
access: {
read: access.userIsAdminOrOwner,
update: access.userIsAdmin,
create: access.userIsAdmin,
delete: access.userIsAdmin,
auth: true,
},
},
password: {
type: Password,
access: {
read: access.userIsAdminOrOwner,
update: access.userIsAdminOrOwner,
create: access.userIsAdmin,
delete: access.userIsAdmin,
auth: true,
},
},
},
});
const authStrategy = keystone.createAuthStrategy({
type: PasswordAuthStrategy,
list: 'User',
config: { protectIdentities: process.env.NODE_ENV === 'production' },
});
const PostSchema = require('./lists/Post')
keystone.createList('Post', {
fields: PostSchema.fields,
access: {
read: true,
create: access.userIsAdminOrOwner,
update: access.userIsAdminOrOwner,
delete: access.userIsAdminOrOwner,
},
labelField: 'title',
plugins: [
createdAt(),
updatedAt(),
],
})
// const ImageSchema = require('./lists/Image')
// keystone.createList('Image', {
// fields: ImageSchema.fields,
// access: {
// read: true,
// create: access.userIsAdminOrOwner,
// update: access.userIsAdminOrOwner,
// delete: access.userIsAdminOrOwner,
// },
// labelField: 'name',
// })
module.exports = {
keystone,
apps: [
new GraphQLApp(),
new AdminUIApp({
name: PROJECT_NAME,
enableDefaultRoute: true,
hooks: require.resolve('./admin/'),
authStrategy,
}),
],
};