forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeystone.ts
More file actions
77 lines (73 loc) · 2.89 KB
/
Copy pathkeystone.ts
File metadata and controls
77 lines (73 loc) · 2.89 KB
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
import { config } from '@keystone-6/core';
import { statelessSessions } from '@keystone-6/core/session';
import { createAuth } from '@keystone-6/auth';
import { lists } from './schema';
/**
* TODO: Implement validateItem. Would be invoked by the getItem() method in
* packages/auth/src/getExtendGraphQLSchema.ts
*/
let sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
let sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
// createAuth configures signin functionality based on the config below. Note this only implements
// authentication, i.e signing in as an item using identity and secret fields in a list. Session
// management and access control are controlled independently in the main keystone config.
const { withAuth } = createAuth({
// This is the list that contains items people can sign in as
listKey: 'User',
// The identity field is typically a username or email address
identityField: 'email',
// The secret field must be a password type field
secretField: 'password',
/* TODO -- review this later, it's not implemented yet and not fully designed (e.g error cases)
// This ensures than an item is actually able to sign in
validateItem: ({ item }) => item.isEnabled,
*/
// initFirstItem turns on the "First User" experience, which prompts you to create a new user
// when there are no items in the list yet
initFirstItem: {
// These fields are collected in the "Create First User" form
fields: ['name', 'email', 'password'],
// This is additional data that will be set when creating the first item
itemData: {
// We need to specify that isAdmin is true for the first item, so the user can access the
// Admin UI (see isAccessAllowed in the admin config below)
isAdmin: true,
// Only enabled users can sign in, so we need to set this as well
// TODO: Come back to this when we review how to restrict signin to valid users
// isEnabled: true,
},
},
// Populate session.data based on the authed user
sessionData: 'name isAdmin',
/* TODO -- complete the UI for these features and enable them
passwordResetLink: {
sendToken(args) {
console.log(`Password reset info:`, args);
},
},
magicAuthLink: {
sendToken(args) {
console.log(`Magic auth info:`, args);
},
},
*/
});
// withAuth applies the signin functionality to the keystone config
export default withAuth(
config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
},
lists,
ui: {},
session:
// Stateless sessions will store the listKey and itemId of the signed-in user in a cookie
statelessSessions({
// The maxAge option controls how long session cookies are valid for before they expire
maxAge: sessionMaxAge,
// The session secret is used to encrypt cookie data (should be an environment variable)
secret: sessionSecret,
}),
})
);