summaryrefslogtreecommitdiff
path: root/frontend/lib
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/lib')
-rw-r--r--frontend/lib/auth.ts39
-rw-r--r--frontend/lib/auth_shared.ts26
-rw-r--r--frontend/lib/auth_types.ts13
3 files changed, 41 insertions, 37 deletions
diff --git a/frontend/lib/auth.ts b/frontend/lib/auth.ts
index 015fddf..55255fc 100644
--- a/frontend/lib/auth.ts
+++ b/frontend/lib/auth.ts
@@ -1,20 +1,7 @@
1import { cookies } from 'next/headers'; 1import { cookies } from 'next/headers';
2import { Env_is_development } from './env'; 2import { Env_is_development } from './env';
3import { Elsie } from 'next/font/google'; 3import type { UserSessionCookie, UserAuth } from './auth_types';
4 4
5export interface UserSessionCookie {
6 name: string,
7 value: string,
8}
9
10export interface UserAuth {
11 isLoggedIn: boolean,
12 username: string,
13 name: string,
14 email: string,
15 provider: string,
16 oauth: boolean,
17}
18 5
19export async function Auth_extract_session_cookie(): Promise<UserSessionCookie | null> { 6export async function Auth_extract_session_cookie(): Promise<UserSessionCookie | null> {
20 const cookieStore = await cookies(); 7 const cookieStore = await cookies();
@@ -47,6 +34,7 @@ export async function Auth_get_user(): Promise<UserAuth> {
47 } 34 }
48 35
49 const cookie = await Auth_extract_session_cookie(); 36 const cookie = await Auth_extract_session_cookie();
37 const { Auth_tinyauth_endpoint } = await import('./auth_shared');
50 const endpoint = Auth_tinyauth_endpoint(); 38 const endpoint = Auth_tinyauth_endpoint();
51 39
52 try { 40 try {
@@ -94,26 +82,3 @@ export async function Auth_get_user(): Promise<UserAuth> {
94 } 82 }
95} 83}
96 84
97export function Auth_user_can_upload(user: UserAuth): boolean {
98 if (!user.isLoggedIn)
99 return false;
100
101 if (Env_is_development())
102 return true;
103
104 return user.oauth && user.email.endsWith("@campus.fct.unl.pt");
105}
106
107function Auth_tinyauth_endpoint(): string {
108 const endpoint = process.env.TINYAUTH_ENDPOINT;
109 if (endpoint == undefined)
110 throw new Error(`env var TINYAUTH_ENDPOINT not defined`);
111 return endpoint;
112}
113
114export function Auth_tinyauth_public_endpoint(): string {
115 const endpoint = process.env.TINYAUTH_PUBLIC_ENDPOINT;
116 if (endpoint == undefined)
117 throw new Error(`env var TINYAUTH_PUBLIC_ENDPOINT not defined`);
118 return endpoint;
119}
diff --git a/frontend/lib/auth_shared.ts b/frontend/lib/auth_shared.ts
new file mode 100644
index 0000000..b23b046
--- /dev/null
+++ b/frontend/lib/auth_shared.ts
@@ -0,0 +1,26 @@
1import { Env_is_development } from './env';
2import type { UserAuth } from './auth_types';
3
4export function Auth_user_can_upload(user: UserAuth): boolean {
5 if (!user.isLoggedIn)
6 return false;
7
8 if (Env_is_development())
9 return true;
10
11 return user.oauth && user.email.endsWith("@campus.fct.unl.pt");
12}
13
14export function Auth_tinyauth_endpoint(): string {
15 const endpoint = process.env.TINYAUTH_ENDPOINT;
16 if (endpoint == undefined)
17 throw new Error(`env var TINYAUTH_ENDPOINT not defined`);
18 return endpoint;
19}
20
21export function Auth_tinyauth_public_endpoint(): string {
22 const endpoint = process.env.TINYAUTH_PUBLIC_ENDPOINT;
23 if (endpoint == undefined)
24 throw new Error(`env var TINYAUTH_PUBLIC_ENDPOINT not defined`);
25 return endpoint;
26}
diff --git a/frontend/lib/auth_types.ts b/frontend/lib/auth_types.ts
new file mode 100644
index 0000000..fca1122
--- /dev/null
+++ b/frontend/lib/auth_types.ts
@@ -0,0 +1,13 @@
1export interface UserSessionCookie {
2 name: string,
3 value: string,
4}
5
6export interface UserAuth {
7 isLoggedIn: boolean,
8 username: string,
9 name: string,
10 email: string,
11 provider: string,
12 oauth: boolean,
13}