-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
54 lines (44 loc) · 1.81 KB
/
auth.ts
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
/*
Welcome to the auth file! Here we have put a config to do basic auth in Keystone.
`createAuth` is an implementation for an email-password login out of the box.
`statelessSessions` is a base implementation of session logic.
For more on auth, check out: https://keystonejs.com/docs/apis/auth#authentication-api
*/
import { createAuth } from "@keystone-6/auth";
// See https://keystonejs.com/docs/apis/session#session-api for the session docs
import { statelessSessions } from "@keystone-6/core/session";
let sessionSecret = process.env.SESSION_SECRET;
// Here is a best practice! It's fine to not have provided a session secret in dev,
// however it should always be there in production.
if (!sessionSecret) {
if (process.env.NODE_ENV === "production") {
throw new Error(
"The SESSION_SECRET environment variable must be set in production"
);
} else {
sessionSecret = "-- DEV COOKIE SECRET; CHANGE ME --";
}
}
// Here we define how auth relates to our schemas.
// What we are saying here is that we want to use the list `User`, and to log in
// we will need their email and password.
const { withAuth } = createAuth({
listKey: "User",
identityField: "email",
sessionData: "name admin",
secretField: "password",
initFirstItem: {
// If there are no items in the database, keystone will ask you to create
// a new user, filling in these fields.
fields: ["name", "email", "password"],
},
});
// This defines how long people will remain logged in for.
// This will get refreshed when they log back in.
let sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
// This defines how sessions should work. For more details, check out: https://keystonejs.com/docs/apis/session#session-api
const session = statelessSessions({
maxAge: sessionMaxAge,
secret: sessionSecret!,
});
export { withAuth, session };