Next.js Discord

Discord Forum

Axios Hook Authentication

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I'm trying to handle authentication in Next13, my backend is in Django, and it is storing the refresh token in the cookie.
I'm using axios and I'm using a hook that intercepts the request and gets a new access token if the previous one is expired.

Anywhere I use the hook, the component becomes a client component.

I think this is kinda bad practice, because most of my pages are now client components.

Can someone please correct me or tell me how best to go about this.
Thanks

10 Replies

Giant panda
Nextjs is fullstack framework. Its wired using an another backend. Can you show me a code snippet?
Affenpinscher
if you have separate backend react is enough for the front end , for authentication in react use react-auth-kit then you dont need next js
@Giant panda Nextjs is fullstack framework. Its wired using an another backend. Can you show me a code snippet?
Giant pandaOP
Yes. It is a full stack framework, but you can also use it for Frontend only
Singapura
I have the same problem. I did it with a middleware that checks if the token is valid and refreshes it if not. I can't figure out the best way to do it.
please make sure you need next.js in your project before using it, don't make a hastily decision to jump on it if it's not what you actually need
@dank() please make sure you **need** next.js in your project before using it, don't make a hastily decision to jump on it if it's not what you actually need
Singapura
Well we are migrating from version 12 to 13. Please give us any solution to this if you have any. Thanks.
@European sprat Don't use axios+whatever the hook thing is in server components. In fact, you should really consider using fetch, otherwise, caching, deduplication, etc doesn't work
Giant pandaOP
If I'm using fetch, how do I handle the part where I want the request to be intercepted if the access token is expired and get a new access token before proceeding with the request
Giant pandaOP
Thought about this, but haven't figured it out