I just followed the docs for Auth.js v5 to implement login via GitLab. I am using version 5.0.0-beta.21" (version 22 is broken).
The login goes well. But when I want to retreive my session info (here in a server component):
const session = await auth()
I get this error message and the value of session is null:
[auth][error] JWTSessionError: Read more at https://errors.authjs.dev#jwtsessionerror
[auth][cause]: TypeError: Cannot read properties of undefined (reading 'name')
at Module.session (webpack-internal:///(rsc)/./node_modules/@auth/core/lib/actions/session.js:37:41)
at async AuthInternal (webpack-internal:///(rsc)/./node_modules/@auth/core/lib/index.js:47:24)
at async Auth (webpack-internal:///(rsc)/./node_modules/@auth/core/index.js:127:34)
at async OverviewPage (webpack-internal:///(rsc)/./src/app/intake/overview/page.tsx:15:25)
[auth][details]: {}
Here is my auth.ts:
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
GitLab({
authorization: `${GITLAB_URL}/oauth/authorize?scope=read_user`,
token: `${GITLAB_URL}/oauth/token`,
userinfo: `${GITLAB_URL}/api/v4/user`,
})
],
callbacks: {
jwt({ token, user, account, trigger }) {
if (user) {
token.id = user.id
}
if (account?.provider === "GitLab") {
return { ...token, accessToken: account?.access_token }
}
},
session({ session, token }) {
session.user.id = token.id
session.accessToken = token.accessToken
return session
},
},
session: {
strategy: "jwt",
},
})
Apparently it fails to extract a user name from decoding the token, but I don't care about the user at all. My goal is to get the token (which does not seem to be a JWT) so that I can call my backend endpoints with it.
What am I doing wrong ?