Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-buttons-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": major
---

The `Session` type is now set by augmenting the `Session` interface in `.keystone/types` instead of being a generic on various types exported by `.keystone/types`
4 changes: 2 additions & 2 deletions examples/auth-magic-link/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { config } from '@keystone-6/core'
import { statelessSessions } from '@keystone-6/core/session'
import { createAuth } from '@keystone-6/auth'
import { type Session, lists, extendGraphqlSchema } from './schema'
import { lists, extendGraphqlSchema } from './schema'
import type { TypeInfo } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
Expand Down Expand Up @@ -38,7 +38,7 @@ const { withAuth } = createAuth({
},
})

export default withAuth<TypeInfo<Session>>(
export default withAuth<TypeInfo>(
config<TypeInfo>({
db: {
provider: 'sqlite',
Expand Down
13 changes: 8 additions & 5 deletions examples/auth-magic-link/schema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { gWithContext, list } from '@keystone-6/core'
import { allowAll, denyAll } from '@keystone-6/core/access'
import { password, text, timestamp } from '@keystone-6/core/fields'
import type { Lists, Context } from '.keystone/types'
import type { Lists, Context, Session } from '.keystone/types'

import { randomBytes } from 'node:crypto'

const g = gWithContext<Context>()
type g<T> = gWithContext.infer<T>

export type Session = {
itemId: string
declare module '.keystone/types' {
interface Session {
itemId: string
listKey: string
}
}

function hasSession({ session }: { session?: Session }) {
Expand Down Expand Up @@ -83,7 +86,7 @@ export const lists = {
oneTimeTokenCreatedAt: timestamp({ ...hiddenField }),
},
}),
} satisfies Lists<Session>
} satisfies Lists

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand Down Expand Up @@ -176,7 +179,7 @@ export const extendGraphqlSchema = g.extend(base => {
await context.sessionStrategy.start({
context,
data: {
listkey: 'User',
listKey: 'User',
itemId: userId,
},
})
Expand Down
4 changes: 2 additions & 2 deletions examples/auth/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { config } from '@keystone-6/core'
import { statelessSessions } from '@keystone-6/core/session'
import { createAuth } from '@keystone-6/auth'
import { type Session, lists } from './schema'
import { lists } from './schema'
import type { TypeInfo } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
Expand Down Expand Up @@ -47,7 +47,7 @@ const { withAuth } = createAuth({
sessionData: 'isAdmin',
})

export default withAuth<TypeInfo<Session>>(
export default withAuth<TypeInfo>(
config<TypeInfo>({
db: {
provider: 'sqlite',
Expand Down
15 changes: 8 additions & 7 deletions examples/auth/schema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { list } from '@keystone-6/core'
import { allowAll, denyAll } from '@keystone-6/core/access'
import { text, checkbox, password } from '@keystone-6/core/fields'
import type { Lists } from '.keystone/types'
import type { Lists, Session } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
// or tested for any particular usage

export type Session = {
itemId: string
data: {
isAdmin: boolean
declare module '.keystone/types' {
interface Session {
itemId: string
data: {
isAdmin: boolean
}
}
}

Expand Down Expand Up @@ -156,4 +157,4 @@ export const lists = {
}),
},
}),
} satisfies Lists<Session>
} satisfies Lists
22 changes: 12 additions & 10 deletions examples/custom-output-paths/my-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ type ResolvedPostUpdateInput = {
publishDate?: import('./node_modules/myprisma').Prisma.PostUpdateInput['publishDate']
}

export interface Session {}

export declare namespace Lists {
export type Post<Session = any> = import('@keystone-6/core/types').ListConfig<Lists.Post.TypeInfo<Session>>
export type Post = import('@keystone-6/core/types').ListConfig<Lists.Post.TypeInfo>
namespace Post {
export type Item = import('./node_modules/myprisma').Post
export type TypeInfo<Session = any> = {
export type TypeInfo = {
key: 'Post'
isSingleton: false
fields: 'id' | 'title' | 'content' | 'publishDate'
Expand All @@ -156,25 +158,25 @@ export declare namespace Lists {
create: ResolvedPostCreateInput
update: ResolvedPostUpdateInput
}
all: __TypeInfo<Session>
all: __TypeInfo
}
}
}
export type Context<Session = any> = import('@keystone-6/core/types').KeystoneContext<TypeInfo<Session>>
export type Config<Session = any> = import('@keystone-6/core/types').KeystoneConfig<TypeInfo<Session>>
export type Context = import('@keystone-6/core/types').KeystoneContext<TypeInfo>
export type Config = import('@keystone-6/core/types').KeystoneConfig<TypeInfo>

export type TypeInfo<Session = any> = {
export type TypeInfo = {
lists: {
readonly Post: Lists.Post.TypeInfo<Session>
readonly Post: Lists.Post.TypeInfo
}
prisma: import('./node_modules/myprisma').PrismaClient
session: Session
}

type __TypeInfo<Session = any> = TypeInfo<Session>
type __TypeInfo = TypeInfo

export type Lists<Session = any> = {
[Key in keyof TypeInfo['lists']]?: import('@keystone-6/core/types').ListConfig<TypeInfo<Session>['lists'][Key]>
export type Lists = {
[Key in keyof TypeInfo['lists']]?: import('@keystone-6/core/types').ListConfig<TypeInfo['lists'][Key]>
} & Record<string, import('@keystone-6/core/types').ListConfig<any>>

export {}
6 changes: 3 additions & 3 deletions examples/custom-session-invalidation/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { config } from '@keystone-6/core'
import { statelessSessions } from '@keystone-6/core/session'
import { createAuth } from '@keystone-6/auth'
import { type Session, lists } from './schema'
import type { Config, Context, TypeInfo } from '.keystone/types'
import { lists } from './schema'
import type { Config, Context, TypeInfo, Session } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand Down Expand Up @@ -32,7 +32,7 @@ const { withAuth } = createAuth({
sessionData: 'passwordChangedAt',
})

function withSessionInvalidation(config: Config<Session>): Config<Session> {
function withSessionInvalidation(config: Config): Config {
const existingSessionStrategy = config.session!

return {
Expand Down
18 changes: 10 additions & 8 deletions examples/custom-session-invalidation/schema.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { list } from '@keystone-6/core'
import { denyAll, unfiltered } from '@keystone-6/core/access'
import { text, password, timestamp } from '@keystone-6/core/fields'
import type { Lists } from '.keystone/types'
import type { Lists, Session } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
// or tested for any particular usage

// needs to be compatible with withAuth
export type Session = {
listKey: string
itemId: string
data: {
passwordChangedAt: string
declare module '.keystone/types' {
interface Session {
listKey: string
itemId: string
data: {
passwordChangedAt: string
}
startedAt: number
}
startedAt: number
}

function hasSession({ session }: { session?: Session }) {
Expand Down Expand Up @@ -85,4 +87,4 @@ export const lists = {
},
},
}),
} satisfies Lists<Session>
} satisfies Lists
4 changes: 2 additions & 2 deletions examples/custom-session-jwt/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jwt from 'jsonwebtoken'
import { config } from '@keystone-6/core'
import { lists, type Session } from './schema'
import type { Context, TypeInfo } from '.keystone/types'
import { lists } from './schema'
import type { Context, TypeInfo, Session } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand Down
12 changes: 7 additions & 5 deletions examples/custom-session-jwt/schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { list } from '@keystone-6/core'
import { allowAll, unfiltered, allOperations } from '@keystone-6/core/access'
import { checkbox, text } from '@keystone-6/core/fields'
import type { Lists } from '.keystone/types'
import type { Lists, Session } from '.keystone/types'

export type Session = {
id: string
admin: boolean
declare module '.keystone/types' {
interface Session {
id: string
admin: boolean
}
}

function hasSession({ session }: { session?: Session }) {
Expand Down Expand Up @@ -61,4 +63,4 @@ export const lists = {
admin: checkbox(),
},
}),
} satisfies Lists<Session>
} satisfies Lists
4 changes: 2 additions & 2 deletions examples/custom-session-next-auth/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { config } from '@keystone-6/core'
import { lists } from './schema'

import { type Session, nextAuthSessionStrategy } from './session'
import { nextAuthSessionStrategy } from './session'
import type { TypeInfo } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
// or tested for any particular usage

export default config<TypeInfo<Session>>({
export default config<TypeInfo>({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
Expand Down
5 changes: 2 additions & 3 deletions examples/custom-session-next-auth/schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { denyAll, allOperations } from '@keystone-6/core/access'
import { list } from '@keystone-6/core'
import { text, relationship } from '@keystone-6/core/fields'
import type { Session } from './session'
import type { Lists } from '.keystone/types'
import type { Lists, Session } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand Down Expand Up @@ -43,4 +42,4 @@ export const lists = {
posts: relationship({ ref: 'Post.author', many: true }),
},
}),
} satisfies Lists<Session>
} satisfies Lists
6 changes: 4 additions & 2 deletions examples/custom-session-next-auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ export const nextAuthOptions = {
],
}

export type Session = {
id: string
declare module '.keystone/types' {
interface Session {
id: string
}
}

export const nextAuthSessionStrategy = {
Expand Down
9 changes: 5 additions & 4 deletions examples/custom-session-passport/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { type VerifyCallback } from 'passport-oauth2'
import { Strategy, type StrategyOptions, type Profile } from 'passport-github2'

import { type Author } from 'myprisma'
import { type TypeInfo } from '.keystone/types'

export type Session = Author
import type { Session, TypeInfo } from '.keystone/types'

declare module '.keystone/types' {
export interface Session extends Author {}
}
export const session = statelessSessions<Session>({
maxAge: 60 * 60 * 24 * 30,
secret: process.env.SESSION_SECRET!,
Expand All @@ -30,7 +31,7 @@ const options: StrategyOptions = {
callbackURL: 'http://localhost:3000/auth/github/callback',
}

export function passportMiddleware(commonContext: KeystoneContext<TypeInfo<Session>>): Router {
export function passportMiddleware(commonContext: KeystoneContext<TypeInfo>): Router {
const router = Router()
const instance = new Passport()
const strategy = new Strategy(
Expand Down
4 changes: 2 additions & 2 deletions examples/custom-session-passport/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'dotenv/config'
import { config } from '@keystone-6/core'
import { type TypeInfo } from '.keystone/types'
import { lists } from './schema'
import { type Session, session, passportMiddleware } from './auth'
import { session, passportMiddleware } from './auth'

export default config<TypeInfo<Session>>({
export default config<TypeInfo>({
db: {
provider: 'sqlite',
url: 'file:./keystone.db',
Expand Down
6 changes: 2 additions & 4 deletions examples/custom-session-passport/schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { denyAll, allOperations } from '@keystone-6/core/access'
import { list } from '@keystone-6/core'
import { text, relationship } from '@keystone-6/core/fields'
import type { Lists } from '.keystone/types'

import type { Session } from './auth'
import type { Lists, Session } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand Down Expand Up @@ -44,4 +42,4 @@ export const lists = {
posts: relationship({ ref: 'Post.author', many: true }),
},
}),
} satisfies Lists<Session>
} satisfies Lists
6 changes: 3 additions & 3 deletions examples/custom-session-redis/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { config } from '@keystone-6/core'
import { storedSessions } from '@keystone-6/core/session'
import { createAuth } from '@keystone-6/auth'
import { createClient } from '@redis/client'
import { lists, type Session } from './schema'
import type { TypeInfo } from '.keystone/types'
import { lists } from './schema'
import type { TypeInfo, Session } from '.keystone/types'

// WARNING: this example is for demonstration purposes only
// as with each of our examples, it has not been vetted
Expand Down Expand Up @@ -57,7 +57,7 @@ function redisSessionStrategy() {
}

export default withAuth(
config<TypeInfo<Session>>({
config<TypeInfo>({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
Expand Down
Loading
Loading