v2: global player, infinite scroll, image picker, threaded comments
This commit is contained in:
@@ -4,11 +4,27 @@ import { AuthContext, type AuthContextValue } from "./AuthContext.ts";
|
||||
|
||||
import { type AuthResponse, deserializeAuthResponse } from "../model.ts";
|
||||
|
||||
function isTokenExpired(token: string): boolean {
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split(".")[1]));
|
||||
return typeof payload.exp === "number" && payload.exp * 1000 < Date.now();
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [authResponse, setAuthResponse] = useState<AuthResponse | null>(() => {
|
||||
const stored = localStorage.getItem("authResponse");
|
||||
if (!stored) return null;
|
||||
|
||||
return stored ? deserializeAuthResponse(JSON.parse(stored)) : null;
|
||||
const parsed = deserializeAuthResponse(JSON.parse(stored));
|
||||
if (isTokenExpired(parsed.token)) {
|
||||
localStorage.removeItem("authResponse");
|
||||
return null;
|
||||
}
|
||||
|
||||
return parsed;
|
||||
});
|
||||
|
||||
const value: AuthContextValue = { authResponse, setAuthResponse };
|
||||
|
||||
Reference in New Issue
Block a user