Compare commits
2 Commits
7f80f4790d
...
db9132de4d
| Author | SHA1 | Date | |
|---|---|---|---|
| db9132de4d | |||
| ae9bea6c7c |
@@ -8,7 +8,7 @@ import { AppProvider, useAppContext } from "./providers/AuthProvider";
|
||||
const HomePage: FunctionComponent = () => {
|
||||
const { route } = useLocation();
|
||||
const { isLoggedIn } = useAppContext();
|
||||
if (isLoggedIn) route("/profile/tasks", true);
|
||||
if (isLoggedIn.value) route("/profile/tasks", true);
|
||||
else route("/login", true);
|
||||
return <div>Redirecting...</div>;
|
||||
};
|
||||
|
||||
@@ -22,6 +22,7 @@ const LoginPage: FunctionComponent = () => {
|
||||
color="secondary"
|
||||
onClick={() => {
|
||||
isLoggedIn.value = true;
|
||||
localStorage.setItem("loggedIn", "true");
|
||||
route("/profile/tasks", true);
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import Menu from "@/components/menu";
|
||||
import { useAppContext } from "@/providers/AuthProvider";
|
||||
import { FunctionComponent } from "preact";
|
||||
import { lazy, Route, Router, useLocation } from "preact-iso";
|
||||
import ids from "./profile.module.scss";
|
||||
|
||||
const ProfilePage: FunctionComponent = () => {
|
||||
const { route } = useLocation();
|
||||
return (
|
||||
const { isLoggedIn } = useAppContext();
|
||||
if (!isLoggedIn.value) route("/login", true);
|
||||
return isLoggedIn.value ? (
|
||||
<div id={ids.main_container}>
|
||||
<div id={ids.router_container}>
|
||||
<Router>
|
||||
@@ -25,6 +28,8 @@ const ProfilePage: FunctionComponent = () => {
|
||||
<Menu />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<p>Redirecting...</p>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ const ProfileSettings: FunctionComponent = () => {
|
||||
color="secondary"
|
||||
onClick={() => {
|
||||
isLoggedIn.value = false;
|
||||
localStorage.setItem("loggedIn", "false");
|
||||
route("/login", true);
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { stringToBoolean } from "@/utils/converter";
|
||||
import { signal, Signal } from "@preact/signals";
|
||||
import { createContext, JSX } from "preact";
|
||||
import { useContext } from "preact/hooks";
|
||||
@@ -5,14 +6,15 @@ import { useContext } from "preact/hooks";
|
||||
interface AppContextValue {
|
||||
isLoggedIn: Signal<boolean>;
|
||||
}
|
||||
const ininitialValue = stringToBoolean(localStorage.getItem("loggedIn"));
|
||||
|
||||
const AppContext = createContext<AppContextValue>({
|
||||
isLoggedIn: signal(false),
|
||||
isLoggedIn: signal(ininitialValue),
|
||||
});
|
||||
|
||||
const AppProvider = ({ children }: { children: JSX.Element }) => {
|
||||
const value: AppContextValue = {
|
||||
isLoggedIn: signal(false),
|
||||
isLoggedIn: signal(ininitialValue),
|
||||
};
|
||||
|
||||
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
|
||||
|
||||
5
src/utils/converter.ts
Normal file
5
src/utils/converter.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const stringToBoolean = (value: string | null): boolean => {
|
||||
if (value === "true") return true;
|
||||
if (value === "false") return false;
|
||||
return false;
|
||||
};
|
||||
Reference in New Issue
Block a user