forked from polarnl/PolarLearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirect.ts
More file actions
47 lines (40 loc) · 1.24 KB
/
redirect.ts
File metadata and controls
47 lines (40 loc) · 1.24 KB
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
/**
* Utility functions for handling authentication redirects
*/
/**
* Validates if a redirect path is safe to use
* @param path - The path to validate
* @returns The validated path or a default safe path
*/
export function getValidRedirectPath(path: string | null | undefined): string {
// Default redirect path
const defaultPath = '/home/start';
// If no path provided, use default
if (!path) {
return defaultPath;
}
// Ensure the path is relative (not absolute URL)
if (path.startsWith('http://') || path.startsWith('https://') || path.startsWith('//')) {
return defaultPath;
}
// Ensure the path starts with /
if (!path.startsWith('/')) {
path = '/' + path;
}
// Blacklist certain paths for security
// AI CODE START
const blockedPaths = ['/api/', '/auth/sign-out', '/manifest.json'];
// AI CODE END
if (blockedPaths.some(blocked => path.startsWith(blocked))) {
return defaultPath;
}
return path;
}
/**
* Clears the redirect cookie from the client side
*/
export function clearRedirectCookie(): void {
if (typeof document !== 'undefined') {
document.cookie = 'polarlearn.goto=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}
}