fix: production login error masking, resolve navbar hydration mismatch, and gracefully handle database render errors
All checks were successful
Staging Build / build (push) Successful in 2m15s
All checks were successful
Staging Build / build (push) Successful in 2m15s
This commit is contained in:
@@ -1,38 +1,68 @@
|
||||
'use server'
|
||||
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { createAdminClient } from '@/lib/supabase/admin'
|
||||
import { headers } from 'next/headers'
|
||||
|
||||
export async function signIn(email: string, password: string) {
|
||||
const supabase = await createClient()
|
||||
const { data, error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
})
|
||||
if (error) {
|
||||
throw new Error(error.message)
|
||||
}
|
||||
|
||||
const userId = data.user?.id
|
||||
if (userId) {
|
||||
const { data: userData, error: userError } = await supabase
|
||||
.from('users')
|
||||
.select('role')
|
||||
.eq('id', userId)
|
||||
.single()
|
||||
|
||||
if (userError || !userData || (userData.role !== 'partner' && userData.role !== 'admin')) {
|
||||
await supabase.auth.signOut()
|
||||
throw new Error("Zugriff verweigert. Nur registrierte Partner dürfen sich anmelden.")
|
||||
try {
|
||||
const supabase = await createClient()
|
||||
const { data, error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
})
|
||||
if (error) {
|
||||
return { success: false, error: error.message }
|
||||
}
|
||||
|
||||
// Andere aktive Sitzungen beenden
|
||||
await supabase.auth.signOut({ scope: 'others' })
|
||||
const userId = data.user?.id
|
||||
if (userId) {
|
||||
let { data: userData, error: userError } = await supabase
|
||||
.from('users')
|
||||
.select('role')
|
||||
.eq('id', userId)
|
||||
.single()
|
||||
|
||||
return { success: true, role: userData.role }
|
||||
if (userError || !userData) {
|
||||
// Fallback: create database records if missing for this authenticated user
|
||||
try {
|
||||
const adminSupabase = createAdminClient()
|
||||
|
||||
// Ensure profile exists
|
||||
await adminSupabase.from('profiles').insert([{ id: userId }]).select()
|
||||
|
||||
// Ensure user exists
|
||||
const { data: insertedUser, error: insertError } = await adminSupabase
|
||||
.from('users')
|
||||
.insert([{ id: userId, role: 'partner' }])
|
||||
.select('role')
|
||||
.single()
|
||||
|
||||
if (!insertError && insertedUser) {
|
||||
userData = insertedUser
|
||||
userError = null
|
||||
}
|
||||
} catch (adminErr) {
|
||||
console.error("Failed to backfill user records:", adminErr)
|
||||
}
|
||||
}
|
||||
|
||||
if (userError || !userData || (userData.role !== 'partner' && userData.role !== 'admin')) {
|
||||
await supabase.auth.signOut()
|
||||
return { success: false, error: "Zugriff verweigert. Nur registrierte Partner dürfen sich anmelden." }
|
||||
}
|
||||
|
||||
// Andere aktive Sitzungen beenden
|
||||
await supabase.auth.signOut({ scope: 'others' })
|
||||
|
||||
return { success: true, role: userData.role }
|
||||
}
|
||||
|
||||
return { success: true, role: 'partner' }
|
||||
} catch (err: any) {
|
||||
console.error("SignIn error:", err)
|
||||
return { success: false, error: err?.message || "Ein unerwarteter Fehler ist aufgetreten." }
|
||||
}
|
||||
|
||||
return { success: true, role: 'partner' }
|
||||
}
|
||||
|
||||
export async function signUp(email: string, password: string) {
|
||||
|
||||
Reference in New Issue
Block a user