-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (58 loc) · 1.51 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
const dotenv = require('dotenv').config()
const { Keystone } = require('@keystonejs/keystone')
const { GraphQLApp } = require('@keystonejs/app-graphql')
const { AdminUIApp } = require('@keystonejs/app-admin-ui')
const { PasswordAuthStrategy } = require('@keystonejs/auth-password')
const { MongooseAdapter: Adapter } = require('@keystonejs/adapter-mongoose')
const PROJECT_NAME = 'yt-tm-keystonejs-graphql-api'
const adapterConfig = { mongoUri: process.env.MONGO_URI }
const isAdmin = ({ authentication: { item: user } }) => {
return !!user && !!user.isAdmin
}
const isLoggedIn = ({ authentication: { item: user } }) => {
return !!user
}
const PostSchema = require('./lists/Post')
const UserSchema = require('./lists/User')
const keystone = new Keystone({
adapter: new Adapter(adapterConfig),
cookieSecret: process.env.COOKIE_SECRET
})
keystone.createList('Post', {
fields: PostSchema.fields,
access: {
read: true,
create: isLoggedIn,
update: isLoggedIn,
delete: isLoggedIn
}
})
keystone.createList('User', {
fields: UserSchema.fields,
access: {
read: true,
create: isAdmin,
update: isAdmin,
delete: isAdmin
}
})
const authStrategy = keystone.createAuthStrategy({
type: PasswordAuthStrategy,
list: 'User',
config: {
identifyField: 'email',
secret: 'password'
}
})
module.exports = {
keystone,
apps: [
new GraphQLApp(),
new AdminUIApp({
name: PROJECT_NAME,
enableDefaultRoute: true,
authStrategy,
isAccessAllowed: isAdmin
})
]
}