0

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 ?

2
  • I'm also getting this with 5.0.0-beta.25 - did you find a solution? Commented Nov 23, 2024 at 20:07
  • @Hades I added my bad solution below. Commented Nov 24, 2024 at 14:26

1 Answer 1

1

I ended up doing this :

    callbacks: {
        async jwt({ token, user, account, profile }) {
            if (account) {
                token.id = profile?.id
            }
            if (user) {
                return { ...token, ...user, ...account };
            }
            return token;
        },
        async session({ token }) {
            return token;
        },

, which is bad because it exposes too much information in the session object, but at least gets rid of the error. If you copy this, make sure to narrow down the session data.

Let's not criticize too much Auth.js for the typing and the inexistent docs, because v5 is a beta version after all (v4 does not support the app router and has even worse docs).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.