Authstate, Login, Logout. Store, dispatch etc
This commit is contained in:
@@ -15,13 +15,12 @@ interface InitState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const initialState: InitState = {
|
const initialState: InitState = {
|
||||||
isLoading: false,
|
isLoading: true,
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
currentUser: null,
|
currentUser: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const AuthProvider = (props: any) => {
|
const AuthProvider = (props: any) => {
|
||||||
|
|
||||||
const [store, setStore] = createStore(initialState);
|
const [store, setStore] = createStore(initialState);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
@@ -35,16 +34,17 @@ const AuthProvider = (props: any) => {
|
|||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
await loadCurrentUser();
|
await loadCurrentUser();
|
||||||
|
setStore("isLoading", false);
|
||||||
});
|
});
|
||||||
|
|
||||||
const setCurrentUser = (user: UserType) => {
|
const setCurrentUser = (user: UserType) => {
|
||||||
setStore("isAuthenticated", true);
|
setStore("isAuthenticated", true);
|
||||||
setStore("currentUser", user);
|
setStore("currentUser", user);
|
||||||
setStore("isLoading", false);
|
|
||||||
};
|
};
|
||||||
const removeCurrentUser = () => {
|
const removeCurrentUser = () => {
|
||||||
setStore("isAuthenticated", false);
|
setStore("isAuthenticated", false);
|
||||||
setStore("currentUser", null);
|
setStore("currentUser", null);
|
||||||
|
localStorage.removeItem("current_user");
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { createStore } from "solid-js/store";
|
|||||||
import EmailPassRecipe from "supertokens-web-js/recipe/emailpassword";
|
import EmailPassRecipe from "supertokens-web-js/recipe/emailpassword";
|
||||||
import Session from "supertokens-web-js/recipe/session";
|
import Session from "supertokens-web-js/recipe/session";
|
||||||
import { useAuthDispatch } from "../../context/AuthContext";
|
import { useAuthDispatch } from "../../context/AuthContext";
|
||||||
import { loginService } from "../../services/auth.service";
|
import { loginService, logoutService } 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 +13,7 @@ const useLogin = () => {
|
|||||||
password: "",
|
password: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
const { setCurrentUser } = useAuthDispatch();
|
const { setCurrentUser, removeCurrentUser } = useAuthDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleInput = (ev: any) => {
|
const handleInput = (ev: any) => {
|
||||||
@@ -66,7 +66,15 @@ const useLogin = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleLogout = async () => {
|
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 () => {
|
const handleJwt = async () => {
|
||||||
|
|||||||
@@ -1,45 +1,52 @@
|
|||||||
|
import { useNavigate } from "@solidjs/router";
|
||||||
import { Component, Show } from "solid-js";
|
import { Component, Show } from "solid-js";
|
||||||
|
import { useAuthState } from "../../../context/AuthContext";
|
||||||
import useLogin from "../../../hooks/auth/login.hook";
|
import useLogin from "../../../hooks/auth/login.hook";
|
||||||
const { handleLogin, handleLogout, handleInput, handleJwt, loading, form } = useLogin();
|
|
||||||
const Login: Component = () => {
|
const Login: Component = () => {
|
||||||
return (
|
const navigate = useNavigate();
|
||||||
<div>
|
const { handleLogin, handleInput, loading, form } = useLogin();
|
||||||
<form onsubmit={handleLogin}>
|
const authState: any = useAuthState();
|
||||||
<div>
|
if (authState.isAuthenticated) {
|
||||||
{/* <label for="name">Username or Email</label> */}
|
navigate("/");
|
||||||
<input
|
} else {
|
||||||
type="text"
|
return (
|
||||||
name="email"
|
<div>
|
||||||
placeholder="Email/Username"
|
<form onsubmit={handleLogin}>
|
||||||
value={form.email}
|
<div>
|
||||||
onInput={handleInput}
|
<label for="email">Username or Email</label>
|
||||||
/>
|
<br />
|
||||||
</div>
|
<input
|
||||||
<div>
|
id="email"
|
||||||
{/* <label for="name">Password</label> */}
|
type="email"
|
||||||
<input
|
name="email"
|
||||||
type="password"
|
placeholder="Email/Username"
|
||||||
name="password"
|
value={form.email}
|
||||||
placeholder="Password"
|
onInput={handleInput}
|
||||||
value={form.password}
|
required
|
||||||
onInput={handleInput}
|
/>
|
||||||
/>
|
</div>
|
||||||
</div>
|
<div>
|
||||||
|
<label for="password">Password</label>
|
||||||
<button disabled={loading()} type="submit">
|
<br />
|
||||||
<Show when={!loading()} fallback="Logging in...">
|
<input
|
||||||
Log In
|
id="password"
|
||||||
</Show>
|
type="password"
|
||||||
</button>
|
name="password"
|
||||||
</form>
|
placeholder="Password"
|
||||||
<button type="button" onclick={handleLogout}>
|
value={form.password}
|
||||||
Logout
|
onInput={handleInput}
|
||||||
</button>
|
required
|
||||||
<button type="button" onclick={handleJwt}>
|
/>
|
||||||
GetJWT
|
</div>
|
||||||
</button>
|
<br />
|
||||||
</div>
|
<button disabled={loading()} type="submit">
|
||||||
);
|
<Show when={!loading()} fallback="Logging in...">
|
||||||
|
Log In
|
||||||
|
</Show>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Login;
|
export default Login;
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
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";
|
||||||
|
|
||||||
const Home: Component = () => {
|
const Home: Component = () => {
|
||||||
const authState: any = useAuthState();
|
const authState: any = useAuthState();
|
||||||
|
const { handleLogout } = useLogin();
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<p>Home</p>
|
<p>Home</p>
|
||||||
|
|
||||||
<Show when={authState?.isAuthenticated}>
|
<Show when={authState?.isAuthenticated}>
|
||||||
<div>
|
<div>
|
||||||
<p>{JSON.parse(authState?.currentUser)}</p>
|
<p>Authenticated</p>
|
||||||
|
<button onclick={handleLogout}>Logout</button>
|
||||||
|
<p>{JSON.stringify(authState)}</p>
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
|
<Show when={!authState?.isAuthenticated}>
|
||||||
|
<a href="/auth/login">Login</a>
|
||||||
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ 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(localStorage.getItem("current_user")!);
|
const user: EmailPassRecipe.UserType = JSON.parse(
|
||||||
|
localStorage.getItem("current_user")!
|
||||||
|
);
|
||||||
if (sessionUserId === user.id) {
|
if (sessionUserId === user.id) {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
@@ -29,4 +31,8 @@ const loginService = async (loginData: AuthData) => {
|
|||||||
return await EmailPassRecipe.signIn(loginData);
|
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