Skip to content

Commit

Permalink
feat: update ls when updating info
Browse files Browse the repository at this point in the history
  • Loading branch information
harshkhandeparkar committed Nov 15, 2023
1 parent ed16dcc commit c68d0d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/pages/MentorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ function MentorForm() {
authContext.onRegister({ ...userData, type: "mentor" });

navigate(authContext.dashboardLink);
} else setInfo("Information successfully changed.");
} else {
authContext.updateUserData(responses.name, responses.email);
setInfo("Information successfully changed.");
}
}

return res.is_ok;
Expand Down
25 changes: 19 additions & 6 deletions src/util/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface IAuthContext {
formLink: ROUTER_PATHS.STUDENT_FORM | ROUTER_PATHS.MENTOR_FORM;
dashboardLink: ROUTER_PATHS.STUDENT_DASHBOARD | ROUTER_PATHS.MENTOR_DASHBOARD;
setUserType: (type: UserType) => void;
updateUserData: (name: string, email: string) => void,
onLogin: (auth: ILocalStorageAuthObj) => void;
onRegister: (auth: IUserAuthData) => void;
onLogout: () => void;
Expand All @@ -55,6 +56,7 @@ const DEFAULT_AUTH_CONTEXT: IAuthContext = {
dashboardLink: ROUTER_PATHS.STUDENT_DASHBOARD,
jwt: DEFAULT_AUTH_OBJ.jwt,
setUserType: () => {},
updateUserData: () => {},
onLogin: () => {},
onRegister: () => {},
onLogout: () => {},
Expand Down Expand Up @@ -124,14 +126,20 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
? ROUTER_PATHS.STUDENT_DASHBOARD
: ROUTER_PATHS.MENTOR_DASHBOARD,
);

localStorage.setItem("auth", JSON.stringify(userAuth));
};

const updateAuth = (auth: ILocalStorageAuthObj) => {
// Set the JWT in the local storage
localStorage.setItem("auth", JSON.stringify(auth));
const updateUserData = (name: string, email: string) => {
setUserAuth({
...userAuth,
userData: {
...userAuth.userData,
name: name,
email: email
}
})
}

const updateAuth = (auth: ILocalStorageAuthObj) => {
setFormLink(
auth.userData.type === "student"
? ROUTER_PATHS.STUDENT_FORM
Expand Down Expand Up @@ -169,6 +177,10 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
setUserAuth(DEFAULT_AUTH_OBJ);
};

useEffect(() => {
localStorage.setItem('auth', JSON.stringify(userAuth));
}, [userAuth])

useEffect(() => {
const auth = getLsAuthObj();

Expand All @@ -191,8 +203,9 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
onLogin,
onRegister,
onLogout,
updateUserData
}),
[isAuthenticated, userAuth, onLogin, onRegister, onLogout],
[isAuthenticated, userAuth, onLogin, onRegister, onLogout, updateUserData],
);

return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
Expand Down

0 comments on commit c68d0d6

Please sign in to comment.