diff options
Diffstat (limited to 'frontend/lib/auth.ts')
| -rw-r--r-- | frontend/lib/auth.ts | 39 |
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 @@ | |||
| 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 | } | ||
