You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lesson Redirect to Dashboard if logged in, timeline 02:00
Oh my gooooood. The more I watch the less I like.... Mama Samba is soooooooo impatient and unreasonable. I have just implemented the feature of fetching a user by id. Instead of using the null in the initial state in AuthContext.jsx - I used a blank object as a kind of a guest with ROLE_GUEST. I decided to go this way so that Sidebar.jsx is loaded properly.
AuthContext.jsx
import{createContext,useContext,useEffect,useState}from"react";import{executeLogin,getCustomerById}from"../../services/client.js";importjwtDecodefrom"jwt-decode";import{redirect}from"react-router-dom";constAuthContext=createContext({});constAuthProvider=({ children })=>{letdefaultCustomer={name: "Viewer",email: "",age: undefined,gender: "",roles: ["ROLE_GUEST"]};const[customer,setCustomer]=useState(defaultCustomer);useEffect(()=>{getCustomer().then(data=>setCustomer({ ...data}))},[]);constgetCustomer=async()=>{consttoken=localStorage.getItem("access_token");constid=localStorage.getItem("customer_id");if(!token||!id){returnlogout();}const{ data }=awaitgetCustomerById(id)constdecodedJwt=jwtDecode(token);if(!data.email===decodedJwt.sub){logout();}returndata;}constlogin=asyncsubmitEmailPassword=>newPromise((resolve,reject)=>executeLogin(submitEmailPassword).then(res=>{constjwt=res.headers["authorization"];localStorage.setItem("access_token",jwt);localStorage.setItem("customer_id",res.data.customerDto.id);getCustomer().then(fetched=>{setCustomer({ ...fetched})resolve(res);});}).catch(err=>{reject(err);}));constlogout=()=>{localStorage.removeItem("access_token");localStorage.removeItem("customer_id");setCustomer(defaultCustomer);redirect("/")};constisCustomerAuthenticated=()=>{consttoken=localStorage.getItem("access_token");constid=localStorage.getItem("customer_id");if(!token||!id){logout();returnfalse}const{ exp }=jwtDecode(token);if(Date.now()>exp*1000){logout();returnfalse;}returntrue;};return(<AuthContext.Providervalue={{
customer,
login,
logout,
isCustomerAuthenticated
}}>{children}</AuthContext.Provider>);};exportconstuseAuth=()=>useContext(AuthContext);exportdefaultAuthProvider;
Each request I take the user's id from the localStorage, fetch the user from the database by the id and place the user into the state. Before that I retrieve the user's email from JWT and compare it with the customer's email - if they are the same - ok. If any of localStorage values have been removed from the client's side (jwt or user id) I redirect the user to the login page and delete any data in their localStorage.
What he does in the Login component is he checks if the user is null (state). The difference is he doesn't fetch the user from the database (there is no async request) and it seems to have worked perfectly well. In my case, by the time it checks the user it is yet GIMMICK (a guest! or it would've been null if I have done the same as he does in the video). Anyway, what we actually need to check is not the user's state, but if the user is authenticated:
Lesson Redirect to Dashboard if logged in, timeline 02:00
Oh my gooooood. The more I watch the less I like.... Mama Samba is soooooooo impatient and unreasonable. I have just implemented the feature of fetching a user by id. Instead of using the null in the initial state in AuthContext.jsx - I used a blank object as a kind of a guest with ROLE_GUEST. I decided to go this way so that Sidebar.jsx is loaded properly.
AuthContext.jsx
Each request I take the user's id from the localStorage, fetch the user from the database by the id and place the user into the state. Before that I retrieve the user's email from JWT and compare it with the customer's email - if they are the same - ok. If any of localStorage values have been removed from the client's side (jwt or user id) I redirect the user to the login page and delete any data in their localStorage.
What he does in the Login component is he checks if the user is null (state). The difference is he doesn't fetch the user from the database (there is no async request) and it seems to have worked perfectly well. In my case, by the time it checks the user it is yet GIMMICK (a guest! or it would've been null if I have done the same as he does in the video). Anyway, what we actually need to check is not the user's state, but if the user is authenticated:
Login.jsx
or, an alternative way....:
if you're going to implement the same, there are extra two pieces of code:
One is used when a user logs in to their account, another one is when a user signs up.
The text was updated successfully, but these errors were encountered: