Files
webshop/shop/lib/actions/auth.ts
DanielS 62e3c7804a
All checks were successful
Staging Build / build (push) Successful in 2m16s
feat: fix logout button, inactivity tracker, and concurrent session termination
2026-06-24 00:15:59 +02:00

69 lines
1.8 KiB
TypeScript

'use server'
import { createClient } from '@/lib/supabase/server'
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.")
}
// Andere aktive Sitzungen beenden
await supabase.auth.signOut({ scope: 'others' })
}
return { success: true }
}
export async function signUp(email: string, password: string) {
throw new Error("Registrierung ist deaktiviert.")
}
export async function signOut() {
const supabase = await createClient()
const { error } = await supabase.auth.signOut()
if (error) {
throw new Error(error.message)
}
return { success: true }
}
export async function resetPassword(email: string) {
const supabase = await createClient()
const origin = (await headers()).get('origin') || ''
const { error } = await supabase.auth.resetPasswordForEmail(email, {
redirectTo: `${origin}/auth/update-password`,
})
if (error) {
throw new Error(error.message)
}
return { success: true }
}
export async function updatePassword(password: string) {
const supabase = await createClient()
const { error } = await supabase.auth.updateUser({ password })
if (error) {
throw new Error(error.message)
}
return { success: true }
}