-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthUtils.ts
55 lines (50 loc) · 1.46 KB
/
authUtils.ts
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
45
46
47
48
49
50
51
52
53
54
55
import { apiRequest } from "./apiUtils";
/**
* Get authentication data including user profile
* @returns The user authentication data or null if not authenticated
*/
export async function checkAuthentication() {
try {
const authData = await apiRequest<{
authenticated: boolean;
userId: string;
email?: string;
name?: string;
permission?: string;
roles?: string[];
}>('/api/auth/profile');
if (authData && authData.authenticated && authData.userId) {
return authData;
}
return null;
} catch (error: unknown) {
console.error('Authentication error:', error);
return null;
}
}
/**
* Redirect to login page with current location as callback URL
* @param currentPath The current path to redirect back to after login
*/
export function redirectToLogin(currentPath: string) {
window.location.href = `/auth/signin?callbackUrl=${encodeURIComponent(currentPath)}`;
}
/**
* Check if user has admin privileges
* @param userData User data from checkAuthentication
* @returns boolean indicating if user has admin access
*/
export function hasAdminAccess(userData: any): boolean {
return (
userData &&
(userData.permission === 'admin' ||
userData.permission === 'superadmin' ||
userData.permission === 'owner' ||
(userData.roles && (
userData.roles.includes('admin') ||
userData.roles.includes('superadmin') ||
userData.roles.includes('owner')
))
)
);
}