Web Dev Roadmap for Beginners (Free!): https://bit.ly/DaveGrayWebDevRoadmap
Learn JWT Authentication and how to persist login state on refresh in this MERN Stack Project tutorial. You will learn how to use JWT access tokens and refresh tokens in React with RTK Query and Redux as well as persist your app state when you refresh the page.
Note: JWT access tokens are sent in an authorization header, but most viewers search for "JWT Authentication" which explains this title instead of "JWT Authorization".
⭐ Become a full-stack web dev with Zero To Mastery Courses:
- Advanced React & Redux: https://bit.ly/AdvReactDev
- The Complete Node.js Developer: https://bit.ly/CompleteNodeJS
- Jr to Senior Web Dev Roadmap: https://bit.ly/WebDevRoadmap-JrtoSr
Subscribe https://bit.ly/3nGHmNn
Course Updates https://courses.davegray.codes/
Discord https://discord.gg/neKghyefqh
Buy Me A Coffee https://www.buymeacoffee.com/davegray
Follow Me On Social Media:
Github: https://github.com/gitdagray
Twitter: https://twitter.com/yesdavidgray
LinkedIn: https://www.linkedin.com/in/davidagray/
All Resources for this MERN Stack Project: https://github.com/gitdagray/mern_stack_course
Playlist for this MERN Stack Project Series: https://bit.ly/3Sn4EaI
JWT Authentication | Persist Login State on Refresh
(00:00) Intro
(00:18) Welcome
(00:35) Starter Code & User Stories
(01:58) Send access token with a baseQuery
(05:10) Test the baseQuery requests
(07:51) baseQueryWithReauth wrapper function
(13:12) Test the baseQueryWithReauth requests
(15:55) Fix for useQuery when it doesn't unsubscribe
(22:12) usePersist custom hook
(23:36) Add Persist toggle to Login form
(25:24) Quick CSS addition
(25:52) Persist Login component
(36:43) Set credentials with onQueryStarted
(38:09) Add PersistLogin to App.js
(39:04) Test the Persistent Login State
Corrections:
(16:39) Terminology 'labels' vs 'cache entries': When different values are placed here, different caches are created. In the upcoming 'Refactor' chapter 12 video, we insure all queries use the same cache entry value to avoid creating additional caches.
Suggested Pre-requisites for this MERN course:
Node.js for Beginners full course: https://youtu.be/f2EqECiTBL8
React JS for Beginners full course: https://youtu.be/RVFAyFWO4go
Redux Toolkit for Beginners full course: https://youtu.be/NqzdVN2tyvQ
React Login Playlist: https://www.youtube.com/playlist?list=PL0Zuz27SZ-6PRCpm9clX0WiBEMB70FWwd
Tutorial References:
Redux Devtools: https://github.com/reduxjs/redux-devtools
Redux Toolkit Docs: Extending fetchBaseQuery for Authorization: https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#automatic-re-authorization-by-extending-fetchbasequery
Was this JWT Authentication and Persist Login State on refresh tutorial helpful? If so, please share. Let me know your thoughts in the comments.
#jwt #authentication #refresh