Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signing up new user #65

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions src/features/authentication/SignupForm.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
import { Button } from '@/ui/Buttons';
import { Form, FormRow, Input } from '@/ui/FormComponent';

import { useForm } from 'react-hook-form';
import { useSignup } from './useSignup';
// Email regex: /\S+@\S+\.\S+/

function SignupForm() {
const { signup, isLoading } = useSignup();
const { register, formState, handleSubmit, reset, getValues } = useForm();
const { errors } = formState;

function onSubmit({ email, password, fullName }) {
signup({ email, password, fullName }, { onSettled: reset });
}
return (
<Form>
<FormRow label='Full name' error={''}>
<Input type='text' id='fullName' />
<Form onSubmit={handleSubmit(onSubmit)}>
<FormRow label='Full name' error={errors?.fullName?.message}>
<Input
type='text'
id='fullName'
{...register('fullName', { required: 'This field is required' })}
/>
</FormRow>

<FormRow label='Email address' error={''}>
<Input type='email' id='email' />
<FormRow label='Email address' error={errors?.email?.message}>
<Input
type='email'
id='email'
{...register('email', {
required: 'This field is required',
pattern: { value: /\S+@\S+\.\S+/, message: 'Invalid email address' },
})}
/>
</FormRow>

<FormRow label='Password (min 8 characters)' error={''}>
<Input type='password' id='password' />
<FormRow label='Password (min 8 characters)' error={errors?.password?.message}>
<Input
type='password'
id='password'
{...register('password', {
required: 'This field is required',
minLength: { value: 8, message: 'Password must be at least 8 characters' },
})}
/>
</FormRow>

<FormRow label='Repeat password' error={''}>
<Input type='password' id='passwordConfirm' />
<FormRow label='Repeat password' error={errors?.passwordConfirm?.message}>
<Input
type='password'
id='passwordConfirm'
{...register('passwordConfirm', {
required: 'This field is required',
validate: (value) => value === getValues().password || 'Passwords do not match',
})}
/>
</FormRow>

<FormRow>
Expand Down
15 changes: 15 additions & 0 deletions src/features/authentication/useSignup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useMutation } from '@tanstack/react-query';
import { signup as signupApi } from '../../services/apiAuthentication';
import toast from 'react-hot-toast';

export function useSignup() {
const { mutate: signup, isLoading } = useMutation({
mutationFn: signupApi,
onSuccess: (user) => {
console.log('User signed up successfully', user);
toast.success('User signed up successfully, please verify your email');
},
});

return { signup, isLoading };
}
8 changes: 7 additions & 1 deletion src/pages/Users.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Heading } from '@/ui/Common';
import { SignupForm } from '@/features/authentication';

function NewUsers() {
return <Heading as='h1'>Create a new user</Heading>;
return (
<>
<Heading as='h1'>Create a new user</Heading>
<SignupForm />
</>
);
}

export default NewUsers;
19 changes: 16 additions & 3 deletions src/services/apiAuthentication.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import supabase from './supabase';

export async function signup({ email, password, fullName }) {
const { data, error } = await supabase.auth.signUp({
email,
password,
options: {
data: { fullName, avatar: '', role: 'user' },
},
});

if (error) {
console.error(error);
throw new Error(error.message || 'Signup failed');
}
return data;
}

export async function login({ email, password }) {
const { data, error } = await supabase.auth.signInWithPassword({
email,
Expand All @@ -10,9 +26,6 @@ export async function login({ email, password }) {
console.error(error);
throw new Error(error.message || 'Login failed');
}

console.log('Authenticated user:', data);

return data;
}

Expand Down
Loading