Authstate, Login, Logout. Store, dispatch etc
This commit is contained in:
@@ -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 (
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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 (
|
||||
<div>
|
||||
<form onsubmit={handleLogin}>
|
||||
<div>
|
||||
{/* <label for="name">Username or Email</label> */}
|
||||
<input
|
||||
type="text"
|
||||
name="email"
|
||||
placeholder="Email/Username"
|
||||
value={form.email}
|
||||
onInput={handleInput}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
{/* <label for="name">Password</label> */}
|
||||
<input
|
||||
type="password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
value={form.password}
|
||||
onInput={handleInput}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button disabled={loading()} type="submit">
|
||||
<Show when={!loading()} fallback="Logging in...">
|
||||
Log In
|
||||
</Show>
|
||||
</button>
|
||||
</form>
|
||||
<button type="button" onclick={handleLogout}>
|
||||
Logout
|
||||
</button>
|
||||
<button type="button" onclick={handleJwt}>
|
||||
GetJWT
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
const navigate = useNavigate();
|
||||
const { handleLogin, handleInput, loading, form } = useLogin();
|
||||
const authState: any = useAuthState();
|
||||
if (authState.isAuthenticated) {
|
||||
navigate("/");
|
||||
} else {
|
||||
return (
|
||||
<div>
|
||||
<form onsubmit={handleLogin}>
|
||||
<div>
|
||||
<label for="email">Username or Email</label>
|
||||
<br />
|
||||
<input
|
||||
id="email"
|
||||
type="email"
|
||||
name="email"
|
||||
placeholder="Email/Username"
|
||||
value={form.email}
|
||||
onInput={handleInput}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label for="password">Password</label>
|
||||
<br />
|
||||
<input
|
||||
id="password"
|
||||
type="password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
value={form.password}
|
||||
onInput={handleInput}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<br />
|
||||
<button disabled={loading()} type="submit">
|
||||
<Show when={!loading()} fallback="Logging in...">
|
||||
Log In
|
||||
</Show>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default Login;
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
import { Component, Show } from "solid-js";
|
||||
import { useAuthState } from "../../../context/AuthContext";
|
||||
import useLogin from "../../../hooks/auth/login.hook";
|
||||
|
||||
const Home: Component = () => {
|
||||
const authState: any = useAuthState();
|
||||
|
||||
const { handleLogout } = useLogin();
|
||||
return (
|
||||
<div>
|
||||
<p>Home</p>
|
||||
|
||||
<Show when={authState?.isAuthenticated}>
|
||||
<div>
|
||||
<p>{JSON.parse(authState?.currentUser)}</p>
|
||||
<p>Authenticated</p>
|
||||
<button onclick={handleLogout}>Logout</button>
|
||||
<p>{JSON.stringify(authState)}</p>
|
||||
</div>
|
||||
</Show>
|
||||
<Show when={!authState?.isAuthenticated}>
|
||||
<a href="/auth/login">Login</a>
|
||||
</Show>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -17,7 +17,9 @@ const currentUser = async () => {
|
||||
(await Session.doesSessionExist())
|
||||
) {
|
||||
let sessionUserId = await Session.getUserId();
|
||||
const user: EmailPassRecipe.UserType = JSON.parse(localStorage.getItem("current_user")!);
|
||||
const user: EmailPassRecipe.UserType = JSON.parse(
|
||||
localStorage.getItem("current_user")!
|
||||
);
|
||||
if (sessionUserId === user.id) {
|
||||
return user;
|
||||
}
|
||||
@@ -29,4 +31,8 @@ const loginService = async (loginData: AuthData) => {
|
||||
return await EmailPassRecipe.signIn(loginData);
|
||||
};
|
||||
|
||||
export { loginService, currentUser };
|
||||
const logoutService = async () => {
|
||||
await EmailPassRecipe.signOut();
|
||||
};
|
||||
|
||||
export { loginService, logoutService, currentUser };
|
||||
|
||||
Reference in New Issue
Block a user