File tree Expand file tree Collapse file tree 2 files changed +21
-9
lines changed
examples/better-auth/server Expand file tree Collapse file tree 2 files changed +21
-9
lines changed Original file line number Diff line number Diff line change 11import { defineGraphQLConfig } from 'nitro-graphql/define'
22import { createDefaultMaskError } from 'nitro-graphql/utils'
33import { tables } from '../drizzle'
4- import { auth } from '../utils/auth'
54import { useDatabase } from '../utils/useDb'
65
76// Custom GraphQL Yoga configuration with Better Auth integration
@@ -13,18 +12,11 @@ export default defineGraphQLConfig({
1312 context : async ( event ) => {
1413 const db = useDatabase ( )
1514
16- // Retrieve session from Better Auth
17- const session = await auth . api . getSession ( {
18- headers : event . headers ,
19- } )
20-
2115 return {
2216 context : {
17+ ...event . context ,
2318 tables,
2419 database : db ,
25- // Better Auth session and user
26- session : session ?. session ?? null ,
27- user : session ?. user ?? null ,
2820 } ,
2921 }
3022 } ,
Original file line number Diff line number Diff line change 1+ import { defineEventHandler } from 'nitro/h3'
2+ import { auth } from '../utils/auth'
3+
4+ export default defineEventHandler ( async ( event ) => {
5+ try {
6+ const session = await auth . api . getSession ( {
7+ headers : event . req . headers ,
8+ } )
9+
10+ if ( session ?. session ) {
11+ // You can save the session to the event context for later use
12+ event . context . session = session . session
13+ event . context . user = session . user
14+ }
15+ }
16+ catch ( error ) {
17+ event . context . session = null
18+ event . context . user = null as any
19+ }
20+ } )
You can’t perform that action at this time.
0 commit comments