@@ -2,21 +2,34 @@ import { getContext } from '@keystone-6/core/context';
22import { getServerSession } from 'next-auth/next' ;
33import GithubProvider from 'next-auth/providers/github' ;
44import config from './keystone' ;
5- import * as PrismaModule from '.myprisma/client' ;
65import type { Context } from '.keystone/types' ;
6+
77// WARNING: this example is for demonstration purposes only
88// as with each of our examples, it has not been vetted
99// or tested for any particular usage
1010
1111// WARNING: you need to change this
1212const sessionSecret = '-- DEV COOKIE SECRET; CHANGE ME --' ;
1313
14+ let _keystoneContext : Context = ( globalThis as any ) . _keystoneContext ;
15+
16+ async function getKeystoneContext ( ) {
17+ if ( _keystoneContext ) return _keystoneContext ;
18+
19+ // TODO: this could probably be better
20+ _keystoneContext = getContext ( config , await import ( '.myprisma/client' ) ) ;
21+ if ( process . env . NODE_ENV !== 'production' ) {
22+ ( globalThis as any ) . _keystoneContext = _keystoneContext ;
23+ }
24+ return _keystoneContext ;
25+ }
26+
1427// see https://next-auth.js.org/configuration/options for more
1528export const nextAuthOptions = {
1629 secret : sessionSecret ,
1730 callbacks : {
1831 async signIn ( { user, account, profile } : any ) {
19- const sudoContext = getKeystoneContext ( ) . sudo ( ) ;
32+ const sudoContext = ( await getKeystoneContext ( ) ) . sudo ( ) ;
2033 // console.log('Next Auth Sign In Details', { user, account, profile });
2134 // check if the user exists in keystone
2235 const keystoneUser = await sudoContext . query . Author . findOne ( {
@@ -52,18 +65,6 @@ export type Session = {
5265 id : string ;
5366} ;
5467
55- let _keystoneContext : Context = ( globalThis as any ) . _keystoneContext ;
56-
57- function getKeystoneContext ( ) {
58- if ( ! _keystoneContext ) {
59- _keystoneContext = getContext ( config , PrismaModule ) ;
60- if ( process . env . NODE_ENV !== 'production' ) {
61- ( globalThis as any ) . _keystoneContext = _keystoneContext ;
62- }
63- }
64- return _keystoneContext ;
65- }
66-
6768export const nextAuthSessionStrategy = {
6869 async get ( { context } : { context : Context } ) : Promise < Session | undefined > {
6970 const { req, res } = context ;
0 commit comments