refactor(lst): refactor to monolithic completed
This commit is contained in:
12
frontend/src/lib/hooks/useLogout.ts
Normal file
12
frontend/src/lib/hooks/useLogout.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {useSessionStore} from "../store/sessionStore";
|
||||
|
||||
export const useLogout = () => {
|
||||
const clearSession = useSessionStore((state) => state.clearSession);
|
||||
|
||||
const logout = () => {
|
||||
clearSession(); // Clears Zustand state
|
||||
return "Logged out";
|
||||
};
|
||||
|
||||
return logout;
|
||||
};
|
||||
49
frontend/src/lib/hooks/useSession.ts
Normal file
49
frontend/src/lib/hooks/useSession.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import {useQuery} from "@tanstack/react-query";
|
||||
import {useSessionStore} from "../store/sessionStore";
|
||||
import {useEffect} from "react";
|
||||
|
||||
const fetchSession = async () => {
|
||||
const token = localStorage.getItem("auth_token");
|
||||
|
||||
if (!token) {
|
||||
throw new Error("No token found");
|
||||
}
|
||||
|
||||
const res = await fetch("/api/auth/session", {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
console.log(res);
|
||||
if (!res.ok) {
|
||||
throw new Error("Session not found");
|
||||
}
|
||||
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export const useSession = () => {
|
||||
const {setSession, clearSession, token} = useSessionStore();
|
||||
|
||||
// Fetch session only if token is available
|
||||
const {data, status, error} = useQuery({
|
||||
queryKey: ["session"],
|
||||
queryFn: fetchSession,
|
||||
enabled: !!token, // Prevents query if token is null
|
||||
staleTime: 5 * 60 * 1000, // 5 mins
|
||||
gcTime: 10 * 60 * 1000, // 10 mins
|
||||
refetchOnWindowFocus: true,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setSession(data.token);
|
||||
}
|
||||
if (error) {
|
||||
clearSession();
|
||||
}
|
||||
}, [data, error]);
|
||||
|
||||
return {session: data && token ? {user: data.user, token: data.token} : null, status, error};
|
||||
};
|
||||
36
frontend/src/lib/store/sessionStore.ts
Normal file
36
frontend/src/lib/store/sessionStore.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import {create} from "zustand";
|
||||
|
||||
// type User = {
|
||||
// id: number;
|
||||
// username: string;
|
||||
// };
|
||||
|
||||
type SessionState = {
|
||||
//user: User | null;
|
||||
token: string | null;
|
||||
setSession: (token: string) => void;
|
||||
clearSession: () => void;
|
||||
};
|
||||
|
||||
export const useSessionStore = create<SessionState>((set) => {
|
||||
// Initialize from localStorage
|
||||
//const storedUser = localStorage.getItem("user");
|
||||
const storedToken = localStorage.getItem("auth_token");
|
||||
|
||||
return {
|
||||
//user: storedUser ? JSON.parse(storedUser) : null,
|
||||
token: storedToken || null,
|
||||
|
||||
setSession: (token) => {
|
||||
localStorage.setItem("auth_token", token);
|
||||
//localStorage.setItem("user", JSON.stringify(user));
|
||||
set({token});
|
||||
},
|
||||
|
||||
clearSession: () => {
|
||||
localStorage.removeItem("auth_token");
|
||||
//localStorage.removeItem("user");
|
||||
set({token: null});
|
||||
},
|
||||
};
|
||||
});
|
||||
6
frontend/src/lib/utils.ts
Normal file
6
frontend/src/lib/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
Reference in New Issue
Block a user