Skip to content

Commit

Permalink
logout even on server response failure
Browse files Browse the repository at this point in the history
  • Loading branch information
rafa-lopes-pt committed Jul 8, 2024
1 parent f91e497 commit 27ddc96
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions frontend/src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function App() {
const auth = useContext(AuthCtx);


const AuthenticatedNavItems =
const AuthenticatedNav =
<Navbar
className="main-navbar">
<NavLink to="/">Shop</NavLink>
Expand All @@ -20,7 +20,7 @@ function App() {
<NavLink to="/" indicateRoute={false} action={auth?.logout}>Logout</NavLink>
</Navbar>

const UnauthenticatedNavItems =
const UnauthenticatedNav =
<Navbar
className="main-navbar">
<NavLink to="/">Shop</NavLink>
Expand All @@ -31,8 +31,8 @@ function App() {
<>
<Frame />
<main id="main">
{auth?.isLoggedIn ? AuthenticatedNavItems : UnauthenticatedNavItems}

{auth?.isLoggedIn ? AuthenticatedNav : UnauthenticatedNav}



Expand Down
26 changes: 19 additions & 7 deletions frontend/src/store/auth.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UserSchemaType } from "../../../shared/schemas/user.schema";
import responseHandler from "../apis/responseHandler";
import RestAPI from "../apis/server.endpoints";
import {
notifyToastPromiseEnd,
notifyToastPromiseSuccess
} from "../components/alerts/toasts/promise.notifier";
import { Id } from "react-toastify";
Expand All @@ -14,6 +15,7 @@ export type AuthCtxProperties = {
login: (data: LoginSchemaType) => Promise<boolean>;
updateUser: (data: UpdateProfileSchemaType) => Promise<boolean>;
logout: () => void;
deleteAccount: () => void,
user: UserSchemaType | null;
setUser: (data: UserSchemaType) => void;
};
Expand Down Expand Up @@ -51,7 +53,7 @@ export const AuthCtxProvider = ({ children }: { children?: ReactNode }) => {
}

async function updateUser(body: UpdateProfileSchemaType) {
return responseHandler(() => RestAPI.updateProfile(body), async (res: Response, toastId: Id) => {
return await responseHandler(() => RestAPI.updateProfile(body), async (res: Response, toastId: Id) => {
const data = (await res.json()).data
setUser(data)
window.sessionStorage.setItem(
Expand All @@ -64,20 +66,30 @@ export const AuthCtxProvider = ({ children }: { children?: ReactNode }) => {

}


async function logout() {
await responseHandler(() =>
setUser(null);
setIsLoggedIn(false);
window.sessionStorage.removeItem("session");

return await responseHandler(() =>
RestAPI.logout(), (_res: Response, toastId: Id) => {
setUser(null);
setIsLoggedIn(false);
window.sessionStorage.removeItem("session");
notifyToastPromiseSuccess(toastId, "Logged Out");

})
}

async function deleteAccount() {
return await responseHandler(RestAPI.deleteProfile, (_res: Response, toastId: Id) => {
setUser(null);
setIsLoggedIn(false);
window.sessionStorage.removeItem("session");
notifyToastPromiseEnd(toastId);
}
)
}

return (
<AuthCtx.Provider value={{ isLoggedIn, login, updateUser, logout, user, setUser }}>
<AuthCtx.Provider value={{ isLoggedIn, login, updateUser, logout, deleteAccount, user, setUser }}>
{children}
</AuthCtx.Provider>
);
Expand Down

0 comments on commit 27ddc96

Please sign in to comment.