summaryrefslogtreecommitdiff
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
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]>
-rw-r--r--frontend/app/api/upload/route.ts3
-rw-r--r--frontend/components/drive/DriveDirectoryClient.tsx2
-rw-r--r--frontend/lib/auth.ts39
-rw-r--r--frontend/lib/auth_shared.ts26
-rw-r--r--frontend/lib/auth_types.ts13
5 files changed, 44 insertions, 39 deletions
diff --git a/frontend/app/api/upload/route.ts b/frontend/app/api/upload/route.ts
index 07ab908..5ce0640 100644
--- a/frontend/app/api/upload/route.ts
+++ b/frontend/app/api/upload/route.ts
@@ -3,7 +3,8 @@ import { writeFile, unlink } from 'fs/promises'
3import { tmpdir } from 'os' 3import { tmpdir } from 'os'
4import { join } from 'path' 4import { join } from 'path'
5import { randomUUID } from 'crypto' 5import { randomUUID } from 'crypto'
6import { Auth_get_user, Auth_user_can_upload } from '@/lib/auth' 6import { Auth_get_user } from '@/lib/auth'
7import { Auth_user_can_upload } from '@/lib/auth_shared'
7import { Drive_import } from '@/lib/drive_server' 8import { Drive_import } from '@/lib/drive_server'
8import { UPLOAD_MAX_FILE_SIZE } from '@/lib/constants' 9import { UPLOAD_MAX_FILE_SIZE } from '@/lib/constants'
9 10
diff --git a/frontend/components/drive/DriveDirectoryClient.tsx b/frontend/components/drive/DriveDirectoryClient.tsx
index d64fb5d..d48be61 100644
--- a/frontend/components/drive/DriveDirectoryClient.tsx
+++ b/frontend/components/drive/DriveDirectoryClient.tsx
@@ -3,7 +3,7 @@
3import type React from "react" 3import type React from "react"
4import { useState, useRef } from "react" 4import { useState, useRef } from "react"
5import Link from "next/link" 5import Link from "next/link"
6import { Auth_tinyauth_public_endpoint } from "@/lib/auth" 6import { Auth_tinyauth_public_endpoint } from "@/lib/auth_shared"
7import { 7import {
8 ChevronRight, 8 ChevronRight,
9 File, 9 File,
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}