47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
/**
|
|
* Authentication utilities
|
|
* Client-side authentication helpers
|
|
*/
|
|
|
|
import type { AuthUser } from "./api/services/authService";
|
|
|
|
/**
|
|
* Get authentication token from localStorage
|
|
*/
|
|
export const getAuthToken = (): string | null => {
|
|
if (typeof window === "undefined") return null;
|
|
return localStorage.getItem("auth_token");
|
|
};
|
|
|
|
/**
|
|
* Get authenticated user from localStorage
|
|
*/
|
|
export const getAuthUser = (): AuthUser | null => {
|
|
if (typeof window === "undefined") return null;
|
|
const userStr = localStorage.getItem("auth_user");
|
|
if (!userStr) return null;
|
|
try {
|
|
return JSON.parse(userStr);
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Check if user is authenticated
|
|
*/
|
|
export const isAuthenticated = (): boolean => {
|
|
return !!getAuthToken() && !!getAuthUser();
|
|
};
|
|
|
|
/**
|
|
* Clear authentication data
|
|
*/
|
|
export const clearAuth = (): void => {
|
|
if (typeof window !== "undefined") {
|
|
localStorage.removeItem("auth_token");
|
|
localStorage.removeItem("auth_refresh_token");
|
|
localStorage.removeItem("auth_user");
|
|
}
|
|
};
|