Next.js Discord

Discord Forum

Access token from Session Callback - not getting access token from getServerSession()

Unanswered
Oak rough bulletgall wasp posted this in #help-forum
Open in Discord
Oak rough bulletgall waspOP
Hello - I am adding my access token to my session object via the session callback. I can see the token when I am using useSession() or getSession on a client component, but not in when i use getServerSession() in a server component. The session object in the server component just returns a user object with name, email, image. I am needing to fetch data from my backend API using the access token in the server component. Is this possible?

I am using the App Router, if that helps.

10 Replies

Oak rough bulletgall waspOP
I can see the accessToken inside of my token object in the session callback. But when I assign session.accessToken = token.accessToken I do not see anything besides the user object in the session I get from getServerSession()
Bump, I'm having the same issue. (Did you find a solution, @Oak rough bulletgall wasp?)
@SpicyJungle Bump, I'm having the same issue. (Did you find a solution, <@107269408283439104>?)
Oak rough bulletgall waspOP
Pass your authOptions into the getServerSession call
Should get your access token back as long as you set it in your session callback
I do that, still only get name, email, image
Oak rough bulletgall waspOP
What does your session callback look like?
@Oak rough bulletgall wasp What does your session callback look like?
    jwt: async ({ token, user, account }) => {
      if (account?.access_token) {
        token.accessToken = account.access_token ?? "";
      }

      return token;
    },
    session: async ({ session, token }) => {
      session.accessToken = token.accessToken as string;
      
      if (session.user) {
        session.user.id = token.sub!;
      }
      
      return session;
    }

i do get the token and all when I used useSession
Oak rough bulletgall waspOP
And when you call getServerSession, you pass in the authOptions as a parameter?
Oak rough bulletgall waspOP
That was what ultimately fixed it for me if I remember correctly