Compare commits

..

2 Commits

Author SHA1 Message Date
db9132de4d feat: pseudo-auth 2025-04-14 21:10:04 +03:00
ae9bea6c7c feat: pseudo-login 2025-04-14 21:01:33 +03:00
6 changed files with 18 additions and 4 deletions

View File

@@ -8,7 +8,7 @@ import { AppProvider, useAppContext } from "./providers/AuthProvider";
const HomePage: FunctionComponent = () => { const HomePage: FunctionComponent = () => {
const { route } = useLocation(); const { route } = useLocation();
const { isLoggedIn } = useAppContext(); const { isLoggedIn } = useAppContext();
if (isLoggedIn) route("/profile/tasks", true); if (isLoggedIn.value) route("/profile/tasks", true);
else route("/login", true); else route("/login", true);
return <div>Redirecting...</div>; return <div>Redirecting...</div>;
}; };

View File

@@ -22,6 +22,7 @@ const LoginPage: FunctionComponent = () => {
color="secondary" color="secondary"
onClick={() => { onClick={() => {
isLoggedIn.value = true; isLoggedIn.value = true;
localStorage.setItem("loggedIn", "true");
route("/profile/tasks", true); route("/profile/tasks", true);
}} }}
> >

View File

@@ -1,11 +1,14 @@
import Menu from "@/components/menu"; import Menu from "@/components/menu";
import { useAppContext } from "@/providers/AuthProvider";
import { FunctionComponent } from "preact"; import { FunctionComponent } from "preact";
import { lazy, Route, Router, useLocation } from "preact-iso"; import { lazy, Route, Router, useLocation } from "preact-iso";
import ids from "./profile.module.scss"; import ids from "./profile.module.scss";
const ProfilePage: FunctionComponent = () => { const ProfilePage: FunctionComponent = () => {
const { route } = useLocation(); const { route } = useLocation();
return ( const { isLoggedIn } = useAppContext();
if (!isLoggedIn.value) route("/login", true);
return isLoggedIn.value ? (
<div id={ids.main_container}> <div id={ids.main_container}>
<div id={ids.router_container}> <div id={ids.router_container}>
<Router> <Router>
@@ -25,6 +28,8 @@ const ProfilePage: FunctionComponent = () => {
<Menu /> <Menu />
</div> </div>
</div> </div>
) : (
<p>Redirecting...</p>
); );
}; };

View File

@@ -29,6 +29,7 @@ const ProfileSettings: FunctionComponent = () => {
color="secondary" color="secondary"
onClick={() => { onClick={() => {
isLoggedIn.value = false; isLoggedIn.value = false;
localStorage.setItem("loggedIn", "false");
route("/login", true); route("/login", true);
}} }}
> >

View File

@@ -1,3 +1,4 @@
import { stringToBoolean } from "@/utils/converter";
import { signal, Signal } from "@preact/signals"; import { signal, Signal } from "@preact/signals";
import { createContext, JSX } from "preact"; import { createContext, JSX } from "preact";
import { useContext } from "preact/hooks"; import { useContext } from "preact/hooks";
@@ -5,14 +6,15 @@ import { useContext } from "preact/hooks";
interface AppContextValue { interface AppContextValue {
isLoggedIn: Signal<boolean>; isLoggedIn: Signal<boolean>;
} }
const ininitialValue = stringToBoolean(localStorage.getItem("loggedIn"));
const AppContext = createContext<AppContextValue>({ const AppContext = createContext<AppContextValue>({
isLoggedIn: signal(false), isLoggedIn: signal(ininitialValue),
}); });
const AppProvider = ({ children }: { children: JSX.Element }) => { const AppProvider = ({ children }: { children: JSX.Element }) => {
const value: AppContextValue = { const value: AppContextValue = {
isLoggedIn: signal(false), isLoggedIn: signal(ininitialValue),
}; };
return <AppContext.Provider value={value}>{children}</AppContext.Provider>; return <AppContext.Provider value={value}>{children}</AppContext.Provider>;

5
src/utils/converter.ts Normal file
View File

@@ -0,0 +1,5 @@
export const stringToBoolean = (value: string | null): boolean => {
if (value === "true") return true;
if (value === "false") return false;
return false;
};