How to get accesstoken(jwt token) from next-auth
Answered
Crazy ant posted this in #help-forum
Crazy antOP
Dear All,
I have been struggling to get accesstoken from next-auth since I need to use it in api's.
I can able to authenticate and can able to protect my routes. Everything seems to be ok except getting jwt token from getServerSession / useSession / /api/auth/[...nextauth].js
versions:-
"next": "14.2.3",
"next-auth": "^4.24.7",
What I am getting i.e jti , iat,exp etc and do not get jwt token or access token.
Please help.
I have been struggling to get accesstoken from next-auth since I need to use it in api's.
I can able to authenticate and can able to protect my routes. Everything seems to be ok except getting jwt token from getServerSession / useSession / /api/auth/[...nextauth].js
versions:-
"next": "14.2.3",
"next-auth": "^4.24.7",
What I am getting i.e jti , iat,exp etc and do not get jwt token or access token.
Please help.
export const authOptions = {
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60,
},
providers: [
CredentialsProvider({
name: 'Credentials',
async authorize(credentials) {
const { user} = await getUser({email: credentials.email)
return user
},
}),
],
callbacks: {
jwt: async ({ token, user }) => {
console.log(token) //-------------> this gives me jti, iat,exp not jwt token
return { ...token, ...user };
},
session: async ({ session, token }) => {
session.user = token;
return session;
},
},
secret: process.env.NEXTAUTH_SECRET,
};Answered by Crazy ant
I am closing this since no one answered to my query. I brought my own solution. I wanted to check if there is any easier method available. I used iron-session along with next-auth to handle this. But there might be an easier way , I guess.
1 Reply
Crazy antOP
I am closing this since no one answered to my query. I brought my own solution. I wanted to check if there is any easier method available. I used iron-session along with next-auth to handle this. But there might be an easier way , I guess.
Answer