aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/utils')
-rw-r--r--frontend/src/utils/Jwt.tsx44
1 files changed, 44 insertions, 0 deletions
diff --git a/frontend/src/utils/Jwt.tsx b/frontend/src/utils/Jwt.tsx
new file mode 100644
index 0000000..ce351fb
--- /dev/null
+++ b/frontend/src/utils/Jwt.tsx
@@ -0,0 +1,44 @@
1// llm ahh funcs
2export function get_user_id_from_token(token: string | undefined): string | undefined {
3 if (!token) {
4 return undefined;
5 }
6 const parts = token.split('.');
7 if (parts.length !== 3) {
8 return undefined;
9 }
10 const base64Url = parts[1];
11 const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
12
13 const jsonPayload = decodeURIComponent(
14 atob(base64)
15 .split('')
16 .map(function (c) {
17 return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
18 })
19 .join('')
20 );
21 return JSON.parse(jsonPayload).sub;
22};
23
24export function get_user_mod_from_token(token: string | undefined): boolean | undefined {
25 if (!token) {
26 return undefined;
27 }
28 const parts = token.split('.');
29 if (parts.length !== 3) {
30 return undefined;
31 }
32 const base64Url = parts[1];
33 const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
34
35 const jsonPayload = decodeURIComponent(
36 atob(base64)
37 .split('')
38 .map(function (c) {
39 return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
40 })
41 .join('')
42 );
43 return JSON.parse(jsonPayload).mod;
44};