You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sign in works fine, if i go to the route manually in browser for example:
localhost:3000/api/resource i get the json no problem.
BUT
if do a fetch/axios call from a server component to the same route localhost:3000/api/resource the session is null.
I have a postman collection that I imported, when i login to nextauth and then hit the endpoint I get the data no problem. I see postman is setting up 3 cookies for localhost authjs.callback, authjs.csrf-token, authjs.session-token.
where do i get access to data to setup the cookie on the api calls? I scoured the docs and cannot find an example.
I am doing a workaround, where im getting the session, then passing the api token as an argument and then attaching to header, parsing in route.ts and re-attaching to header to the external api, which is fine, but it would be great if i could just call auth() to get session and grab the token without messing with manually adding to headers on every call.
snippet of ideal route.ts auth handling and attached is a snip of the postman cookies that get set
const session = (await auth()) as any; <---- this is returning null
if (!session?.user) {
return new NextResponse(
JSON.stringify({ status: "fail", message: "You are not logged in" }),
{ status: 401 },
);
}
const config = {
headers: {
Authorization: `Bearer ${session.user.accessToken}`,
},
};
const response = await apiV1.get("/users", config);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
sign in works fine, if i go to the route manually in browser for example:
localhost:3000/api/resource i get the json no problem.
BUT
if do a fetch/axios call from a server component to the same route localhost:3000/api/resource the session is null.
I have a postman collection that I imported, when i login to nextauth and then hit the endpoint I get the data no problem. I see postman is setting up 3 cookies for localhost authjs.callback, authjs.csrf-token, authjs.session-token.
where do i get access to data to setup the cookie on the api calls? I scoured the docs and cannot find an example.
I am doing a workaround, where im getting the session, then passing the api token as an argument and then attaching to header, parsing in route.ts and re-attaching to header to the external api, which is fine, but it would be great if i could just call auth() to get session and grab the token without messing with manually adding to headers on every call.
snippet of ideal route.ts auth handling and attached is a snip of the postman cookies that get set
Beta Was this translation helpful? Give feedback.
All reactions