File size: 1,131 Bytes
8d7950f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dffa8ae
8d7950f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * Admin Auth Utility for LeadPilot Admin Portal
 * Stores the admin JWT separately from the product JWT.
 * Do NOT mix with src/lib/auth.ts (product user session).
 */

const ADMIN_TOKEN_KEY = "leadpilot_admin_token";

export interface AdminUser {
    id: string;
    email: string;
    full_name: string;
    is_superuser: boolean;
    role?: { name: string; permissions: string[] } | null;
}

export const adminAuth = {
    getToken: (): string | null => {
        if (typeof window === "undefined") return null;
        return localStorage.getItem(ADMIN_TOKEN_KEY);
    },

    setToken: (token: string): void => {
        if (typeof window === "undefined") return;
        localStorage.setItem(ADMIN_TOKEN_KEY, token);
    },

    clearToken: (): void => {
        if (typeof window === "undefined") return;
        localStorage.removeItem(ADMIN_TOKEN_KEY);
    },

    isAuthed: (): boolean => {
        return !!adminAuth.getToken();
    },

    logout: (): void => {
        adminAuth.clearToken();
        if (typeof window !== "undefined") {
            window.location.href = "/app/admin/login";
        }
    },
};