Attach JWT token to all axios requests #6260
Unanswered
harrisonratcliffe
asked this question in
Help
Replies: 3 comments 5 replies
-
hi, did you solved it?? i also need to attach it to axios |
Beta Was this translation helpful? Give feedback.
2 replies
-
You could save the token in cookies from
|
Beta Was this translation helpful? Give feedback.
3 replies
-
Call the session in a server component, then pass it to a client component, finally assign the token from session to axios. // ServerComponent.js
export default async function ServerComponent(children) {
const session = await getSession();
return <ClientComponent session={session}>{children}</ClientComponent>
}
// ClientComponent.js
'use client'
import { SessionProvider} from 'next-auth/react'
import { axios } from "axios"
export default async function ClientComponent(session, children) {
axios.defaults.headers.common['Authorization'] = `Bearer ${session?.tokens.access}`
return <SessionProvider session={session}>{children}</SessionProvider>
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Question 💬
Hello, after successful login I fetch a JWT token from my backend API and store it as
session.accessToken
. How can I attach that to all axios requests? I tried using an axios interceptor with getSession inside a axios hook but that is returning as undefined.I'm able to get the access token fine using something like getServerSideProps but having issues getting it from my axios hook.
How to reproduce ☕️
Attempt to talk to my backend API using the above hook.
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Beta Was this translation helpful? Give feedback.
All reactions