Working on Auth flows. Struggling with getting client and server to both update when signing in or out.
This commit is contained in:
@ -3,17 +3,18 @@
|
||||
import 'server-only';
|
||||
import { encodedRedirect } from '@/utils/utils';
|
||||
import { createServerClient } from '@/utils/supabase';
|
||||
import type { User } from '@supabase/supabase-js';
|
||||
import { headers } from 'next/headers';
|
||||
import { redirect } from 'next/navigation';
|
||||
import type { User } from '@/utils/supabase';
|
||||
import type { Result } from './index';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
|
||||
export const signUp = async (formData: FormData) => {
|
||||
const name = formData.get('name') as string;
|
||||
const email = formData.get('email') as string;
|
||||
const password = formData.get('password') as string;
|
||||
const supabase = await createServerClient();
|
||||
//const origin = (await headers()).get('origin');
|
||||
const origin = (await headers()).get('origin');
|
||||
|
||||
if (!email || !password) {
|
||||
return encodedRedirect(
|
||||
@ -23,36 +24,26 @@ export const signUp = async (formData: FormData) => {
|
||||
);
|
||||
}
|
||||
|
||||
const { data, error } = await supabase.auth.signUp({
|
||||
const { error } = await supabase.auth.signUp({
|
||||
email,
|
||||
password,
|
||||
//options: {
|
||||
//emailRedirectTo: `${origin}/auth/callback`,
|
||||
//},
|
||||
options: {
|
||||
emailRedirectTo: `${origin}/auth/callback`,
|
||||
data: {
|
||||
full_name: name,
|
||||
email,
|
||||
provider: 'email',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
return redirect('/');
|
||||
//return encodedRedirect('error', '/sign-up',
|
||||
//'Thanks for signing up! Please check your email for a verification link.');
|
||||
return encodedRedirect('error', '/sign-up', error.message);
|
||||
} else {
|
||||
try {
|
||||
if (!data.user) throw new Error('Could not sign up');
|
||||
const { error } = await supabase
|
||||
.from('profiles')
|
||||
.update({
|
||||
full_name: name,
|
||||
provider: 'email',
|
||||
})
|
||||
.eq('id', data.user.id);
|
||||
if (error) throw new Error('Could not update profile');
|
||||
} catch (error) {
|
||||
console.error('Error updating profile: ', error);
|
||||
} finally {
|
||||
return redirect('/');
|
||||
//return encodedRedirect('success', '/',
|
||||
//'Thanks for signing up! Please check your email for a verification link.);
|
||||
}
|
||||
return encodedRedirect(
|
||||
'success',
|
||||
'/sign-up',
|
||||
'Thanks for signing up! Please check your email for a verification link.',
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@ -68,8 +59,9 @@ export const signIn = async (formData: FormData) => {
|
||||
|
||||
if (error) {
|
||||
return encodedRedirect('error', '/sign-in', error.message);
|
||||
} else {
|
||||
return redirect('/');
|
||||
}
|
||||
return redirect('/');
|
||||
};
|
||||
|
||||
export const forgotPassword = async (formData: FormData) => {
|
||||
@ -147,10 +139,8 @@ export const resetPassword = async (
|
||||
|
||||
|
||||
export const resetPasswordFromEmail = async (formData: FormData) => {
|
||||
|
||||
const password = formData.get('password') as string;
|
||||
const confirmPassword = formData.get('confirmPassword') as string;
|
||||
|
||||
if (!password || !confirmPassword) {
|
||||
encodedRedirect(
|
||||
'error',
|
||||
@ -158,7 +148,6 @@ export const resetPasswordFromEmail = async (formData: FormData) => {
|
||||
'Password and confirm password are required',
|
||||
);
|
||||
}
|
||||
|
||||
const supabase = await createServerClient();
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
|
Reference in New Issue
Block a user