11import { config } from '@keystone-6/core'
2- import { statelessSessions } from '@keystone-6/core/session'
3- import { createAuth } from '@keystone-6/auth'
2+ import { createAuth , SessionStrategy , statelessSessions } from '@keystone-6/auth'
43import { lists } from './schema'
5- import type { Config , Context , TypeInfo , Session } from '.keystone/types'
4+ import type { TypeInfo , Lists } from '.keystone/types'
5+ import type { BaseKeystoneTypeInfo } from '@keystone-6/core/types'
66
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// withAuth is a function we can use to wrap our base configuration
12- const { withAuth } = createAuth ( {
12+ const { withAuth } = createAuth < Lists . User . TypeInfo , { itemId : string ; startedAt : number } > ( {
1313 // this is the list that contains our users
1414 listKey : 'User' ,
1515
@@ -29,55 +29,57 @@ const { withAuth } = createAuth({
2929 fields : [ 'name' , 'password' ] ,
3030 } ,
3131
32- sessionData : 'passwordChangedAt' ,
32+ sessionStrategy : withSessionStartedAt ( statelessSessions ( ) ) ,
33+ async getSession ( { context, data } ) {
34+ const user = await context . db . User . findOne ( {
35+ where : { id : data . itemId } ,
36+ } )
37+ if ( ! user ) return
38+ if ( user . passwordChangedAt && user . passwordChangedAt > new Date ( data . startedAt ) ) {
39+ return
40+ }
41+ return { user }
42+ } ,
3343} )
3444
35- function withSessionInvalidation ( config : Config ) : Config {
36- const existingSessionStrategy = config . session !
37-
45+ function withSessionStartedAt < T , TypeInfo extends BaseKeystoneTypeInfo > (
46+ existingSessionStrategy : SessionStrategy <
47+ T & { startedAt : number } ,
48+ T & { startedAt : number } ,
49+ TypeInfo
50+ >
51+ ) : SessionStrategy < T , T & { startedAt : number } , TypeInfo > {
3852 return {
39- ...config ,
40- session : {
41- ...existingSessionStrategy ,
42- async get ( { context } : { context : Context } ) : Promise < Session | undefined > {
43- const session = await existingSessionStrategy . get ( { context } )
44- if ( ! session ) return
45-
46- // has the password changed since the session started?
47- if ( new Date ( session . data . passwordChangedAt ) > new Date ( session . startedAt ) ) {
48- // invalidate the session if password changed
49- await existingSessionStrategy . end ( { context } )
50- return
51- }
52-
53- return session
54- } ,
55- async start ( { context, data } : { context : Context ; data : Session } ) {
56- return await existingSessionStrategy . start ( {
57- context,
58- data : {
59- ...data ,
60- startedAt : Date . now ( ) ,
61- } ,
62- } )
63- } ,
53+ async start ( { context, data } ) {
54+ await existingSessionStrategy . start ( {
55+ context,
56+ data : { ...data , startedAt : Date . now ( ) } ,
57+ } )
58+ } ,
59+ async get ( { context } ) {
60+ const session = await existingSessionStrategy . get ( { context } )
61+ if (
62+ ! session ||
63+ typeof session !== 'object' ||
64+ ! ( 'startedAt' in session ) ||
65+ typeof session . startedAt !== 'number'
66+ )
67+ return
68+ return { ...session , startedAt : session . startedAt }
6469 } ,
70+ end : existingSessionStrategy . end ,
6571 }
6672}
6773
68- export default withSessionInvalidation (
69- withAuth (
70- config < TypeInfo > ( {
71- db : {
72- provider : 'sqlite' ,
73- url : process . env . DATABASE_URL || 'file:./keystone-example.db' ,
74+ export default withAuth (
75+ config < TypeInfo > ( {
76+ db : {
77+ provider : 'sqlite' ,
78+ url : process . env . DATABASE_URL || 'file:./keystone-example.db' ,
7479
75- // WARNING: this is only needed for our monorepo examples, dont do this
76- prismaClientPath : 'node_modules/myprisma' ,
77- } ,
78- lists,
79- // you can find out more at https://keystonejs.com/docs/apis/session#session-api
80- session : statelessSessions < Session > ( ) ,
81- } )
82- )
80+ // WARNING: this is only needed for our monorepo examples, dont do this
81+ prismaClientPath : 'node_modules/myprisma' ,
82+ } ,
83+ lists,
84+ } )
8385)
0 commit comments