Next-auth: Access token available through JWT session v2 vs v3 behavior

Created on 21 Jul 2020  路  3Comments  路  Source: nextauthjs/next-auth

Please refer to the documentation, the example project and existing issues before creating a new issue.

Thank you for an awesome project, I have a question/comment in regards v2 vs v3 behavior, please let me know if I need to clarify it more.

Your question

How to store/retrieve the provider access token with v3 in order to use it for external API calls.

This is follow up on the question by @tsaiDavid

Provided extended version of the example project to illustrate the issue. Look at two branches v2 and v3.
Specifically, it uses the Auth0 provider. And printing out session result on protected page:

V3 output

localhost_3000_protected_v2

V3 output

localhost_3000_protected_v3

What are you trying to do
v2 allows you to expose access token (and other information) via session callback, like so:

    session: async (session, data) => {
      // Access token available in the data for version v2
      // https://github.com/iaincollins/next-auth/compare/main..v3#diff-06cbba295b0b2816cc9c0ff790ff8302R68
      session.custom = "custom session variable";
      session.account = data.account;
      return Promise.resolve(session);
    },

in v3 it is not available anymore and such the code above is not working anymore. I believe that these changes (~L68) are resulting in this descreptency.

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

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

PS: ~I'm trying to get it deployed somewhere for live examples, will update description when succeed.~

Live deployment for v2 and v3 use [email protected]/password credentials. Note: signout functionality somewhat broken.

question

Most helpful comment

Thanks for raising this and going into detail! That is super helpful.

I am not sure if this is a bug or a gap in our documentation for v3 but I'm looking at both and will update with more info shortly.

All 3 comments

Thanks for raising this and going into detail! That is super helpful.

I am not sure if this is a bug or a gap in our documentation for v3 but I'm looking at both and will update with more info shortly.

This should be resolved with the new callbacks in version 3.0!

The User object is now passed to the Session callback, this can be used to look up the configured OAuth Provider accounts for a user by querying the accounts table for the User ID.

If not using a database, the JWT payload is passed instead of a User object. The account data is available to the JWT, where it can also be stored and passed to the Session object, for those using NextAuth.js without a database.

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iaincollins picture iaincollins  路  3Comments

Xetera picture Xetera  路  3Comments

SharadKumar picture SharadKumar  路  3Comments

eatrocks picture eatrocks  路  3Comments

loonskai picture loonskai  路  3Comments