how to add more data to next-auth session (discord provider)
Answered
subscr posted this in #help-forum
subscrOP
Hello guys, i'm using the discord provider and i wanted jwt to have this data:
code: /api/auth/[...nextauth]/route.ts
https://pastebin.com/Wwp4fq9t
it's possible?
{
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;
}
}
...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
@subscr how can i convert this to jwt?
next-auth will convert it
@Ray next-auth will convert it
subscrOP
where can i get this jwt?
in website cookies?
what version of next-auth are you using
subscrOP
the latest
4.24.7
@subscr 4.24.7
you could get that token with
cookies()subscrOP
what is the name?
@subscr what is the name?
check it on your browser, I can't remember it, something like
next-auth.sessionsubscrOP
session-token
not working 😭
subscrOP
do you knoww? @Ray
subscrOP
i need do decode the jwt in a external api
@subscr i need do decode the jwt in a external api
https://next-auth.js.org/configuration/options#override-jwt-encode-and-decode-methods
then you would need to encode and decode your own
then you would need to encode and decode your own
@Ray https://next-auth.js.org/configuration/options#override-jwt-encode-and-decode-methods
then you would need to encode and decode your own
subscrOP
next-auth works in the external api?
to make this
@subscr next-auth works in the external api?
what do you mean
subscrOP
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
@Ray you have to override it with this so you will be able to decode on your backend
subscrOP
where i put this code?
@subscr where i put this code?
you should create a new post for it, this post has been answered already
subscrOP
okay