diff --git a/frontend/src/context/AuthContext.tsx b/frontend/src/context/AuthContext.tsx index 7789666..ad6d1a0 100644 --- a/frontend/src/context/AuthContext.tsx +++ b/frontend/src/context/AuthContext.tsx @@ -15,13 +15,12 @@ interface InitState { } const initialState: InitState = { - isLoading: false, + isLoading: true, isAuthenticated: false, currentUser: null, }; const AuthProvider = (props: any) => { - const [store, setStore] = createStore(initialState); const navigate = useNavigate(); @@ -35,16 +34,17 @@ const AuthProvider = (props: any) => { onMount(async () => { await loadCurrentUser(); + setStore("isLoading", false); }); const setCurrentUser = (user: UserType) => { setStore("isAuthenticated", true); setStore("currentUser", user); - setStore("isLoading", false); }; const removeCurrentUser = () => { setStore("isAuthenticated", false); setStore("currentUser", null); + localStorage.removeItem("current_user"); }; return ( diff --git a/frontend/src/hooks/auth/login.hook.ts b/frontend/src/hooks/auth/login.hook.ts index 78f8a4c..104651a 100644 --- a/frontend/src/hooks/auth/login.hook.ts +++ b/frontend/src/hooks/auth/login.hook.ts @@ -5,7 +5,7 @@ import { createStore } from "solid-js/store"; import EmailPassRecipe from "supertokens-web-js/recipe/emailpassword"; import Session from "supertokens-web-js/recipe/session"; import { useAuthDispatch } from "../../context/AuthContext"; -import { loginService } from "../../services/auth.service"; +import { loginService, logoutService } from "../../services/auth.service"; const useLogin = () => { const [loading, setLoading] = createSignal(false); const [form, setForm] = createStore({ @@ -13,7 +13,7 @@ const useLogin = () => { password: "", }); - const { setCurrentUser } = useAuthDispatch(); + const { setCurrentUser, removeCurrentUser } = useAuthDispatch(); const navigate = useNavigate(); const handleInput = (ev: any) => { @@ -66,7 +66,15 @@ const useLogin = () => { }; const handleLogout = async () => { - const logout = await EmailPassRecipe.signOut(); + setLoading(true); + setLoading(false); + try { + await logoutService(); + } catch (error) { + console.log(error); + } finally { + removeCurrentUser(); + } }; const handleJwt = async () => { diff --git a/frontend/src/routes/views/auth/Login.tsx b/frontend/src/routes/views/auth/Login.tsx index fa9225d..a5775d2 100644 --- a/frontend/src/routes/views/auth/Login.tsx +++ b/frontend/src/routes/views/auth/Login.tsx @@ -1,45 +1,52 @@ +import { useNavigate } from "@solidjs/router"; import { Component, Show } from "solid-js"; +import { useAuthState } from "../../../context/AuthContext"; import useLogin from "../../../hooks/auth/login.hook"; -const { handleLogin, handleLogout, handleInput, handleJwt, loading, form } = useLogin(); const Login: Component = () => { - return ( -
Home
{JSON.parse(authState?.currentUser)}
+Authenticated
+ +{JSON.stringify(authState)}