How do I get my next-auth.session-token in Cookies
Answered
Almond stone wasp posted this in #help-forum
Almond stone waspOP
I need to access my next-auth.session-token value that is stored in Cookies to use in a specific API call but I cannot figure out how to retrieve it. Especially on the server side. Where do I start?
Answered by Almond stone wasp
Thank you for the response. I ended up going a different direction and using the next-auth callbacks and then import { getServerSession } from 'next-auth/next';
import { authOptions } from '../api/auth/[...nextauth]/route'; and then const session = await getServerSession(authOptions);
import { authOptions } from '../api/auth/[...nextauth]/route'; and then const session = await getServerSession(authOptions);
4 Replies
Toyger
session callback maybe, or if you want in some of your api/server actions then you need to use
or if you want like cookie itself, you can use cookies https://nextjs.org/docs/app/api-reference/functions/cookies
getServerSessionor if you want like cookie itself, you can use cookies https://nextjs.org/docs/app/api-reference/functions/cookies
Almond stone waspOP
hmm trying that now and the jwt using next cookies is not correct. Looking into getServerSession next
Hi, you can get the token by calling the
In the example above, I have a utility file of
getSession function which is part of the next-auth/react package. It returns the logged-in user's data alongside its token.import { getSession } from "next-auth/react"
export const getClientAccessToken = async () => {
const session = await getSession()
const accessToken = session?.user.token
return accessToken
}In the example above, I have a utility file of
auth.js and use the function where I need it.@Hassib Hi, you can get the token by calling the `getSession` function which is part of the `next-auth/react` package. It returns the logged-in user's data alongside its token.
jsx
import { getSession } from "next-auth/react"
export const getClientAccessToken = async () => {
const session = await getSession()
const accessToken = session?.user.token
return accessToken
}
In the example above, I have a utility file of `auth.js` and use the function where I need it.
Almond stone waspOP
Thank you for the response. I ended up going a different direction and using the next-auth callbacks and then import { getServerSession } from 'next-auth/next';
import { authOptions } from '../api/auth/[...nextauth]/route'; and then const session = await getServerSession(authOptions);
import { authOptions } from '../api/auth/[...nextauth]/route'; and then const session = await getServerSession(authOptions);
Answer