Next.js Discord

Discord Forum

getServerSession in server component not returning access token etc with next-auth/auth.js

Unanswered
SpicyJungle posted this in #help-forum
Open in Discord
Hi, I'm trying to use auth.js and server components in next.js 14.9. When using useSession and the session provider, I get everything i need, like email, username, id, and the accessToken. However, when I call getServerSession and pass the authOptions as an argument, I only get email, username and image.

My component:
export default async function AuthenticatedLanding() {
  const data = await getServerSession(authOptions);
  if (!data) return <></>;

  console.log(data); // only logs email, name and image in the object

  ...


Auth.js Callbacks:
  callbacks: {
    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!;
      }
      
      console.log(session);
      return session;
    }
  },



next-auth.d.ts:
/* eslint-disable no-unused-vars */

import type { Session, User } from 'next-auth';
import type { JWT } from 'next-auth/jwt';

type UserId = string;

declare module 'next-auth/jwt' {
  interface JWT {
    id: UserId;
  }
}

declare module 'next-auth' {
  interface Session {
    user: User & { id: UserId };
    accessToken: string;
  }
}


thanks!

2 Replies

Bymping this post, still having this issue
Schneider’s Smooth-fronted Caiman
bump