Some cleanups, added logout hook
This commit is contained in:
@@ -4,6 +4,7 @@ import AppRouter from "./routes";
|
|||||||
import SuperTokens from "supertokens-web-js";
|
import SuperTokens from "supertokens-web-js";
|
||||||
import EmailPass from "supertokens-web-js/recipe/emailpassword";
|
import EmailPass from "supertokens-web-js/recipe/emailpassword";
|
||||||
import Session from "supertokens-web-js/recipe/session";
|
import Session from "supertokens-web-js/recipe/session";
|
||||||
|
import AuthProvider from "./context/AuthContext";
|
||||||
|
|
||||||
SuperTokens.init({
|
SuperTokens.init({
|
||||||
appInfo: {
|
appInfo: {
|
||||||
@@ -11,14 +12,15 @@ SuperTokens.init({
|
|||||||
apiBasePath: "/auth/api",
|
apiBasePath: "/auth/api",
|
||||||
appName: "Fluxem",
|
appName: "Fluxem",
|
||||||
},
|
},
|
||||||
recipeList: [
|
recipeList: [EmailPass.init(), Session.init()],
|
||||||
EmailPass.init(),
|
|
||||||
Session.init(),
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const App: Component = () => {
|
const App: Component = () => {
|
||||||
return <AppRouter />;
|
return (
|
||||||
|
<AuthProvider>
|
||||||
|
<AppRouter />
|
||||||
|
</AuthProvider>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useLocation, useNavigate } from "@solidjs/router";
|
import { useNavigate } from "@solidjs/router";
|
||||||
import { createContext, onMount, Show, useContext } from "solid-js";
|
import { createContext, onMount, Show, useContext } from "solid-js";
|
||||||
import { createStore } from "solid-js/store";
|
import { createStore } from "solid-js/store";
|
||||||
import { UserType } from "supertokens-web-js/recipe/emailpassword";
|
import { UserType } from "supertokens-web-js/recipe/emailpassword";
|
||||||
@@ -37,14 +37,15 @@ const AuthProvider = (props: any) => {
|
|||||||
setStore("isLoading", false);
|
setStore("isLoading", false);
|
||||||
});
|
});
|
||||||
|
|
||||||
const setCurrentUser = (user: UserType) => {
|
const setCurrentUser = (user?: UserType) => {
|
||||||
|
if (user) {
|
||||||
setStore("isAuthenticated", true);
|
setStore("isAuthenticated", true);
|
||||||
setStore("currentUser", user);
|
setStore("currentUser", user);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const removeCurrentUser = () => {
|
const removeCurrentUser = () => {
|
||||||
setStore("isAuthenticated", false);
|
setStore("isAuthenticated", false);
|
||||||
setStore("currentUser", null);
|
setStore("currentUser", null);
|
||||||
localStorage.removeItem("current_user");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ import { useNavigate } from "@solidjs/router";
|
|||||||
import { createSignal } from "solid-js";
|
import { createSignal } from "solid-js";
|
||||||
import { createStore } from "solid-js/store";
|
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 { useAuthDispatch } from "../../context/AuthContext";
|
||||||
import { loginService, logoutService } from "../../services/auth.service";
|
import {
|
||||||
|
delUserFromLocalStorage,
|
||||||
|
loginService,
|
||||||
|
logoutService,
|
||||||
|
setUserInLocalStorage,
|
||||||
|
} from "../../services/auth.service";
|
||||||
|
|
||||||
const useLogin = () => {
|
const useLogin = () => {
|
||||||
const [loading, setLoading] = createSignal(false);
|
const [loading, setLoading] = createSignal(false);
|
||||||
const [form, setForm] = createStore({
|
const [form, setForm] = createStore({
|
||||||
@@ -13,7 +17,8 @@ const useLogin = () => {
|
|||||||
password: "",
|
password: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
const { setCurrentUser, removeCurrentUser } = useAuthDispatch();
|
const { setCurrentUser } = useAuthDispatch();
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleInput = (ev: any) => {
|
const handleInput = (ev: any) => {
|
||||||
@@ -30,9 +35,8 @@ const useLogin = () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
if (loginData.status === "OK") {
|
if (loginData.status === "OK") {
|
||||||
//TODO: Add user in store. Set Authenticated true.
|
|
||||||
localStorage.setItem("current_user", JSON.stringify(loginData.user));
|
|
||||||
setCurrentUser(loginData.user);
|
setCurrentUser(loginData.user);
|
||||||
|
setUserInLocalStorage(loginData.user);
|
||||||
navigate("/", { replace: true });
|
navigate("/", { replace: true });
|
||||||
}
|
}
|
||||||
if (loginData.status === "FIELD_ERROR") {
|
if (loginData.status === "FIELD_ERROR") {
|
||||||
@@ -49,50 +53,9 @@ const useLogin = () => {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// try {
|
|
||||||
// const login = await EmailPassRecipe.signIn({
|
|
||||||
// formFields: [
|
|
||||||
// { id: "email", value: form.email },
|
|
||||||
// { id: "password", value: form.password },
|
|
||||||
// ],
|
|
||||||
// });
|
|
||||||
// console.log(login)
|
|
||||||
// } catch (error) {
|
|
||||||
// console.error(error);
|
|
||||||
// } finally {
|
|
||||||
// setLoading(false);
|
|
||||||
// }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLogout = async () => {
|
return { handleInput, loading, handleLogin, form };
|
||||||
setLoading(true);
|
|
||||||
setLoading(false);
|
|
||||||
try {
|
|
||||||
await logoutService();
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
} finally {
|
|
||||||
removeCurrentUser();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleJwt = async () => {
|
|
||||||
const session = await Session.doesSessionExist();
|
|
||||||
if (session) {
|
|
||||||
let userId = await Session.getUserId();
|
|
||||||
let jwt = (await Session.getAccessTokenPayloadSecurely()).jwt;
|
|
||||||
// console.log(userId, jwt);
|
|
||||||
const { isVerified } = await EmailPassRecipe.isEmailVerified();
|
|
||||||
console.log(isVerified);
|
|
||||||
if (!isVerified) {
|
|
||||||
const sendEmail = await EmailPassRecipe.sendVerificationEmail();
|
|
||||||
console.log(sendEmail);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return { handleInput, loading, handleLogin, handleLogout, handleJwt, form };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useLogin;
|
export default useLogin;
|
||||||
|
|||||||
28
frontend/src/hooks/auth/logout.hook.ts
Normal file
28
frontend/src/hooks/auth/logout.hook.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { createSignal } from "solid-js";
|
||||||
|
import { useAuthDispatch } from "../../context/AuthContext";
|
||||||
|
import {
|
||||||
|
delUserFromLocalStorage,
|
||||||
|
logoutService,
|
||||||
|
} from "../../services/auth.service";
|
||||||
|
|
||||||
|
const useLogout = () => {
|
||||||
|
const [loading, setLoading] = createSignal(false);
|
||||||
|
const { removeCurrentUser } = useAuthDispatch();
|
||||||
|
|
||||||
|
const handleLogout = async () => {
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
await logoutService();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
} finally {
|
||||||
|
removeCurrentUser();
|
||||||
|
delUserFromLocalStorage();
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return { handleLogout, loading };
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useLogout;
|
||||||
@@ -4,14 +4,10 @@ import { render } from "solid-js/web";
|
|||||||
import "./index.css";
|
import "./index.css";
|
||||||
import App from "./App";
|
import App from "./App";
|
||||||
import { Router } from "@solidjs/router";
|
import { Router } from "@solidjs/router";
|
||||||
import AuthProvider from "./context/AuthContext";
|
|
||||||
|
|
||||||
render(
|
render(
|
||||||
() => (
|
() => (
|
||||||
<Router>
|
<Router>
|
||||||
<AuthProvider>
|
|
||||||
<App />
|
<App />
|
||||||
</AuthProvider>
|
|
||||||
</Router>
|
</Router>
|
||||||
),
|
),
|
||||||
document.getElementById("main-container") as HTMLElement
|
document.getElementById("main-container") as HTMLElement
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { Component, Show } from "solid-js";
|
import { Component, Show } from "solid-js";
|
||||||
import { useAuthState } from "../../../context/AuthContext";
|
import { useAuthState } from "../../../context/AuthContext";
|
||||||
import useLogin from "../../../hooks/auth/login.hook";
|
import useLogin from "../../../hooks/auth/login.hook";
|
||||||
|
import useLogout from "../../../hooks/auth/logout.hook";
|
||||||
|
|
||||||
const Home: Component = () => {
|
const Home: Component = () => {
|
||||||
const authState: any = useAuthState();
|
const authState: any = useAuthState();
|
||||||
const { handleLogout } = useLogin();
|
const { handleLogout, loading } = useLogout();
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<p>Home</p>
|
<p>Home</p>
|
||||||
@@ -12,7 +13,11 @@ const Home: Component = () => {
|
|||||||
<Show when={authState?.isAuthenticated}>
|
<Show when={authState?.isAuthenticated}>
|
||||||
<div>
|
<div>
|
||||||
<p>Authenticated</p>
|
<p>Authenticated</p>
|
||||||
<button onclick={handleLogout}>Logout</button>
|
<button onclick={handleLogout} disabled={loading()}>
|
||||||
|
<Show when={!loading()} fallback="Signing out...">
|
||||||
|
Sign Out
|
||||||
|
</Show>
|
||||||
|
</button>
|
||||||
<p>{JSON.stringify(authState)}</p>
|
<p>{JSON.stringify(authState)}</p>
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import EmailPassRecipe from "supertokens-web-js/recipe/emailpassword";
|
import EmailPassRecipe, {
|
||||||
|
UserType,
|
||||||
|
} from "supertokens-web-js/recipe/emailpassword";
|
||||||
import Session from "supertokens-web-js/recipe/session";
|
import Session from "supertokens-web-js/recipe/session";
|
||||||
|
|
||||||
|
const curentUserKey = "current_user";
|
||||||
|
|
||||||
export interface AuthData {
|
export interface AuthData {
|
||||||
formFields: FormFields[];
|
formFields: FormFields[];
|
||||||
}
|
}
|
||||||
@@ -17,13 +21,13 @@ const currentUser = async () => {
|
|||||||
(await Session.doesSessionExist())
|
(await Session.doesSessionExist())
|
||||||
) {
|
) {
|
||||||
let sessionUserId = await Session.getUserId();
|
let sessionUserId = await Session.getUserId();
|
||||||
const user: EmailPassRecipe.UserType = JSON.parse(
|
const user = getUserFromLocaStorage();
|
||||||
localStorage.getItem("current_user")!
|
if (user) {
|
||||||
);
|
|
||||||
if (sessionUserId === user.id) {
|
if (sessionUserId === user.id) {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,4 +39,23 @@ const logoutService = async () => {
|
|||||||
await EmailPassRecipe.signOut();
|
await EmailPassRecipe.signOut();
|
||||||
};
|
};
|
||||||
|
|
||||||
export { loginService, logoutService, currentUser };
|
const setUserInLocalStorage = (user: UserType) => {
|
||||||
|
localStorage.setItem(curentUserKey, JSON.stringify(user));
|
||||||
|
};
|
||||||
|
|
||||||
|
const getUserFromLocaStorage = () => {
|
||||||
|
const user: UserType = JSON.parse(localStorage.getItem(curentUserKey)!);
|
||||||
|
return user;
|
||||||
|
};
|
||||||
|
|
||||||
|
const delUserFromLocalStorage = () => {
|
||||||
|
localStorage.removeItem(curentUserKey);
|
||||||
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
loginService,
|
||||||
|
logoutService,
|
||||||
|
currentUser,
|
||||||
|
setUserInLocalStorage,
|
||||||
|
delUserFromLocalStorage,
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user