- Pages: login, register, tasks list, task detail - Components: TaskCard, TaskForm, GroupSelector, StatusBadge, Header - Tailwind CSS, dark/light mode, PWA manifest - Running on :3001 via systemd Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useContext } from "react";
|
|
import { User } from "./api";
|
|
|
|
export interface AuthState {
|
|
token: string | null;
|
|
user: User | null;
|
|
setAuth: (token: string | null, user: User | null) => void;
|
|
logout: () => void;
|
|
}
|
|
|
|
export const AuthContext = createContext<AuthState>({
|
|
token: null,
|
|
user: null,
|
|
setAuth: () => {},
|
|
logout: () => {},
|
|
});
|
|
|
|
export function useAuth() {
|
|
return useContext(AuthContext);
|
|
}
|
|
|
|
export function getStoredToken(): string | null {
|
|
if (typeof window === "undefined") return null;
|
|
return localStorage.getItem("taskteam_token");
|
|
}
|
|
|
|
export function setStoredToken(token: string | null) {
|
|
if (typeof window === "undefined") return;
|
|
if (token) {
|
|
localStorage.setItem("taskteam_token", token);
|
|
} else {
|
|
localStorage.removeItem("taskteam_token");
|
|
}
|
|
}
|
|
|
|
export function getStoredUser(): User | null {
|
|
if (typeof window === "undefined") return null;
|
|
const raw = localStorage.getItem("taskteam_user");
|
|
if (!raw) return null;
|
|
try {
|
|
return JSON.parse(raw);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function setStoredUser(user: User | null) {
|
|
if (typeof window === "undefined") return;
|
|
if (user) {
|
|
localStorage.setItem("taskteam_user", JSON.stringify(user));
|
|
} else {
|
|
localStorage.removeItem("taskteam_user");
|
|
}
|
|
}
|