feat: port all auth forms to Next.js Server Actions to avoid exposing Supabase API to the browser
All checks were successful
Staging Build / build (push) Successful in 2m19s
All checks were successful
Staging Build / build (push) Successful in 2m19s
This commit is contained in:
62
shop/lib/actions/auth.ts
Normal file
62
shop/lib/actions/auth.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
'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 { error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
})
|
||||
if (error) {
|
||||
throw new Error(error.message)
|
||||
}
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
export async function signUp(email: string, password: string) {
|
||||
const supabase = await createClient()
|
||||
const origin = (await headers()).get('origin') || ''
|
||||
const { error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password,
|
||||
options: {
|
||||
emailRedirectTo: `${origin}/protected`,
|
||||
},
|
||||
})
|
||||
if (error) {
|
||||
throw new Error(error.message)
|
||||
}
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
Reference in New Issue
Block a user