signup functioon
This commit is contained in:
parent
5b3aa117cd
commit
79ac1c3072
|
|
@ -29,10 +29,22 @@ const login = async ({email, password}: {email: string, password: string}) => {
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
const signup = async (email: string, password: string) => {
|
const signup = async (formData: FormData) => {
|
||||||
|
const form = {
|
||||||
|
email: formData.get('email') as string,
|
||||||
|
password: formData.get('password') as string,
|
||||||
|
"password-confirm": formData.get('password-confirm') as string,
|
||||||
|
firstName: formData.get('firstName') as string,
|
||||||
|
lastName: formData.get('lastName') as string,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (form.password !== form["password-confirm"]) {
|
||||||
|
throw "Passwords do not match";
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch('/api/auth/signup', {
|
const response = await fetch('/api/auth/signup', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ email, password }),
|
body: JSON.stringify(form),
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,22 @@
|
||||||
|
import { Token } from '@/types';
|
||||||
import logo from '@assets/images/logo.svg';
|
import logo from '@assets/images/logo.svg';
|
||||||
|
import { toastError, toastSuccess } from '@controllers/Toasts';
|
||||||
|
import { signup } from '@controllers/UserController';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const data = new FormData(e.target as HTMLFormElement);
|
||||||
|
signup(data).then((data: Token) => {
|
||||||
|
toastSuccess('You have successfully signed up!');
|
||||||
|
navigate('/login');
|
||||||
|
}).catch((err) => {
|
||||||
|
toastError(err as string);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex flex-col min-h-full items-center justify-center py-12 px-4 bg-grey-dark sm:px-6 lg:px-8">
|
<div className="flex flex-col min-h-full items-center justify-center py-12 px-4 bg-grey-dark sm:px-6 lg:px-8">
|
||||||
|
|
@ -8,31 +24,31 @@ export default () => {
|
||||||
<img className="mx-auto h-20 pb-2 w-auto" src={logo} alt="Groupomania" />
|
<img className="mx-auto h-20 pb-2 w-auto" src={logo} alt="Groupomania" />
|
||||||
</div>
|
</div>
|
||||||
<div className="w-full max-w-md space-y-8 bg-grey rounded-lg p-5">
|
<div className="w-full max-w-md space-y-8 bg-grey rounded-lg p-5">
|
||||||
<form className="m-6 " action="#" method="POST">
|
<form className="m-6 " action="#" method="POST" onSubmit={(e) => onSubmit(e)}>
|
||||||
<div className="-space-y-px rounded-md shadow-sm">
|
<div className="-space-y-px rounded-md shadow-sm">
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="lastname" className="sr-only">
|
<label htmlFor="lastName" className="sr-only">
|
||||||
Nom
|
Nom
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
id="lastname"
|
id="lastName"
|
||||||
name="lastname"
|
name="lastName"
|
||||||
type="text"
|
type="text"
|
||||||
autoComplete="lastname"
|
autoComplete="lastName"
|
||||||
required
|
required
|
||||||
className="relative block w-full appearance-none rounded-lg border px-3 py-2 my-2 placeholder-grey-light focus:z-10 focus:border-red focus:outline-none focus:ring-red sm:text-sm"
|
className="relative block w-full appearance-none rounded-lg border px-3 py-2 my-2 placeholder-grey-light focus:z-10 focus:border-red focus:outline-none focus:ring-red sm:text-sm"
|
||||||
placeholder="Nom"
|
placeholder="Nom"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="firstname" className="sr-only">
|
<label htmlFor="firstName" className="sr-only">
|
||||||
Prénom
|
Prénom
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
id="firstname"
|
id="firstName"
|
||||||
name="firstname"
|
name="firstName"
|
||||||
type="text"
|
type="text"
|
||||||
autoComplete="firstname"
|
autoComplete="firstName"
|
||||||
required
|
required
|
||||||
className="relative block w-full appearance-none rounded-lg border px-3 py-2 my-2 placeholder-grey-light focus:z-10 focus:border-red focus:outline-none focus:ring-red sm:text-sm"
|
className="relative block w-full appearance-none rounded-lg border px-3 py-2 my-2 placeholder-grey-light focus:z-10 focus:border-red focus:outline-none focus:ring-red sm:text-sm"
|
||||||
placeholder="Prénom"
|
placeholder="Prénom"
|
||||||
|
|
|
||||||
|
|
@ -21,15 +21,15 @@
|
||||||
"jsx": "react-jsx",
|
"jsx": "react-jsx",
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": [
|
|
||||||
"src/*"
|
|
||||||
],
|
|
||||||
"@components/*": [
|
"@components/*": [
|
||||||
"src/components/*"
|
"src/components/*"
|
||||||
],
|
],
|
||||||
"@controllers/*": [
|
"@controllers/*": [
|
||||||
"src/controllers/*"
|
"src/controllers/*"
|
||||||
],
|
],
|
||||||
|
"@/*": [
|
||||||
|
"src/*"
|
||||||
|
],
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue