-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
45 lines (39 loc) · 1.16 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
import { randomBytes } from "crypto"
import { createAuth } from "@keystone-6/auth"
import { KeystoneContext } from "@keystone-6/core/types"
import { statelessSessions } from "@keystone-6/core/session"
import { TypeInfo } from "./.keystone/types"
import { mailPasswordResetToken } from "./utils/mailPasswordResetToken"
let sessionSecret = process.env.SESSION_SECRET
if (!sessionSecret && process.env.NODE_ENV !== "production") {
sessionSecret = randomBytes(32).toString("hex")
}
const { withAuth } = createAuth({
listKey: "User",
identityField: "email",
sessionData: "id name email",
secretField: "password",
initFirstItem: {
fields: ["name", "email", "password"],
},
passwordResetLink: {
async sendToken({ token, identity, itemId, context }) {
const user = await (
context as any as KeystoneContext<TypeInfo>
).db.User.findOne({ where: { id: itemId as string } })
mailPasswordResetToken({
token,
email: identity,
username: user?.name || "",
})
},
},
})
const sessionMaxAge = 60 * 60 * 24 * 7
const session = statelessSessions({
maxAge: sessionMaxAge,
secret: sessionSecret!,
secure: true,
sameSite: "none",
})
export { withAuth, session }