Initial commit
This commit is contained in:
98
skills/supabase-auth-ssr-setup/assets/auth-actions.ts
Normal file
98
skills/supabase-auth-ssr-setup/assets/auth-actions.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
'use server';
|
||||
|
||||
import { createServerClient } from '@/lib/supabase/server';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
/**
|
||||
* Sign out the current user
|
||||
*/
|
||||
export async function logout() {
|
||||
const supabase = await createServerClient();
|
||||
await supabase.auth.signOut();
|
||||
revalidatePath('/', 'layout');
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign in with email and password
|
||||
*/
|
||||
export async function signInWithPassword(formData: FormData) {
|
||||
const email = formData.get('email') as string;
|
||||
const password = formData.get('password') as string;
|
||||
|
||||
const supabase = await createServerClient();
|
||||
const { error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
return { error: error.message };
|
||||
}
|
||||
|
||||
revalidatePath('/', 'layout');
|
||||
redirect('/dashboard');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign up with email and password
|
||||
*/
|
||||
export async function signUpWithPassword(formData: FormData) {
|
||||
const email = formData.get('email') as string;
|
||||
const password = formData.get('password') as string;
|
||||
|
||||
const supabase = await createServerClient();
|
||||
const { error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password,
|
||||
options: {
|
||||
emailRedirectTo: `${process.env.NEXT_PUBLIC_SITE_URL}/auth/callback`,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
return { error: error.message };
|
||||
}
|
||||
|
||||
return { success: 'Check your email to confirm your account' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Send magic link for passwordless login
|
||||
*/
|
||||
export async function signInWithMagicLink(formData: FormData) {
|
||||
const email = formData.get('email') as string;
|
||||
|
||||
const supabase = await createServerClient();
|
||||
const { error } = await supabase.auth.signInWithOtp({
|
||||
email,
|
||||
options: {
|
||||
emailRedirectTo: `${process.env.NEXT_PUBLIC_SITE_URL}/auth/callback`,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
return { error: error.message };
|
||||
}
|
||||
|
||||
return { success: 'Check your email for the magic link' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Request password reset
|
||||
*/
|
||||
export async function resetPassword(formData: FormData) {
|
||||
const email = formData.get('email') as string;
|
||||
|
||||
const supabase = await createServerClient();
|
||||
const { error } = await supabase.auth.resetPasswordForEmail(email, {
|
||||
redirectTo: `${process.env.NEXT_PUBLIC_SITE_URL}/auth/reset-password`,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
return { error: error.message };
|
||||
}
|
||||
|
||||
return { success: 'Check your email for the password reset link' };
|
||||
}
|
||||
Reference in New Issue
Block a user