diff options
| author | diogo464 <[email protected]> | 2025-08-13 16:34:34 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-08-13 16:34:34 +0100 |
| commit | 817d5696ec6dfe7679467f05619d58b8d741230d (patch) | |
| tree | 24a2cb10369eaa6660773bfa2a7bb1b29bd9c9d2 /frontend | |
| parent | fe27e88eff20b342ba54e8df34ee962d62233552 (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')
| -rw-r--r-- | frontend/app/api/upload/route.ts | 3 | ||||
| -rw-r--r-- | frontend/components/drive/DriveDirectoryClient.tsx | 2 | ||||
| -rw-r--r-- | frontend/lib/auth.ts | 39 | ||||
| -rw-r--r-- | frontend/lib/auth_shared.ts | 26 | ||||
| -rw-r--r-- | frontend/lib/auth_types.ts | 13 |
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' | |||
| 3 | import { tmpdir } from 'os' | 3 | import { tmpdir } from 'os' |
| 4 | import { join } from 'path' | 4 | import { join } from 'path' |
| 5 | import { randomUUID } from 'crypto' | 5 | import { randomUUID } from 'crypto' |
| 6 | import { Auth_get_user, Auth_user_can_upload } from '@/lib/auth' | 6 | import { Auth_get_user } from '@/lib/auth' |
| 7 | import { Auth_user_can_upload } from '@/lib/auth_shared' | ||
| 7 | import { Drive_import } from '@/lib/drive_server' | 8 | import { Drive_import } from '@/lib/drive_server' |
| 8 | import { UPLOAD_MAX_FILE_SIZE } from '@/lib/constants' | 9 | import { 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 @@ | |||
| 3 | import type React from "react" | 3 | import type React from "react" |
| 4 | import { useState, useRef } from "react" | 4 | import { useState, useRef } from "react" |
| 5 | import Link from "next/link" | 5 | import Link from "next/link" |
| 6 | import { Auth_tinyauth_public_endpoint } from "@/lib/auth" | 6 | import { Auth_tinyauth_public_endpoint } from "@/lib/auth_shared" |
| 7 | import { | 7 | import { |
| 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 @@ | |||
| 1 | import { cookies } from 'next/headers'; | 1 | import { cookies } from 'next/headers'; |
| 2 | import { Env_is_development } from './env'; | 2 | import { Env_is_development } from './env'; |
| 3 | import { Elsie } from 'next/font/google'; | 3 | import type { UserSessionCookie, UserAuth } from './auth_types'; |
| 4 | 4 | ||
| 5 | export interface UserSessionCookie { | ||
| 6 | name: string, | ||
| 7 | value: string, | ||
| 8 | } | ||
| 9 | |||
| 10 | export interface UserAuth { | ||
| 11 | isLoggedIn: boolean, | ||
| 12 | username: string, | ||
| 13 | name: string, | ||
| 14 | email: string, | ||
| 15 | provider: string, | ||
| 16 | oauth: boolean, | ||
| 17 | } | ||
| 18 | 5 | ||
| 19 | export async function Auth_extract_session_cookie(): Promise<UserSessionCookie | null> { | 6 | export 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 | ||
| 97 | export 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 | |||
| 107 | function 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 | |||
| 114 | export 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 @@ | |||
| 1 | import { Env_is_development } from './env'; | ||
| 2 | import type { UserAuth } from './auth_types'; | ||
| 3 | |||
| 4 | export 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 | |||
| 14 | export 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 | |||
| 21 | export 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 @@ | |||
| 1 | export interface UserSessionCookie { | ||
| 2 | name: string, | ||
| 3 | value: string, | ||
| 4 | } | ||
| 5 | |||
| 6 | export interface UserAuth { | ||
| 7 | isLoggedIn: boolean, | ||
| 8 | username: string, | ||
| 9 | name: string, | ||
| 10 | email: string, | ||
| 11 | provider: string, | ||
| 12 | oauth: boolean, | ||
| 13 | } | ||
