- Updated the registration flow to check if the first admin account has been created, preventing further public registrations. - Introduced `is_first_admin_setup_available` function to determine registration availability. - Modified the `/register` and `/login` pages to redirect based on the setup state. - Enhanced the user creation process to handle internal admin accounts correctly. - Added migration script to enforce the new registration rules in the database. - Refactored chat API to improve message handling and context building. - Updated dashboard and settings components for better state management. - Improved error handling and user feedback across various components.
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
'use server'
|
||
|
||
import { revalidatePath } from 'next/cache'
|
||
import { redirect } from 'next/navigation'
|
||
import { createClient } from '@/lib/supabase/server'
|
||
import { getFirstAdminSetupState } from '@/lib/auth/first-admin-setup'
|
||
|
||
export async function login(formData: FormData) {
|
||
const supabase = await createClient()
|
||
|
||
const data = {
|
||
email: formData.get('email') as string,
|
||
password: formData.get('password') as string,
|
||
}
|
||
|
||
const { error } = await supabase.auth.signInWithPassword(data)
|
||
|
||
if (error) {
|
||
redirect(`/login?error=true&message=${encodeURIComponent(error.message)}`)
|
||
}
|
||
|
||
revalidatePath('/', 'layout')
|
||
redirect('/')
|
||
}
|
||
|
||
export async function signup(formData: FormData) {
|
||
const setupState = await getFirstAdminSetupState()
|
||
|
||
if (setupState.errorMessage) {
|
||
redirect(`/register?error=true&message=${encodeURIComponent(setupState.errorMessage)}`)
|
||
}
|
||
|
||
if (!setupState.available) {
|
||
redirect(
|
||
`/login?error=true&message=${encodeURIComponent(
|
||
'Kayıt kapalı. Bu self-host kurulumunda ilk admin hesabı zaten oluşturulmuş.',
|
||
)}`,
|
||
)
|
||
}
|
||
|
||
const supabase = await createClient()
|
||
|
||
const data = {
|
||
email: formData.get('email') as string,
|
||
password: formData.get('password') as string,
|
||
}
|
||
|
||
const { error } = await supabase.auth.signUp(data)
|
||
|
||
if (error) {
|
||
redirect(`/register?error=true&message=${encodeURIComponent(error.message)}`)
|
||
}
|
||
|
||
revalidatePath('/', 'layout')
|
||
redirect('/')
|
||
}
|
||
|
||
export async function signOut() {
|
||
const supabase = await createClient()
|
||
|
||
await supabase.auth.signOut()
|
||
|
||
revalidatePath('/', 'layout')
|
||
redirect('/login')
|
||
}
|