mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-05-13 17:21:53 +00:00
feat(login / register form): Added validation and birthdate to form
This commit is contained in:
+59
-34
@@ -5,61 +5,86 @@ import {
|
||||
ButtonGroup,
|
||||
Button,
|
||||
Image,
|
||||
} from '@chakra-ui/react';
|
||||
import {useRouter} from 'next/router';
|
||||
import {signOut, useSession} from 'next-auth/react';
|
||||
Link,
|
||||
} from "@chakra-ui/react";
|
||||
import { useRouter } from "next/router";
|
||||
import { signOut, useSession } from "next-auth/react";
|
||||
import Nextlink from "next/link";
|
||||
|
||||
type Props = {
|
||||
variant: "static" | "fixed"
|
||||
}
|
||||
variant: "static" | "fixed";
|
||||
};
|
||||
|
||||
export default function Navbar({variant = "fixed"}: Props) {
|
||||
export default function Navbar({ variant = "fixed" }: Props) {
|
||||
const router = useRouter();
|
||||
const {data: session, status} = useSession();
|
||||
const { data: session, status } = useSession();
|
||||
|
||||
const RightSection = () => {
|
||||
if (status === "authenticated" && session.user) return (
|
||||
<Flex justify={'right'} flexBasis={'100%'}>
|
||||
<Text>{session.user.email}</Text>
|
||||
|
||||
if (status === "authenticated" && session.user)
|
||||
return (
|
||||
<Flex justify={"right"} flexBasis={"100%"}>
|
||||
<ButtonGroup>
|
||||
<Button colorScheme={'purple'} onClick={() => signOut()}>
|
||||
<Button colorScheme={"purple"} onClick={() => signOut()}>
|
||||
Déconnexion
|
||||
</Button>
|
||||
</ButtonGroup>
|
||||
</Flex>
|
||||
);
|
||||
else return (
|
||||
<Flex justify={'right'} flexBasis={'100%'}>
|
||||
);
|
||||
else
|
||||
return (
|
||||
<Flex justify={"right"} flexBasis={"100%"}>
|
||||
<ButtonGroup>
|
||||
<Button onClick={() => router.push("/register")}>
|
||||
Inscription
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => router.push('/register')}>Inscription</Button>
|
||||
<Button colorScheme={'purple'}
|
||||
onClick={() => router.push('/login')}>
|
||||
colorScheme={"purple"}
|
||||
onClick={() => router.push("/login")}
|
||||
>
|
||||
Connexion
|
||||
</Button>
|
||||
</ButtonGroup>
|
||||
</Flex>
|
||||
);
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Box position={variant} zIndex={9999} top={0} width={'100vw'}
|
||||
backdropFilter={'auto'} backdropBlur={'20px'} px={10} py={2}>
|
||||
<Flex align={'center'}>
|
||||
<Box flexBasis={'100%'}>
|
||||
<Image src={"/logo.svg"} h={"3rem"} objectFit={"contain"}/>
|
||||
</Box>
|
||||
<Box
|
||||
position={variant}
|
||||
zIndex={9999}
|
||||
top={0}
|
||||
width={"100vw"}
|
||||
backdropFilter={"auto"}
|
||||
backdropBlur={"20px"}
|
||||
px={10}
|
||||
py={2}
|
||||
>
|
||||
<Flex align={"center"}>
|
||||
<Box flexBasis={"100%"}>
|
||||
<Image src={"/logo.svg"} h={"3rem"} objectFit={"contain"} />
|
||||
</Box>
|
||||
|
||||
<Flex gap={5} justify={'center'} flexBasis={'100%'}>
|
||||
<Text>A propos</Text>
|
||||
<Text>Contact</Text>
|
||||
<Text>Aide</Text>
|
||||
</Flex>
|
||||
|
||||
<RightSection/>
|
||||
<Flex gap={5} justify={"center"} flexBasis={"100%"}>
|
||||
{status === "authenticated" ? (
|
||||
<>
|
||||
<Link href={"/dashboard"} color={"purple.500"}>
|
||||
Tableau de bord
|
||||
</Link>
|
||||
<Link href={"/userProfile"} color={"purple.500"}>
|
||||
Profile
|
||||
</Link>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Text>A propos</Text>
|
||||
<Text>Contact</Text>
|
||||
<Text>Aide</Text>
|
||||
</>
|
||||
)}
|
||||
</Flex>
|
||||
</Box>
|
||||
);
|
||||
|
||||
<RightSection />
|
||||
</Flex>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,91 +1,115 @@
|
||||
import {
|
||||
Box, Button,
|
||||
Box,
|
||||
Button,
|
||||
Flex,
|
||||
FormControl,
|
||||
FormLabel,
|
||||
Heading,
|
||||
Input,
|
||||
Container,
|
||||
} from '@chakra-ui/react';
|
||||
import {useState} from 'react';
|
||||
import {useRouter} from 'next/router';
|
||||
import {signIn, SignInResponse} from 'next-auth/react';
|
||||
FormErrorMessage,
|
||||
} from "@chakra-ui/react";
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/router";
|
||||
import { signIn, SignInResponse } from "next-auth/react";
|
||||
|
||||
import {LoginData} from '@/models/form/LoginData';
|
||||
import { LoginData } from "@/models/form/LoginData";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
export default function LoginForm() {
|
||||
const router = useRouter();
|
||||
const [loginData, setLoginData] = useState(new LoginData());
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [invalidInput, setInvalidInput] = useState(false);
|
||||
const [wrongPasswordError, setWrongPasswordError] = useState(false);
|
||||
|
||||
const buttonWidth = {base: '100%', md: 'unset'};
|
||||
const {
|
||||
handleSubmit,
|
||||
register,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm();
|
||||
|
||||
const handleInput = (data: any) => {
|
||||
setInvalidInput(false);
|
||||
setLoginData({...loginData, ...data});
|
||||
};
|
||||
const buttonWidth = { base: "100%", md: "unset" };
|
||||
|
||||
const handleSubmit = async () => {
|
||||
setIsLoading(true)
|
||||
signIn('credentials', {...loginData, redirect: false})
|
||||
.then((res: unknown) => {
|
||||
const {ok: connexionSuccess} = res as SignInResponse;
|
||||
const errorPassword = "Mot de passe incorrect";
|
||||
|
||||
if (!connexionSuccess) {
|
||||
const loginUser = async (value: LoginData) => {
|
||||
setIsLoading(true);
|
||||
signIn("credentials", { ...value, redirect: false }).then(
|
||||
(res: unknown) => {
|
||||
const { ok: connexionSuccess } = res as SignInResponse;
|
||||
setIsLoading(false);
|
||||
setInvalidInput(true);
|
||||
|
||||
if (connexionSuccess) router.push("/dashboard");
|
||||
else setWrongPasswordError(true);
|
||||
}
|
||||
else router.push('/dashboard');
|
||||
});
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Box flexBasis={'100%'}>
|
||||
<Container>
|
||||
<Heading textAlign={'center'} size={'2xl'}>Connexion</Heading>
|
||||
|
||||
{/* Email */}
|
||||
<FormControl mb={'1rem'} mt={'100px'} isInvalid={invalidInput}>
|
||||
<Box flexBasis={"100%"}>
|
||||
<Container>
|
||||
<form id="login_form" onSubmit={handleSubmit(loginUser)}>
|
||||
<Heading textAlign={"center"} size={"2xl"}>
|
||||
Connexion
|
||||
</Heading>
|
||||
|
||||
{/*Email*/}
|
||||
<FormControl mb={"1rem"} id={"email"} isInvalid={errors.email}>
|
||||
<FormLabel>Adresse email</FormLabel>
|
||||
<Input
|
||||
isInvalid={invalidInput}
|
||||
id={'email'}
|
||||
type={'email'}
|
||||
value={loginData.email}
|
||||
onChange={(evt) => handleInput({email: evt.target.value})}
|
||||
placeholder={'adresse@email.com'}
|
||||
required={true}
|
||||
type="text"
|
||||
{...register("email", {
|
||||
required: { value: true, message: "Adresse email requise" },
|
||||
pattern: {
|
||||
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
|
||||
message: "Adresse email invalide",
|
||||
},
|
||||
})}
|
||||
placeholder="adresse@email.com"
|
||||
/>
|
||||
<FormErrorMessage>{errors.email?.message}</FormErrorMessage>
|
||||
</FormControl>
|
||||
|
||||
{/* Mot de passe */
|
||||
}
|
||||
<FormControl mb={'1rem'}>
|
||||
<FormLabel>Mot de passe</FormLabel>
|
||||
<Input
|
||||
isInvalid={invalidInput}
|
||||
id={'password'}
|
||||
type={'password'}
|
||||
value={loginData.password}
|
||||
onChange={(evt) => handleInput({password: evt.target.value})}
|
||||
placeholder={'Mot de passe'}
|
||||
required={true}
|
||||
/>
|
||||
{/*Mot de passe*/}
|
||||
<FormControl
|
||||
mb={"1rem"}
|
||||
id="password"
|
||||
isInvalid={errors.password || wrongPasswordError}
|
||||
>
|
||||
<FormLabel>Mot de passe</FormLabel>
|
||||
<Input
|
||||
type="password"
|
||||
placeholder="Mot de passe"
|
||||
{...register("password", {
|
||||
required: { value: true, message: "Mot de passe requis" },
|
||||
})}
|
||||
/>
|
||||
<FormErrorMessage>{errors.password?.message}</FormErrorMessage>
|
||||
<FormErrorMessage>
|
||||
{wrongPasswordError && errorPassword}
|
||||
</FormErrorMessage>
|
||||
</FormControl>
|
||||
|
||||
{/*Boutons*/
|
||||
}
|
||||
<Flex justify={'center'} mt={'50px'} gap={5}
|
||||
justifyContent={'space-between'}>
|
||||
<Button onClick={() => router.push('/')}
|
||||
w={buttonWidth}>Retour</Button>
|
||||
<Button isLoading={isLoading} onClick={handleSubmit}
|
||||
w={buttonWidth} colorScheme="purple">Connexion</Button>
|
||||
{/*Boutons*/}
|
||||
<Flex
|
||||
justify={"center"}
|
||||
mt={"50px"}
|
||||
gap={5}
|
||||
justifyContent={"space-between"}
|
||||
>
|
||||
<Button onClick={() => router.push("/")} w={buttonWidth}>
|
||||
Retour
|
||||
</Button>
|
||||
<Button
|
||||
isLoading={isLoading}
|
||||
w={buttonWidth}
|
||||
colorScheme="purple"
|
||||
type="submit"
|
||||
>
|
||||
Connexion
|
||||
</Button>
|
||||
</Flex>
|
||||
</form>
|
||||
</Container>
|
||||
</Box>
|
||||
)
|
||||
|
||||
}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,135 +1,192 @@
|
||||
import {
|
||||
Box, Button,
|
||||
Box,
|
||||
Button,
|
||||
Flex,
|
||||
FormControl,
|
||||
FormLabel,
|
||||
Heading,
|
||||
Input,
|
||||
Container,
|
||||
} from '@chakra-ui/react';
|
||||
import {useRouter} from 'next/router';
|
||||
import {useState} from 'react';
|
||||
import {signIn, SignInResponse} from 'next-auth/react';
|
||||
import {RegisterData} from '@/models/form/RegisterData';
|
||||
FormErrorMessage,
|
||||
} from "@chakra-ui/react";
|
||||
import { useRouter } from "next/router";
|
||||
import { useState } from "react";
|
||||
import { signIn, SignInResponse } from "next-auth/react";
|
||||
import { RegisterData } from "@/models/form/RegisterData";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
export default function RegisterForm() {
|
||||
const router = useRouter();
|
||||
const [registerData, setRegisterData] = useState(new RegisterData());
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [invalidInput, setInvalidInput] = useState(false);
|
||||
|
||||
const buttonWidth = {base: '100%', md: 'unset'};
|
||||
const {
|
||||
handleSubmit,
|
||||
register,
|
||||
watch,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm();
|
||||
|
||||
const handleInput = (data: any) => {
|
||||
setInvalidInput(false);
|
||||
setRegisterData({...registerData, ...data});
|
||||
const buttonWidth = { base: "100%", md: "unset" };
|
||||
|
||||
const validateDate = (value: string) => {
|
||||
const selected = new Date(value).getFullYear();
|
||||
const now = new Date().getFullYear();
|
||||
return now - selected >= 18 || "Vous devez avoir 18 ans ou plus";
|
||||
};
|
||||
|
||||
const handleSubmit = () => {
|
||||
let {email, firstName, lastName, password, confirmPassword} = registerData;
|
||||
if (password !== confirmPassword) setInvalidInput(true);
|
||||
const registerUser = (values: RegisterData) => {
|
||||
let { email, firstName, lastName, birthdate, password, confirmPassword } =
|
||||
values;
|
||||
const date = new Date(birthdate);
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({email, firstName, lastName, password}),
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
birthdate: date,
|
||||
}),
|
||||
};
|
||||
|
||||
setIsLoading(true);
|
||||
fetch('/api/users', options).then(() => {
|
||||
signIn('credentials', {email, password, redirect: false})
|
||||
.then((res: unknown) => {
|
||||
const {ok: connexionSuccess} = res as SignInResponse;
|
||||
fetch("/api/users", options)
|
||||
.then(() => {
|
||||
signIn("credentials", { email, password, redirect: false }).then(
|
||||
(res: unknown) => {
|
||||
const { ok: connexionSuccess } = res as SignInResponse;
|
||||
|
||||
// TODO If success -> goto interactive form else login
|
||||
router.push(connexionSuccess ? '/' : '/login');
|
||||
});
|
||||
}).catch(() => {
|
||||
setIsLoading(false);
|
||||
setInvalidInput(true);
|
||||
setRegisterData({...registerData, password: '', confirmPassword: ''});
|
||||
});
|
||||
router.push(connexionSuccess ? "/dashboard" : "/");
|
||||
}
|
||||
);
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
|
||||
return (
|
||||
<Box flexBasis={'100%'}>
|
||||
<Container>
|
||||
<Heading textAlign={'center'} size={'2xl'}>Inscription</Heading>
|
||||
<Box flexBasis={"100%"}>
|
||||
<Container>
|
||||
<form id="register_form" onSubmit={handleSubmit(registerUser)}>
|
||||
<Heading textAlign={"center"} size={"2xl"}>
|
||||
Inscription
|
||||
</Heading>
|
||||
|
||||
<Flex mt={'100px'} mb={'1rem'} gap={5}>
|
||||
<Flex mt={"100px"} mb={"1rem"} gap={5}>
|
||||
{/*Prénom*/}
|
||||
<FormControl>
|
||||
<FormControl id="firstName" isInvalid={errors.firstName}>
|
||||
<FormLabel>Prénom</FormLabel>
|
||||
<Input
|
||||
id="firstName"
|
||||
type="text"
|
||||
value={registerData.firstName}
|
||||
onChange={(evt) => handleInput({firstName: evt.target.value})}
|
||||
placeholder="Prénom"
|
||||
required={true}
|
||||
type="text"
|
||||
placeholder="Prénom"
|
||||
{...register("firstName", {
|
||||
required: { value: true, message: "Prénom requis" },
|
||||
})}
|
||||
/>
|
||||
<FormErrorMessage>{errors.firstName?.message}</FormErrorMessage>
|
||||
</FormControl>
|
||||
|
||||
{/*Nom*/}
|
||||
<FormControl>
|
||||
<FormControl id="lastName" isInvalid={errors.lastName}>
|
||||
<FormLabel>Nom</FormLabel>
|
||||
<Input
|
||||
id="lastName"
|
||||
type="text"
|
||||
value={registerData.lastName}
|
||||
onChange={(evt) => handleInput({lastName: evt.target.value})}
|
||||
placeholder="Nom"
|
||||
required={true}
|
||||
type="text"
|
||||
placeholder="Nom"
|
||||
{...register("lastName", {
|
||||
required: { value: true, message: "Nom requis" },
|
||||
})}
|
||||
/>
|
||||
<FormErrorMessage>{errors.lastName?.message}</FormErrorMessage>
|
||||
</FormControl>
|
||||
</Flex>
|
||||
|
||||
{/*Email*/}
|
||||
<FormControl mb={'1rem'} isInvalid={invalidInput}>
|
||||
<FormControl mb={"1rem"} id={"email"} isInvalid={errors.email}>
|
||||
<FormLabel>Adresse email</FormLabel>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
value={registerData.email}
|
||||
onChange={(evt) => handleInput({email: evt.target.value})}
|
||||
placeholder="adresse@email.com"
|
||||
type="text"
|
||||
{...register("email", {
|
||||
required: { value: true, message: "Adresse email requise" },
|
||||
pattern: {
|
||||
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
|
||||
message: "Adresse email invalide",
|
||||
},
|
||||
})}
|
||||
placeholder="adresse@email.com"
|
||||
/>
|
||||
<FormErrorMessage>{errors.email?.message}</FormErrorMessage>
|
||||
</FormControl>
|
||||
|
||||
{/*Date de naissance*/}
|
||||
<FormControl
|
||||
mb={"1rem"}
|
||||
id={"birthdate"}
|
||||
isInvalid={errors.birthdate}
|
||||
>
|
||||
<FormLabel>Date de naissance</FormLabel>
|
||||
<Input
|
||||
type="date"
|
||||
{...register("birthdate", {
|
||||
required: { value: true, message: "Date de naissance requise" },
|
||||
validate: validateDate,
|
||||
})}
|
||||
/>
|
||||
<FormErrorMessage>{errors.birthdate?.message}</FormErrorMessage>
|
||||
</FormControl>
|
||||
|
||||
{/*Mot de passe*/}
|
||||
<FormControl mb={'1rem'} isInvalid={invalidInput}>
|
||||
<FormControl mb={"1rem"} id="password" isInvalid={errors.password}>
|
||||
<FormLabel>Mot de passe</FormLabel>
|
||||
<Input
|
||||
id="password"
|
||||
type="password"
|
||||
value={registerData.password}
|
||||
onChange={(evt) => handleInput({password: evt.target.value})}
|
||||
placeholder="Mot de passe"
|
||||
type="password"
|
||||
placeholder="Mot de passe"
|
||||
{...register("password", {
|
||||
required: { value: true, message: "Mot de passe requis" },
|
||||
})}
|
||||
/>
|
||||
<FormErrorMessage>{errors.password?.message}</FormErrorMessage>
|
||||
</FormControl>
|
||||
|
||||
{/*Mot de passe (2)*/}
|
||||
<FormControl mb={'1rem'} isInvalid={invalidInput}>
|
||||
<FormControl
|
||||
mb={"1rem"}
|
||||
id="confirmPassword"
|
||||
isInvalid={errors.confirmPassword}
|
||||
>
|
||||
<FormLabel>Confirmation du mot de passe</FormLabel>
|
||||
<Input
|
||||
id="confirmPassword"
|
||||
type="password"
|
||||
value={registerData.confirmPassword}
|
||||
onChange={(evt) => handleInput(
|
||||
{confirmPassword: evt.target.value})}
|
||||
placeholder="Mot de passe"
|
||||
type="password"
|
||||
placeholder="Mot de passe"
|
||||
{...register("confirmPassword", {
|
||||
required: { value: true, message: "Confirmation requise" },
|
||||
validate: (value: string) => {
|
||||
return (
|
||||
watch("password") === value ||
|
||||
"Les mots de passe ne correspondent pas"
|
||||
);
|
||||
},
|
||||
})}
|
||||
/>
|
||||
<FormErrorMessage>
|
||||
{errors.confirmPassword?.message}
|
||||
</FormErrorMessage>
|
||||
</FormControl>
|
||||
|
||||
|
||||
<Flex mt={'50px'} w={'100%'} justify={'space-between'} gap={5}>
|
||||
<Button onClick={() => router.push('/')}
|
||||
w={buttonWidth}>Retour</Button>
|
||||
<Button isLoading={isLoading} onClick={handleSubmit} w={buttonWidth} colorScheme="purple">Je
|
||||
m'inscris</Button>
|
||||
<Flex mt={"50px"} w={"100%"} justify={"space-between"} gap={5}>
|
||||
<Button onClick={() => router.push("/")} w={buttonWidth}>
|
||||
Retour
|
||||
</Button>
|
||||
<Button
|
||||
isLoading={isSubmitting}
|
||||
w={buttonWidth}
|
||||
colorScheme="purple"
|
||||
type="submit"
|
||||
>
|
||||
Je m'inscris
|
||||
</Button>
|
||||
</Flex>
|
||||
</Container>
|
||||
</Box>
|
||||
</form>
|
||||
</Container>
|
||||
</Box>
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
export class RegisterData {
|
||||
|
||||
constructor(
|
||||
public firstName: string = "",
|
||||
public lastName: string = "",
|
||||
public email: string = "",
|
||||
public password: string = "",
|
||||
public confirmPassword: string = "",
|
||||
public firstName: string = "",
|
||||
public lastName: string = "",
|
||||
public email: string = "",
|
||||
public birthdate: string = "",
|
||||
public password: string = "",
|
||||
public confirmPassword: string = ""
|
||||
) {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user