Replies: 9 comments 9 replies
-
Did you find a solution? |
Beta Was this translation helpful? Give feedback.
-
I got the same problem/question how to solve this? |
Beta Was this translation helpful? Give feedback.
-
Same problem. Trying to hit external Express API and need to pass in JWT for authorisation. |
Beta Was this translation helpful? Give feedback.
-
If I understand correctly, you can use the |
Beta Was this translation helpful? Give feedback.
-
Hi @ztjustin, it might be too late for you but I hope someone will find this useful I have a separated express server with return res.status(200).json({ accessToken, some, other, data, ... }) Here's my callbacks: {
...,
async jwt({ token, user }) {
// the user object is what returned from the Credentials login, it has `accessToken` from the server `/login` endpoint
// assign the accessToken to the `token` object, so it will be available on the `session` callback
if (user) {
token.accessToken = user.accessToken
}
return token
},
async session({ session, token }) {
// the token object is what returned from the `jwt` callback, it has the `accessToken` that we assigned before
// Assign the accessToken to the `session` object, so it will be available on our app through `useSession` hooks
if (token) {
session.accessToken = token.accessToken
}
return session
}
}, With that setup, you can get your import React, {useEffect} from 'react'
import { useSession } from 'next-auth/react'
const App = () => {
// I am using next-auth v4 (currently beta), and the session is available on the `data` object
const { data } = useSession()
useEffect(() => {
if(data?.accessToken) {
fetch(`/your-server/endpoint`, {
// assign the token as bearer token on your request headers
headers: {
Authorization: `Bearer ${data?.accessToken}`
}
}).then(...).catch(...)
}
}, [data])
return (...)
} or, even better, you can set up a fetcher function somewhere else and assign the Authorization headers when the cc: @JavaJamie @CollinMonahanLab49 maybe you guys need this too |
Beta Was this translation helpful? Give feedback.
-
I can't get this to work. I did the same thing as @vasilenka mentioned above but when I do a fetch outside the app, with postman for example it seems like the token doesn't do anything. |
Beta Was this translation helpful? Give feedback.
-
I used a google auth provider with next auth, I logged with google and the details of google was send to my backend and the response from backend was a jwt token. I need that token to send to backend so that i can access that data , mine code is
|
Beta Was this translation helpful? Give feedback.
-
` import axios from "axios"; const baseURL = export default apiInstance; this is nextAuth.js file import NextAuth from "next-auth"; export default NextAuth({ ` |
Beta Was this translation helpful? Give feedback.
-
use server component and access cookie using cookies() and make a req is secure and pass data to client component or do other operation |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Your question
I have an external API that get the token from (cookies || header.Authorization || req.body). I am stucked due to I could not find the right way to send the token from my next JS app using nextAuth.
What are you trying to do
I was trying to do 2 things:
1 --- from my backend get the cookie: __Secure-next-auth.session-token || next-auth.session-token
2 --- sending token as header Authorization bearer token , but I just could get the payload info by useSession(), so when I tried to get payload from useEffect() hook it fails.
Reproduction
Payload
Feedback
By the away, it is awesome library.
Regards.
Beta Was this translation helpful? Give feedback.
All reactions