summaryrefslogtreecommitdiff
path: root/frontend/lib/auth.ts
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-13 16:34:34 +0100
committerdiogo464 <[email protected]>2025-08-13 16:34:34 +0100
commit817d5696ec6dfe7679467f05619d58b8d741230d (patch)
tree24a2cb10369eaa6660773bfa2a7bb1b29bd9c9d2 /frontend/lib/auth.ts
parentfe27e88eff20b342ba54e8df34ee962d62233552 (diff)
refactor: split auth module into separate files and fix imports
- Created auth_types.ts for type definitions - Created auth_shared.ts for utility functions and endpoints - Updated auth.ts to focus on session management - Fixed all auth import statements throughout codebase - Updated login redirect to use Auth_tinyauth_public_endpoint properly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Diffstat (limited to 'frontend/lib/auth.ts')
-rw-r--r--frontend/lib/auth.ts39
1 files changed, 2 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}