Skip to content

Commit 191bca8

Browse files
Josh Calderdcousens
authored andcommitted
move all to session.ts
1 parent e46ef13 commit 191bca8

File tree

3 files changed

+56
-54
lines changed

3 files changed

+56
-54
lines changed
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
11
import NextAuth from 'next-auth';
2-
import GithubProvider from 'next-auth/providers/github';
3-
import { getNextSession, onNextSignIn } from '../../../../session';
4-
// WARNING: this example is for demonstration purposes only
5-
// as with each of our examples, it has not been vetted
6-
// or tested for any particular usage
2+
import { nextAuthOptions } from '../../../../session';
73

8-
// WARNING: you need to change this
9-
const sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
10-
11-
// see https://next-auth.js.org/configuration/options for more
12-
export const authOptions = {
13-
secret: sessionSecret,
14-
callbacks: {
15-
signIn: onNextSignIn,
16-
session: getNextSession,
17-
},
18-
providers: [
19-
GithubProvider({
20-
clientId: process.env.GITHUB_ID!,
21-
clientSecret: process.env.GITHUB_SECRET!,
22-
}),
23-
],
24-
};
25-
26-
export default NextAuth(authOptions);
4+
export default NextAuth(nextAuthOptions);

examples/custom-session-next-auth/keystone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { config } from '@keystone-6/core';
22
import { fixPrismaPath } from '../example-utils';
33
import { lists } from './schema';
44

5-
import { Session, nextAuthSession } from './session';
5+
import { Session, nextAuthSessionStrategy } from './session';
66
import type { TypeInfo } from '.keystone/types';
77

88
// WARNING: this example is for demonstration purposes only
@@ -44,5 +44,5 @@ export default config<TypeInfo<Session>>({
4444
},
4545
lists,
4646
// you can find out more at https://keystonejs.com/docs/apis/session#session-api
47-
session: nextAuthSession,
47+
session: nextAuthSessionStrategy,
4848
});

examples/custom-session-next-auth/session.ts

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
import { getContext } from '@keystone-6/core/context';
22
import { getServerSession } from 'next-auth/next';
3-
import { authOptions } from './admin/pages/api/auth/[...nextauth]';
3+
import GithubProvider from 'next-auth/providers/github';
44
import config from './keystone';
55
import * as PrismaModule from '.myprisma/client';
66
import type { Context } from '.keystone/types';
7+
// WARNING: this example is for demonstration purposes only
8+
// as with each of our examples, it has not been vetted
9+
// or tested for any particular usage
10+
11+
// WARNING: you need to change this
12+
const sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --';
13+
14+
// see https://next-auth.js.org/configuration/options for more
15+
export const nextAuthOptions = {
16+
secret: sessionSecret,
17+
callbacks: {
18+
async signIn({ user, account, profile }: any) {
19+
const sudoContext = getKeystoneContext().sudo();
20+
// console.log('Next Auth Sign In Details', { user, account, profile });
21+
// check if the user exists in keystone
22+
const keystoneUser = await sudoContext.query.Author.findOne({
23+
where: { subjectId: profile.id },
24+
});
25+
// if not, create them
26+
if (!keystoneUser) {
27+
await sudoContext.query.Author.createOne({
28+
data: {
29+
subjectId: profile.id,
30+
name: profile.name,
31+
},
32+
});
33+
}
34+
// return true to allow the sign in to complete
35+
return true;
36+
},
37+
async session({ token, session }: any) {
38+
// console.log('Next Auth Session Details', { session, token });
39+
// add the users subjectId and email to the session object
40+
return { ...session, subjectId: token.sub };
41+
},
42+
},
43+
providers: [
44+
GithubProvider({
45+
clientId: process.env.GITHUB_ID!,
46+
clientSecret: process.env.GITHUB_SECRET!,
47+
}),
48+
],
49+
};
750

851
export type Session = {
952
id: string;
1053
};
54+
1155
let _keystoneContext: Context = (globalThis as any)._keystoneContext;
1256

1357
function getKeystoneContext() {
@@ -19,33 +63,8 @@ function getKeystoneContext() {
1963
}
2064
return _keystoneContext;
2165
}
22-
export async function onNextSignIn({ user, account, profile }: any) {
23-
const sudoContext = getKeystoneContext().sudo();
24-
// console.log('Next Auth Sign In Details', { user, account, profile });
25-
// check if the user exists in keystone
26-
const keystoneUser = await sudoContext.query.Author.findOne({
27-
where: { subjectId: profile.id },
28-
});
29-
// if not, create them
30-
if (!keystoneUser) {
31-
await sudoContext.query.Author.createOne({
32-
data: {
33-
subjectId: profile.id,
34-
name: profile.name,
35-
},
36-
});
37-
}
38-
// return true to allow the sign in to complete
39-
return true;
40-
}
41-
42-
export async function getNextSession({ session, token }: any) {
43-
// console.log('Next Auth Session Details', { session, token });
44-
// add the users subjectId and email to the session object
45-
return { ...session, email: token.email, subjectId: token.sub };
46-
}
4766

48-
export const nextAuthSession = {
67+
export const nextAuthSessionStrategy = {
4968
async get({ context }: { context: Context }): Promise<Session | undefined> {
5069
const { req, res } = context;
5170
const { headers } = req ?? {};
@@ -58,7 +77,12 @@ export const nextAuthSession = {
5877
cookies[key] = decodeURIComponent(value);
5978
}
6079
// get the next-auth session
61-
const nextAuthSession = await getServerSession({ headers, cookies } as any, res, authOptions);
80+
// TODO: get types for getServerSession.
81+
const nextAuthSession = await getServerSession(
82+
{ headers, cookies } as any,
83+
res,
84+
nextAuthOptions
85+
);
6286
// get the keystone user using the subjectId
6387
const keystoneAuthor = await context.db.Author.findOne({
6488
where: { subjectId: nextAuthSession?.subjectId },

0 commit comments

Comments
 (0)