Can't call `await getJwtToken(ctx)` on new session?

Trying to generate a JWT after the user signs in via email/pass. Current code is:
if (ctx.path.indexOf('/sign-in/email') !== -1) {
const headers = must(ctx.context.responseHeaders);
const session = ctx.context.newSession;

const token = ctx.context.responseHeaders?.get('set-auth-jwt') || (await getJwtToken(ctx));

if (session && token) {
setCookies(headers, {
jwt: token
});
}
if (ctx.path.indexOf('/sign-in/email') !== -1) {
const headers = must(ctx.context.responseHeaders);
const session = ctx.context.newSession;

const token = ctx.context.responseHeaders?.get('set-auth-jwt') || (await getJwtToken(ctx));

if (session && token) {
setCookies(headers, {
jwt: token
});
}
But getJwtToken fails with can't user us undefined, essentially. Is there something I am missing here or is this a bug?
5 Replies
Izak Filmalter
Izak Filmalter2mo ago
Running into the same issue.
Izak Filmalter
Izak Filmalter2mo ago
GitHub
better-auth/packages/better-auth/src/plugins/jwt/index.ts at main ...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Izak Filmalter
Izak Filmalter2mo ago
I'm doing the following hack while we wait for a fix:
const session = ctx.context.newSession

ctx.context.session = session

const token =
ctx.context.responseHeaders?.get('set-auth-jwt') || (await getJwtToken(ctx))
const session = ctx.context.newSession

ctx.context.session = session

const token =
ctx.context.responseHeaders?.get('set-auth-jwt') || (await getJwtToken(ctx))
Let me open an issue.
Izak Filmalter
Izak Filmalter2mo ago
GitHub
Error with getJwtToken on newSession: TypeError: Cannot read prop...
I copied the setup @Bekacru did and ran into the same issue: rocicorp/ztunes#9 (comment) Code: after: createAuthMiddleware(async ctx => { if (ctx.path.indexOf('/callback/') !== -1) { con...
Izak Filmalter
Izak Filmalter2mo ago
GitHub
Fix getJwtToken to look for session or newSession. by izakfil...
Resolves #3353 Summary by cubic Updated getJwtToken to use either session or newSession from context, fixing issues when only newSession is present.

Did you find this page helpful?