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"; 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({ resolver: yupResolver(validationSchema) }); const term = watch("acceptTerms") const connexionWithGoogle = async () => { clearErrors("acceptTerms") if (term) { signIn("google"); } else { toast.error('Vous devez accepter les conditions obligatoire', { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", }); //relance une vérification de la checkbox // await trigger("acceptTerms"); } } const onSubmit = async (data: LoginSubmitForm) => { if (term && !errors.email?.message) { console.log("email", data.email); signIn("email", { email: data.email, callbackUrl: `${window.location.origin}/create` }) } else { toast.error('Vous devez accepter les conditions obligatoire', { position: "top-center", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: "light", }); } } // 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 ( <> Connexion

Connexion

{errors.email?.message}

{/*
{errors.acceptTerms?.message}
*/}
) }; export default MainPage;