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

187 lines
5.0 KiB

import React from "react";
import prisma from '../lib/prisma'
import Image from 'next/image';
import * as Yup from 'yup';
import Head from "next/head";
import { getCsrfToken, signIn, useSession } from "next-auth/react";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm } from "react-hook-form";
import { AiOutlineGoogle } from 'react-icons/ai'
import Router from "next/router";
import style from '../styles/Connexion.module.css';
import imageConnexion from '../public/images/connexion_image.png'
import Link from "next/link";
import { SiGnuprivacyguard } from 'react-icons/si'
import { toast } from "react-toastify";
import { Token } from "nodemailer/lib/xoauth2";
import { isGeneratorFunction } from "util/types";
export async function getServerSideProps(context) {
const csrfToken = await getCsrfToken(context)
return {
props: { csrfToken },
}
}
type LoginSubmitForm = {
email: string,
acceptTerms: boolean
};
type Props = {
csrfToken: Token
}
const MainPage = ({ csrfToken }: Props) => {
const { data: session } = useSession();
const validationSchema = Yup.object().shape({
// email: Yup.string().email(
// "Veuillez rentrer une adresse mail valide"
// ),
// acceptTerms: Yup.bool().oneOf(
// [true],
// "Vous devez accepter les conditions obligatoire"
// )
})
const {
register,
clearErrors,
handleSubmit,
watch,
trigger,
formState: { errors }
} = useForm<LoginSubmitForm>({
resolver: yupResolver(validationSchema)
});
const acceptTermCheckBox = watch("acceptTerms")
const connexionWithGoogle = async () => {
if (onCheckTerms()) {
signIn("google")
}
await trigger("acceptTerms");
}
const onMailSubmit = async (data: LoginSubmitForm) => {
if (onCheckTerms()) {
signIn("email", { email: data.email, callbackUrl: `${window.location.origin}/create` })
}
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
}
}
// try {
// const body = { bioValue: data.bio };
// await fetch(`/api/profile`, {
// method: "POST",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify(body),
// })
// .then((res) => res.json())
// .then((data) => {
// setResponse(data);
// console.log('response', data);
// })
// await Router.push("/profile");
// } catch (error) {
// console.error(error);
// }
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-lg-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">
<label
htmlFor="acceptTerms"
className="form-check-label"
>
J'ai lu et j'accepte les
conditions
</label>
<input
type="checkbox"
className={`form-check-input ${errors.acceptTerms ? 'is-invalid' : ''}`}
{...register("acceptTerms")}
name="acceptTerms"
/>
</div>
</form>
</div >
</div>
</div>
</>)
};
export default MainPage;