Next.js Discord

Discord Forum

how to add more data to next-auth session (discord provider)

Answered
subscr posted this in #help-forum
Open in Discord
Hello guys, i'm using the discord provider and i wanted jwt to have this data:

{
  version:2,
  user: { id: "userid" },
  oauth: {access_token:"oauth2 token"},
  expires: Date.now()+(5*3600*24*1000)
}


code: /api/auth/[...nextauth]/route.ts
https://pastebin.com/Wwp4fq9t
it's possible?
Answered by Ray
try
declare module "next-auth" {
  interface Session {
    version: number;
    oauth: {
      access_token: string;
    };
    user: {
      id: string;
    } & DefaultSession["user"];
  }
}

...
callbacks: {
  session({ session }) {
      session.version = 2;
      session.oauth = {
        access_token: "",
      };
      session.user.id = "";
      return session;
  }
}
...
View full answer

30 Replies

@subscr Hello guys, i'm using the discord provider and i wanted jwt to have this data: { version:2, user: { id: "userid" }, oauth: {access_token:"oauth2 token"}, expires: Date.now()+(5*3600*24*1000) } code: /api/auth/[...nextauth]/route.ts https://pastebin.com/Wwp4fq9t it's possible?
try
declare module "next-auth" {
  interface Session {
    version: number;
    oauth: {
      access_token: string;
    };
    user: {
      id: string;
    } & DefaultSession["user"];
  }
}

...
callbacks: {
  session({ session }) {
      session.version = 2;
      session.oauth = {
        access_token: "",
      };
      session.user.id = "";
      return session;
  }
}
...
Answer
okay
worked, thanks
@Ray hey
how can i convert this to jwt?
@subscr how can i convert this to jwt?
next-auth will convert it
@Ray next-auth will convert it
where can i get this jwt?
in website cookies?
@subscr in website cookies?
yes
what version of next-auth are you using
the latest
4.24.7
@subscr 4.24.7
you could get that token with cookies()
what is the name?
@subscr what is the name?
check it on your browser, I can't remember it, something like next-auth.session
session-token
@Ray
not working 😭
do you knoww? @Ray
i need do decode the jwt in a external api
to make this
@subscr next-auth works in the external api?
what do you mean
i'm sending the jwt to the external api and decoding with jsonwebtoken
next-auth use jwe which mean the token is encrypted
@Ray https://next-auth.js.org/configuration/options#override-jwt-encode-and-decode-methods then you would need to encode and decode your own
you have to override it with this so you will be able to decode on your backend
@subscr where i put this code?
you should create a new post for it, this post has been answered already
okay