Next-auth: Missing accessToken

Created on 19 Nov 2020  路  4Comments  路  Source: nextauthjs/next-auth

Your question
According to this documentation, when I use this configuration I should expect to see an accessToken property in session. I see every property in the example but the accessToken. What am I missing?

const options = {
    pages: {
        signIn: '/login'
    },
    session: {
        jwt: true
    },
    callbacks: {
        session: async (session: any, jwt: any) => {
            console.log(session)
            console.log(jwt)
            return Promise.resolve(session)
        }
    },
    providers: [
        Providers.Auth0({
            domain: publicRuntimeConfig.AUTH0_DOMAIN,
            clientId: publicRuntimeConfig.AUTH0_CLIENT_ID,
            clientSecret: process.env.AUTH0_APPLICATIONS_NEXTJS_CLIENTSECRET ?? ""
        })
    ],
    debug: true
}

What are you trying to do
I want to use getSession() in my client to retrieve the access token so I can include it in the Bearer header of a request to another API in my application.

Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [ ] Found the documentation helpful
  • [ ] Found documentation but was incomplete
  • [x] Could not find relevant documentation
  • [ ] Found the example project helpful
  • [ ] Did not find the example project helpful
question

All 4 comments

@jonathaneckman You can add it back in using callbacks for the session (you can also do that for the refresh token too).

This worked for me:

callbacks: {

    jwt: async (token, user,  account) => {
      if (account) {
        token.accessToken = account.accessToken;
        token.refreshToken = account.refreshToken;
      }

      return Promise.resolve(token);
    },

    session: async (session, token) => {
      session.accessToken = token.accessToken;
      session.refreshToken = token.refreshToken;
      return Promise.resolve(session);
    },
  },

That was it. Thank you!

I have a similar case with a custom Oauth, but my account is null... Any idea why?

hmm, odd. I added the user param (on JWT callback) to avoid the null problem on my side when I first commented but there may be a better solution

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eatrocks picture eatrocks  路  3Comments

iaincollins picture iaincollins  路  3Comments

Xetera picture Xetera  路  3Comments

ryanditjia picture ryanditjia  路  3Comments

ryanbahan picture ryanbahan  路  3Comments