11import { getContext } from '@keystone-6/core/context' ;
22import { getServerSession } from 'next-auth/next' ;
3- import { authOptions } from './admin/pages/api/ auth/[...nextauth] ' ;
3+ import GithubProvider from 'next- auth/providers/github ' ;
44import config from './keystone' ;
55import * as PrismaModule from '.myprisma/client' ;
66import 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
851export type Session = {
952 id : string ;
1053} ;
54+
1155let _keystoneContext : Context = ( globalThis as any ) . _keystoneContext ;
1256
1357function 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