Refresh Token with next-auth and Axios Interceptors in Next.js 13 Authentication

Refresh Token with next-auth and Axios Interceptors in Next.js 13 Authentication

Sakura Dev

1 год назад

54,127 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Romain Bacque
Romain Bacque - 22.11.2023 01:15

Hi, great tuto ! Just one question, How do you define the expiration date/duration of the access token and of the refresh token ?

Ответить
Ayhm Hamame
Ayhm Hamame - 20.11.2023 00:35

very useful thank you

Ответить
P D
P D - 19.11.2023 15:54

Great one! I wonder if this is safe to get accessToken to client components, is it possible for someone to hijack / steal it ? I was initially using import { getServerSession } from "next-auth/next" and calling backend only from server components / server actions to make sure that accessToken is only processes on the server.

Ответить
Muhammad Naufal Amrizal
Muhammad Naufal Amrizal - 18.11.2023 03:45

superp next auth series videos!!!!! thankyou sir

Ответить
Andres Felipe Laguilavo Ruiz
Andres Felipe Laguilavo Ruiz - 31.10.2023 19:53

Nice video, thanks. I have a question though, how can I get an access token if I use OAuth?

Ответить
Guilherme Fialho
Guilherme Fialho - 29.10.2023 07:06

Really nice video, congrats

Ответить
Temitope Ilesanmi
Temitope Ilesanmi - 29.10.2023 01:35

Thank you for this tutorial. What if the user has been deactivated or the refresh token has been tampered with, after several attempts, I want to log the user out or clear the current user session. Also, how can this work on server side for instance I want to use it with nextauth. Thank you.

Ответить
Kiss Tamás
Kiss Tamás - 24.10.2023 17:40

thank you, this video very helpful. I have a question: what if I want to send request to the backend from the nextjs server side (server components). Your hooks only works on the client side now, do you have any idea?

Ответить
OldNoob
OldNoob - 23.10.2023 07:35

hi i set my refresh token to 300s for test back to sign in automatically, but it never trigger that even if my refresh token 401, can you help me?

Ответить
nerophase
nerophase - 22.10.2023 01:40

great tutorial! thank you!!

Ответить
AI asked AI
AI asked AI - 17.10.2023 12:08

Wouldn't there be a conflict between next-auth's default expires value and your specified expires value in your backend? Also, how do we manage to provide access to the backend APIs if the user is logged in using Google or any other 3rd party provider? How can we create access and refresh tokens for those?

Ответить
Mohd Sajid Ali
Mohd Sajid Ali - 13.10.2023 18:40

Did axios also works same as fetch in caching of api request and deduped

Ответить
Zul
Zul - 11.10.2023 12:55

hello sir, how about handling the refresh token inside middleware? is it convenient?

Ответить
Thưởng Hoàng
Thưởng Hoàng - 22.09.2023 12:07

Thank you so much. I find solution for my site. But I have a problem when I refresh a page(already login), I got an request 401 before I got request 200. I want to check request 401 or request not include authorization token before send request. I also use axios and react-query.

Ответить
Leonardo Muniz
Leonardo Muniz - 21.09.2023 13:56

but is this refresh client side? how do you make it server side?
nice video bro

Ответить
Jony D
Jony D - 19.09.2023 21:58

This is best video. Thank you.

Ответить
Eugene Vilder
Eugene Vilder - 17.09.2023 23:09

Excellent video. Thank you so much. Very detailed.
One think though - I'm not able to find your video about backend implementation of Refresh Token api endpoint. Could you please share a link?

Ответить
Dave Thomson
Dave Thomson - 13.09.2023 18:27

Can you do a video implementing the same token process using a differnet auth api backend. Like Fastapi / Rest

Ответить
Praneeth Kumar
Praneeth Kumar - 06.09.2023 15:18

Bro, when I am trying to use the same useAxiosAuth for fetching the data onload of the page. It is not appending the bearer token to the request headers.

Ответить
Steven Tran
Steven Tran - 31.08.2023 11:57

so amazing

Ответить
Yasser Youssef
Yasser Youssef - 26.08.2023 00:04

Hello again .. as you know, I tried your solution and fixed some issues with it as we discussed before. It is working fine. BUT .. when I try to use the axiosAuth inside useEffect, it works fine when the page loads for the first time and I can see my profile data. But if I clicked on Refresh in the browser to reload the page, I get 401 from the API. After investigating, it turns out that the Next-Auth session is returning "undefined" for the hooks therefore the accessToken and the refreshToken are being passed as "undefined" to the API. why is that? I am stuck for days now

Ответить
Nacho Huerta
Nacho Huerta - 23.08.2023 03:02

Hi, how can apply this with SWR? Thanks!

Ответить
Noe Leiva
Noe Leiva - 09.08.2023 05:34

wow! amazing video man. NextJS has been complicated to work with especially working with hooks. Keep it up. anything MSAL and Next JS related coming up ?

Ответить
askzin joga na 11
askzin joga na 11 - 02.08.2023 16:12

hi! how could i make the session expire and return to the login page whenever my token coming from the server is no longer valid?

Ответить
Chris Legaxy
Chris Legaxy - 02.08.2023 05:19

Thanks for this. But is there a way to update the session of server side? In the axios interceptor, in your code, we can fall-back to refresh token to get the accesToken but it's not updating the session with the new tokens?

Ответить
Successor Onyemaechi
Successor Onyemaechi - 30.07.2023 17:55

Amazing! video.
Is there no way to logout user on the server side? I've read the documentation and made a lot of research still no where to find that solution, please any solution or work around? I will really appreciate.

Ответить
Moh Misaghi
Moh Misaghi - 22.07.2023 17:23

Hi Vahid thanks for the video, how can we deal with the server side requests ?

Ответить
Code School for Geeks
Code School for Geeks - 16.07.2023 05:18

I will buy your course if you make a udemy course on next 13.4. Covering: (Next-auth with access token, refresh token, email verification, reset password and a simple crud blog, and SEO) That would cover everything in one course. Please consider it.

Ответить
Code School for Geeks
Code School for Geeks - 16.07.2023 04:46

I love your videos. Can you please make a video on "How to reset password?"

Ответить
IT Seeker
IT Seeker - 12.07.2023 22:51

What if you have a multi requests? Will the refresh token run many times?

Ответить
Manish Wagale
Manish Wagale - 11.07.2023 12:19

But instead of nodejs to generate refreshToken can you make video to generate refresh token using next api and use it in frontend to access data without accesstoken expiry??

Ответить
KaKun
KaKun - 06.07.2023 09:32

but i thought nextauth handles all of this or did i just misunderstood

Ответить
Ayush Gogna
Ayush Gogna - 04.07.2023 21:06

but what you are doing is create a custom hook and using it in page and for that it requires "use client" do you have solution without adding use client ? and axios does not support next revalidate how to work around that any idea ?

Ответить
Ajay Siddharth Kovid
Ajay Siddharth Kovid - 04.07.2023 10:16

I have watched all your next-auth videos and they are great.
I have encountered one problem on reloading the app from the browser I am not getting the session immediately (on routing I am getting correct session value) which result in app crash.
Can you please help?

Ответить
celio css
celio css - 28.06.2023 17:36

I never understand next-auth but with this series is amazing, now I am in another lever, thanks!

Ответить
ardi zariat
ardi zariat - 19.06.2023 18:45

Thank you.
But i have question, why i hard reload page accessToken and refreshToken lost?

Ответить
Strahinja Cokić
Strahinja Cokić - 09.06.2023 20:14

I do not think this is a good solution. Interesting concept though.
Besides making redundant API calls and intentionally resolving error 401, I faced three problems:
- We update tokens only for current session client side so this will not work on refresh when we get new session and it uses the tokens from cookie (which are unchanged)
- The solution does not support refreshing tokens for multiple sessions when user is using multiple tabs
- The solution does not work with SSR since we only update the tokens client side

I like the idea of using interceptors here though. I might experiment the solution where I am checking if access_token is expired when request is made, and if needed then refetch and update the token.

Ответить
Chieve Id
Chieve Id - 08.06.2023 10:41

Its a great tutorial but i found a problem when i refresh my page. my hearders Authorization accessToken return an Undefined. i am following your useAxiosAuth custom hooks. is there any way to fix it?

Ответить
Yasser Youssef
Yasser Youssef - 03.06.2023 22:37

Can we get the backend server code for the refresh token generation?

Ответить
Satish Sharma
Satish Sharma - 02.06.2023 04:26

Can we use next-auth in nextjs with laravel as backend for API provider.

Ответить
polianych
polianych - 01.06.2023 20:35

Hello, thanks for such a great tutorial. I am trying to use useAxiosAuth as baseQuery in rtk-query createApi. And obviously, I got an error: Invalid hook call. Hooks can only be called inside of the body of a function component. Could you advise how it is better to modify your hooks?

Ответить
Yaroslav Protsenko
Yaroslav Protsenko - 01.06.2023 02:11

Can u explain to me how to use this axios instance if I use redux saga to make requests, when u configured axios instance in the react hook because u need to get session data from their getSession hook, and use it in the component.

Ответить
Haji Koding
Haji Koding - 30.05.2023 11:18

your discord channel link has expired. can i have a new one ?

Ответить
Hien Vinh Nguyen
Hien Vinh Nguyen - 27.05.2023 16:43

I have a issue with refresh token rotation:
1. When user call a api from ssr but access_token_1 is now expired and calls have triggered refreshAccessToken(). Now exchange the refresh_token_1 (disabled by backend) with a new access_token_2 and refresh_token_2
2. User call api from client that used old access_token_1 and refresh_token_1

Ответить
John Covv
John Covv - 22.05.2023 22:41

Thanks

Ответить
Hajime
Hajime - 17.05.2023 13:44

I think it would be also nice to make a guide about how to use something similar like in useAxiosAuth but in getServerSide props or maybe it's not possible? What I mean is to fetch data in SSR but also with included authorization like accessToken, refreshToken and needed data like userId maybe

Ответить
wakibtz
wakibtz - 14.05.2023 22:37

Hi. I have question. Is it secure save tokens like this (in next auth) than in cookies for example?

Ответить
Rafael Huszcza
Rafael Huszcza - 14.05.2023 07:17

Nice video, helps me a lot, nice content, I guess I watched more then 10 videos in only one day, and I know I am learning well about the new features in next 13. But got a problem with using axios and not fetch, because when I use axios interceptors to refresh, i need use axios in all my application, and actually next features like cache, ou next revalidate are only available using fetch, so i guess its necessary use fetch interceptors, you know use something like that?

Ответить