mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
93 lines
1.7 KiB
TypeScript
93 lines
1.7 KiB
TypeScript
import { redirect } from 'react-router-dom'
|
|
import { getSession } from './auth-client'
|
|
|
|
export interface AuthConfig {
|
|
enabledProviders: string[]
|
|
registrationEnabled: boolean
|
|
isFirstUser: boolean
|
|
adminConfigured: boolean
|
|
}
|
|
|
|
async function fetchAuthConfig(): Promise<AuthConfig> {
|
|
const defaultConfig: AuthConfig = {
|
|
enabledProviders: ['credentials'],
|
|
registrationEnabled: true,
|
|
isFirstUser: false,
|
|
adminConfigured: false,
|
|
}
|
|
const response = await fetch('/api/auth-info/config')
|
|
if (!response.ok) {
|
|
return defaultConfig
|
|
}
|
|
try {
|
|
return await response.json()
|
|
} catch {
|
|
return defaultConfig
|
|
}
|
|
}
|
|
|
|
export async function loginLoader() {
|
|
const [config, session] = await Promise.all([
|
|
fetchAuthConfig(),
|
|
getSession(),
|
|
])
|
|
|
|
if (session.data?.user) {
|
|
return redirect('/')
|
|
}
|
|
|
|
if (config.isFirstUser && !config.adminConfigured) {
|
|
return redirect('/setup')
|
|
}
|
|
|
|
return { config }
|
|
}
|
|
|
|
export async function setupLoader() {
|
|
const [config, session] = await Promise.all([
|
|
fetchAuthConfig(),
|
|
getSession(),
|
|
])
|
|
|
|
if (session.data?.user) {
|
|
return redirect('/')
|
|
}
|
|
|
|
if (!config.isFirstUser || config.adminConfigured) {
|
|
return redirect('/login')
|
|
}
|
|
|
|
return { config }
|
|
}
|
|
|
|
export async function registerLoader() {
|
|
const [config, session] = await Promise.all([
|
|
fetchAuthConfig(),
|
|
getSession(),
|
|
])
|
|
|
|
if (session.data?.user) {
|
|
return redirect('/')
|
|
}
|
|
|
|
if (!config.registrationEnabled) {
|
|
return redirect('/login')
|
|
}
|
|
|
|
if (config.isFirstUser && !config.adminConfigured) {
|
|
return redirect('/setup')
|
|
}
|
|
|
|
return { config }
|
|
}
|
|
|
|
export async function protectedLoader() {
|
|
const session = await getSession()
|
|
|
|
if (!session.data?.user) {
|
|
return redirect('/login')
|
|
}
|
|
|
|
return null
|
|
}
|