From 817d5696ec6dfe7679467f05619d58b8d741230d Mon Sep 17 00:00:00 2001 From: diogo464 Date: Wed, 13 Aug 2025 16:34:34 +0100 Subject: refactor: split auth module into separate files and fix imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- frontend/app/api/upload/route.ts | 3 +- frontend/components/drive/DriveDirectoryClient.tsx | 2 +- frontend/lib/auth.ts | 39 ++-------------------- frontend/lib/auth_shared.ts | 26 +++++++++++++++ frontend/lib/auth_types.ts | 13 ++++++++ 5 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 frontend/lib/auth_shared.ts create mode 100644 frontend/lib/auth_types.ts 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' import { tmpdir } from 'os' import { join } from 'path' import { randomUUID } from 'crypto' -import { Auth_get_user, Auth_user_can_upload } from '@/lib/auth' +import { Auth_get_user } from '@/lib/auth' +import { Auth_user_can_upload } from '@/lib/auth_shared' import { Drive_import } from '@/lib/drive_server' import { UPLOAD_MAX_FILE_SIZE } from '@/lib/constants' 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 @@ import type React from "react" import { useState, useRef } from "react" import Link from "next/link" -import { Auth_tinyauth_public_endpoint } from "@/lib/auth" +import { Auth_tinyauth_public_endpoint } from "@/lib/auth_shared" import { ChevronRight, 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 @@ import { cookies } from 'next/headers'; import { Env_is_development } from './env'; -import { Elsie } from 'next/font/google'; +import type { UserSessionCookie, UserAuth } from './auth_types'; -export interface UserSessionCookie { - name: string, - value: string, -} - -export interface UserAuth { - isLoggedIn: boolean, - username: string, - name: string, - email: string, - provider: string, - oauth: boolean, -} export async function Auth_extract_session_cookie(): Promise { const cookieStore = await cookies(); @@ -47,6 +34,7 @@ export async function Auth_get_user(): Promise { } const cookie = await Auth_extract_session_cookie(); + const { Auth_tinyauth_endpoint } = await import('./auth_shared'); const endpoint = Auth_tinyauth_endpoint(); try { @@ -94,26 +82,3 @@ export async function Auth_get_user(): Promise { } } -export function Auth_user_can_upload(user: UserAuth): boolean { - if (!user.isLoggedIn) - return false; - - if (Env_is_development()) - return true; - - return user.oauth && user.email.endsWith("@campus.fct.unl.pt"); -} - -function Auth_tinyauth_endpoint(): string { - const endpoint = process.env.TINYAUTH_ENDPOINT; - if (endpoint == undefined) - throw new Error(`env var TINYAUTH_ENDPOINT not defined`); - return endpoint; -} - -export function Auth_tinyauth_public_endpoint(): string { - const endpoint = process.env.TINYAUTH_PUBLIC_ENDPOINT; - if (endpoint == undefined) - throw new Error(`env var TINYAUTH_PUBLIC_ENDPOINT not defined`); - return endpoint; -} 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 @@ +import { Env_is_development } from './env'; +import type { UserAuth } from './auth_types'; + +export function Auth_user_can_upload(user: UserAuth): boolean { + if (!user.isLoggedIn) + return false; + + if (Env_is_development()) + return true; + + return user.oauth && user.email.endsWith("@campus.fct.unl.pt"); +} + +export function Auth_tinyauth_endpoint(): string { + const endpoint = process.env.TINYAUTH_ENDPOINT; + if (endpoint == undefined) + throw new Error(`env var TINYAUTH_ENDPOINT not defined`); + return endpoint; +} + +export function Auth_tinyauth_public_endpoint(): string { + const endpoint = process.env.TINYAUTH_PUBLIC_ENDPOINT; + if (endpoint == undefined) + throw new Error(`env var TINYAUTH_PUBLIC_ENDPOINT not defined`); + return endpoint; +} 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 @@ +export interface UserSessionCookie { + name: string, + value: string, +} + +export interface UserAuth { + isLoggedIn: boolean, + username: string, + name: string, + email: string, + provider: string, + oauth: boolean, +} -- cgit