React Login Authentication with JWT Access, Refresh Tokens, Cookies and Axios

React Login Authentication with JWT Access, Refresh Tokens, Cookies and Axios

Dave Gray

2 года назад

714,626 Просмотров

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


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

Thomas Munguya
Thomas Munguya - 14.10.2023 04:42

I love how you explain concepts in detail. You go as far as explaining the motivation behind every decision made. Great tutorial and thanks!.

Ответить
Your Casual Developer
Your Casual Developer - 08.10.2023 16:04

I love you Dave ❤❤❤❤❤❤❤❤❤❤❤❤❤❤

Ответить
kodewala
kodewala - 04.10.2023 15:08

A perfect solution to my every problem. Trust me this video is going to help me a lot.

Ответить
Karacan
Karacan - 03.10.2023 10:58

Hey Dave, first of all, thank you for your tutorial. I learned a lot and used the method you described in my Next.js 13 app. Everything seems to be working fine, and I also used SWR. Could you please let me know if I did anything wrong by using it next13 and swr?

Best of luck!

Ответить
Morris
Morris - 01.10.2023 22:30

You know, when you don't take the time to respnod, you don't realize how stale your content gets. In this world anything over a year old is suspect. Otherwise good stuff.

Ответить
Kove
Kove - 29.09.2023 20:26

If you get 403 status when you try to refresh token, it's probably because during login cookies might not be saved in the browser. To fix it, you have to change siteName attribute from 'None' to 'Strict' when you are sending cookies ( Login and Refresh Token route)

Ответить
Samuel Araujo de Souza
Samuel Araujo de Souza - 26.09.2023 22:41

Hey, where can I found the back-end of this project?

Ответить
Hector Santos
Hector Santos - 16.09.2023 22:38

Do i have to manually redirect the user to login in every request or can this be automated

Ответить
Codehood
Codehood - 11.09.2023 17:14

Thank you so much

Ответить
Htoo Maung Thait
Htoo Maung Thait - 11.09.2023 08:06

Thank for showing way to implement role based authentication and access control in react.

Ответить
Hao Chung
Hao Chung - 08.09.2023 05:11

What an amazing tutorial, Dave!
Thank you very much for all your efforts putting in these videos!
I've been wondering that why we shouldn't store JWT in local storage, because when we make a private request, we attach our JWT in the header ("Authorization": "Bearer ..."), so someone can copy that token from the request header. Why is this method more secured?

Ответить
YeOldeKnob
YeOldeKnob - 05.09.2023 20:04

Anyone else have trouble with the useRefreshToken / GET request on the /refreshToken endpoint? If Im running my backend on 3001 and my React frontend on 3000, the GET request doesn't send any cookies via the headers so I get a 401 error... It's been driving me absolutely crazy since the GET request works fine via Postman... I'm begging for someone to review my code here because it's seriously putting a damper on my project

Ответить
Marko Askovic
Marko Askovic - 05.09.2023 17:52

Hello! Thanks for the great tutorial! I have read in some tutorials that the refresh token should also be renewed (so the previous refresh token is canceled), and sent back to the client, each time the access token is renewed. Is this correct or not ?

Ответить
Igor Lamoia
Igor Lamoia - 04.09.2023 14:48

Amazing! I was thinking about axios hooks to redirect the user after try to refresh the token. This was so Helpfull. Very, very good. You are so fucking good man

Ответить
Adesh Tamrakar
Adesh Tamrakar - 04.09.2023 05:56

Hi Dave,
I have one doubt, since here you are using Context to store Auth state, on a page refresh, it will lose its value. And I don't want the user to login everytime page is refreshed. How to handle this ?

Ответить
raphael portela
raphael portela - 30.08.2023 17:50

can i implement this on next ? without using next auth

Ответить
Deadeye
Deadeye - 29.08.2023 06:36

Sorry i'm nore sure about storing access token in memory of client. what if client does browser refresh then i go back to api and ask for new token and also refresh token to be generated on each page hard refresh?

Ответить
Mubashir Hassan
Mubashir Hassan - 20.08.2023 12:22

If we have multiple concurrent requests with an invalid access token, then multiple requests would be sent to refresh token endpoint from interceptor, and we will get different pairs of tokens for each refresh token request.

So considering that we have implemented refresh token rotation with reuse detection, some of the request would still fail because only 1 pair of tokens will be valid from all these concurrent refresh token requests.

How do we handle that @DaveGrayTeachesCode

Ответить
Kirill Zlobin
Kirill Zlobin - 19.08.2023 10:12

Your tutorials contain soooooooooooooooo much valuble information. The other tuts do not talk about AbortController in useEffect, but this is so much useful. I have watched a lot of videos. You are amazing man

Ответить
Nishant Rana
Nishant Rana - 18.08.2023 18:05

i am getting error AXIOS NOT SENDING AUTHORIZATION HEADER - REACTJS-SPRINGBOOT

Ответить
KASHMIR TECH TV
KASHMIR TECH TV - 07.08.2023 16:21

very practical video.good

Ответить
Harun
Harun - 07.08.2023 14:42

Dou you have an url of deployed node app to send requests (if it is deployed)? It would be great to keep up with the video without BE requirements

Ответить
RISHI DUBEY
RISHI DUBEY - 07.08.2023 01:57

your explanati level on is something else !!!

Ответить
Gautam Kumar
Gautam Kumar - 05.08.2023 09:52

All this interconnected tutorials feels like movies franchise :)
love this tutorial btw.

Ответить
Darshit Vaghasiya
Darshit Vaghasiya - 31.07.2023 12:02

@DaveGrayTeachesCode
I'm getting Axios throwing CanceledError with Abort controller in react because of strict mode how can i improve the abort controller with strict mode

Ответить
Code
Code - 30.07.2023 15:42

auth?.roles?.find(role => allowedRoles?.includes(role)) why find is not function?

Ответить
Bryce Taylor
Bryce Taylor - 28.07.2023 02:30

Just to go to a next level. Is there any way to store the refresh and access tokens on a server instead of the client ?

Ответить
Hristo Pisarev
Hristo Pisarev - 25.07.2023 23:18

I have watched many tutorials and paid courses but this is next level - awesome explanation of everything ! Thank you!

Ответить
Bobser
Bobser - 24.07.2023 07:54

How can i conditionally render a component like this but using jwt token instead of sessionStorage:
const isAuthenticated = sessionStorage.getItem("user");

Ответить
Mauro Nunes
Mauro Nunes - 23.07.2023 22:46

Insanity how good this is.

Ответить
David Tinoco
David Tinoco - 21.07.2023 17:24

Wouldn't you want to put the controller outside the useEffect?

Ответить
Ayush Saha
Ayush Saha - 15.07.2023 18:43

Hey Dave. My backend dev provided my the auth endpoints that do not have a refresh route or a /refresh token. Could refer me to some resource that would help me to stroke and use the auth token in a cookie and use it? Or which part of this tutorial should I follow that will work for cases with only access token?

Thank you for your tutorials!

Ответить
Adam Mohammed Muzammil
Adam Mohammed Muzammil - 13.07.2023 13:14

Any ideas on what icon theme he's using for folder's?

Ответить
Karam Isa
Karam Isa - 10.07.2023 03:59

Wow what a great explanation

Ответить
Diyorbek Yuldashov
Diyorbek Yuldashov - 04.07.2023 16:43

What is username and password?
I can not enter website

Ответить
cristian vuolo
cristian vuolo - 29.06.2023 01:05

Hi Dave, I have a question. What would be the best way to provide a private instance of Axios to Redux? Currently, I'm passing it through actions, but it's quite inconvenient to do that repeatedly. Thank you very much!

Ответить
Md Abid Hussain
Md Abid Hussain - 23.06.2023 19:54

Thanks for this awesome tutorial. I have a question
What will happen if i don't use controller.abort() in clean up function. And i was also getting a cancel error name 'CanceledError' i don't know why? maybe because i 'm using vite for react?

Ответить
Imran Ali
Imran Ali - 22.06.2023 16:48

When I refresh browser using ctrl +F5 I am auotamtically logout due to authcontext undefine. auth?.roles?.find(role => allowedRoles?.includes(role)) uath is undefined.Please resolve this issue. Even you can check your code also

Ответить
opos___
opos___ - 18.06.2023 18:13

Hi Dave can you help me with this problem? so, i make my app require to login to access it, all of that thing working fine for the Role Protected Route, Login , etc. The problem is, when i refresh the page, it comeback to login page. can you gave me suggestion from that problem?

Ответить
Rajat Gupta
Rajat Gupta - 17.06.2023 15:37

Great tutorial, got so many doubts cleared. However I have 1 question: Since, we are already sending access token and at server can also access refreshToken from cookie (as withCredentials is true), we can simply verify accessToken at server and if it's expired, we can verify refreshToken and if refresh token is valid we can simply send back new access token. This way we don't have to make 2 (requestInterceptor and responseInterceptor) requests to the server and hence don't require responseInterceptor at all.

Ответить
Murtuza Zabuawala
Murtuza Zabuawala - 13.06.2023 14:32

Hey Dave,
Thanks a lot for your videos.
I have question, If we store JWT token in the memory, How do I make my web app accessible from new browser tab?
What if I want to allow my user to open some component into new browser tab

Ответить
Mz
Mz - 09.06.2023 17:40

thanks broo it works

Ответить