You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
The_Tip_Top/pages/connexion.tsx

115 lines
3.7 KiB

import React from "react";
import Head from "next/head";
import { signIn } from "next-auth/react";
import { useForm } from "react-hook-form";
import { AiOutlineGoogle } from 'react-icons/ai'
import style from '../styles/Connexion.module.css';
import { SiGnuprivacyguard } from 'react-icons/si'
import { toast } from "react-toastify";
type LoginSubmitForm = {
email: string,
acceptTerms: boolean
};
const Connexion = () => {
const {
register,
handleSubmit,
watch,
trigger,
formState: { errors }
} = useForm<LoginSubmitForm>();
const acceptTermCheckBox = watch("acceptTerms")
const connexionWithGoogle = async () => {
if (onCheckTerms()) {
signIn("google", { callbackUrl: `${window.location.origin}/mon-compte` })
}
await trigger("acceptTerms");
}
const onMailSubmit = async (data: LoginSubmitForm) => {
if (onCheckTerms()) {
signIn("email", { email: data.email, callbackUrl: `${window.location.origin}/mon-compte` })
}
await trigger("acceptTerms");
await trigger("email");
}
const onCheckTerms = () => {
if (acceptTermCheckBox) {
return true;
} else {
toast.error('Vous devez accepter les conditions obligatoires', {
position: "top-center",
autoClose: 2000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "light",
});
return false
}
}
return (
<>
<Head>
<title>Connexion</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
</Head>
<div className={`${style['masthead']} d-flex justify-content-center align-items-center text-center`}>
<div className="container align-items-center d-flex flex-column">
<div className={`${style['bloc-content']} col-12 col-md-6`}>
<h1 className="fw-light">Connexion</h1>
<SiGnuprivacyguard size={50} />
<form onSubmit={handleSubmit(onMailSubmit)}>
<div className="form-group mx-auto my-3 w-100">
<input
type="email" id="email" name="email"
className={`mb-3 form-control ${errors.email ? 'is-invalid' : ''}`}
placeholder="Email..."
{...register("email")}
/>
<div className="d-grid">
<button type="submit" className="btn d-grid btn-primary">
Se connecter avec mon email
</button>
</div>
</div>
<hr />
<div className="d-grid">
<button onClick={connexionWithGoogle} type="button" className={`btn btn-primary ${style.loginButton}`} style={{ backgroundColor: "#dd4b39" }}>
<span className={style.logo}><AiOutlineGoogle /></span>Se connecter avec google
</button>
</div>
<div className="form-group my-1">
<label
htmlFor="acceptTerms"
className="form-check-label"
>
J'ai lu et j'accepte les
conditions
</label>
<input
type="checkbox"
className={`form-check-input mx-1 ${errors.acceptTerms ? 'is-invalid' : ''}`}
{...register("acceptTerms")}
name="acceptTerms"
id="acceptTerms"
/>
</div>
</form>
</div >
</div>
</div>
</>)
};
export default Connexion;