Update session on hooks

Hello everyone, I'm trying to update a session (only if the user logged in with google or github) this is the code const auth = betterAuth({
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
const session = await getSessionFromCtx(ctx)
console.log(session) // output: is null
console.log(ctx.context.session) // output: is null

await ctx.context.internalAdapter.updateSession(
session!.session.id,
{
...ctx.
}
)
}
else if (ctx.params?.id === 'github') {
console.log('github callback, attaching wallet....')
}
}
}),
}
...
})
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
const session = await getSessionFromCtx(ctx)
console.log(session) // output: is null
console.log(ctx.context.session) // output: is null

await ctx.context.internalAdapter.updateSession(
session!.session.id,
{
...ctx.
}
)
}
else if (ctx.params?.id === 'github') {
console.log('github callback, attaching wallet....')
}
}
}),
}
...
})
session and ctx.context.session are always null.... how can I update the session if the user came from social login?
3 Replies
lonelyplanet
lonelyplanet3d ago
i think its newSession and newUser
lonelyplanet
lonelyplanet3d ago
Hooks | Better Auth
Better Auth Hooks let you customize BetterAuth's behavior
bitangel84
bitangel84OP3d ago
newSession works, but I'm not able to update the session
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.id === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.id,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.id === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.id,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},
I've found the issue... 1. Use ctx.context.newSession 2. Use newSession.session.token to update the session this is the code that works
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.token === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.token,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/callback/:id') {
if (ctx.params?.id === 'google') {
console.log('google callback, attaching wallet....')

const newSession = ctx.context.newSession
if (newSession?.session?.token === undefined) {
console.log('no session found')
return
}

console.log('session is', newSession)

await ctx.context.internalAdapter.updateSession(
newSession.session.token,
{
selectedWallet: 'test',
},
)

console.log('session updated')
}
}
}),
},

Did you find this page helpful?