-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
59 lines (49 loc) · 1.47 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
55
56
57
58
59
// If you want to learn more about how Keystone6 OAuth authentication works, please
// visit our documentation at https://keystone-oauth.vercel.app
import 'dotenv/config';
import { createAuth } from 'keystone-6-oauth';
import GoogleProvider from 'keystone-6-oauth/providers/google';
import { statelessSessions } from '@keystone-6/core/session';
let sessionSecret = process.env.SESSION_SECRET;
if (!sessionSecret) {
if (process.env.NODE_ENV === 'production') {
throw new Error(
'The SESSION_SECRET environment variable must be set in production'
);
} else {
sessionSecret = "~~ randomBytes(32).toString('hex') ~~ ";
}
}
const sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
const session = statelessSessions({
maxAge: sessionMaxAge,
secret: sessionSecret!,
});
const { withAuth } = createAuth({
autoCreate: true,
identityField: 'subjectId',
keystonePath: '/admin',
listKey: 'User',
onSignIn: async (props: any) => {
const email = props.user.email as string;
console.log(`🚀 ~ onSignIn: ~ email`, email)
return true;
},
onSignUp: async (props: any) => {
const email = props.user.email as string;
return { email };
},
pages: {
error: '/auth/error',
signIn: '/login',
},
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}),
],
sessionData: `id email`,
sessionSecret,
});
export { session, withAuth };