Merge branch 'master' of https://github.com/Eric-ciccotti/rest-nextjs-api-routes-auth into feature/cedric
This commit is contained in:
commit
d480a3f8b2
11 files changed
+216
-101
No files matched your search
@@ -18,28 +18,35 @@ const options = {
|
||||
authorization: 'https://accounts.google.com/o/oauth2/v2/auth?prompt=consent&access_type=offline&response_type=code',
|
||||
}),
|
||||
EmailProvider({
|
||||
server: process.env.EMAIL_SERVER,
|
||||
from: process.env.EMAIL_FROM,
|
||||
server: {
|
||||
host: process.env.EMAIL_SERVER_HOST,
|
||||
port: parseInt(process.env.EMAIL_SERVER_PORT),
|
||||
auth: {
|
||||
user: process.env.EMAIL_SERVER_USER,
|
||||
pass: process.env.EMAIL_SERVER_PASSWORD
|
||||
}
|
||||
},
|
||||
from: process.env.EMAIL_FROM
|
||||
}),
|
||||
],
|
||||
adapter: PrismaAdapter(prisma),
|
||||
secret: process.env.SECRET,
|
||||
callbacks: {
|
||||
async session({ session, user }) {
|
||||
|
||||
const getCurrentUser = await prisma.user.findMany({
|
||||
where: { email: session.user.email },
|
||||
})
|
||||
//récupération du role
|
||||
const { role } = getCurrentUser[0];
|
||||
|
||||
session.user.role = role
|
||||
|
||||
|
||||
|
||||
return session
|
||||
}
|
||||
},
|
||||
error: '/error',
|
||||
|
||||
pages: {
|
||||
// signIn: '/auth/signin',
|
||||
// signOut: '/auth/signout',
|
||||
// error: '/auth/error', // Error code passed in query string as ?error=
|
||||
verifyRequest: '/verify-request', // (used for check email message)
|
||||
// newUser: '/auth/new-user' // New users will be directed here on first sign in (leave the property out if not of interest)
|
||||
}
|
||||
}
|
||||
+101
-87
@@ -4,7 +4,7 @@ import prisma from '../lib/prisma'
|
||||
import Image from 'next/image';
|
||||
import * as Yup from 'yup';
|
||||
import Head from "next/head";
|
||||
import { signIn, useSession } from "next-auth/react";
|
||||
import { getCsrfToken, signIn, useSession } from "next-auth/react";
|
||||
import { yupResolver } from "@hookform/resolvers/yup";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
@@ -21,31 +21,42 @@ 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 = () => {
|
||||
const MainPage = ({ csrfToken }: Props) => {
|
||||
const { data: session } = useSession();
|
||||
|
||||
const validationSchema = Yup.object().shape({
|
||||
email: Yup.string().email(),
|
||||
acceptTerms: Yup.bool().oneOf(
|
||||
[true],
|
||||
"Accepter les conditions est obligatoire"
|
||||
)
|
||||
// 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,
|
||||
@@ -55,37 +66,63 @@ const MainPage = () => {
|
||||
});
|
||||
|
||||
|
||||
const term = watch("acceptTerms")
|
||||
const acceptTermCheckBox = watch("acceptTerms")
|
||||
|
||||
|
||||
const connexionWithGoogle = async () => {
|
||||
if (term) {
|
||||
signIn("google");
|
||||
if (onCheckTerms()) {
|
||||
signIn("google")
|
||||
}
|
||||
//relance une vérification de la checkbox
|
||||
await trigger("acceptTerms");
|
||||
}
|
||||
|
||||
const onSubmit = async (data: LoginSubmitForm) => {
|
||||
|
||||
const onMailSubmit = async (data: LoginSubmitForm) => {
|
||||
if (onCheckTerms()) {
|
||||
signIn("email", { email: data.email, callbackUrl: `${window.location.origin}/create` })
|
||||
}
|
||||
await trigger("acceptTerms");
|
||||
await trigger("email");
|
||||
}
|
||||
|
||||
|
||||
// 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);
|
||||
// }
|
||||
};
|
||||
|
||||
const onCheckTerms = (): any => {
|
||||
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",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 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>
|
||||
@@ -93,70 +130,47 @@ const MainPage = () => {
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
|
||||
</Head>
|
||||
|
||||
<div className={`${style['bloc-content']} col-12 col-lg-6`}>
|
||||
|
||||
|
||||
<h1 className="fw-light">Connexion</h1>
|
||||
|
||||
<SiGnuprivacyguard size={50} />
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<form onSubmit={handleSubmit(onMailSubmit)}>
|
||||
<div className="form-group mx-auto my-3 w-100">
|
||||
|
||||
<div className="mb-3">
|
||||
|
||||
<input
|
||||
type="email"
|
||||
className={`form-control ${errors.email ? 'is-invalid' : ''}`}
|
||||
placeholder="Email..."
|
||||
|
||||
{...register("email")}
|
||||
/>
|
||||
</div>
|
||||
<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 btn-primary">
|
||||
<button type="submit" className="btn d-grid btn-primary">
|
||||
Se connecter avec mon email
|
||||
</button>
|
||||
</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-check">
|
||||
<label
|
||||
htmlFor="acceptTerms"
|
||||
className="form-check-label"
|
||||
>
|
||||
J'ai lu et j'accepte les
|
||||
conditions
|
||||
</label>
|
||||
<input
|
||||
type="checkbox"
|
||||
className="form-check-input"
|
||||
{...register("acceptTerms")}
|
||||
name="acceptTerms"
|
||||
/>
|
||||
|
||||
</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 className="invalid-feedback alert alert-danger">{errors.acceptTerms?.message}</div>
|
||||
<div className="invalid-feedback alert alert-danger">{errors.email?.message}</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
</div >
|
||||
</>)
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import React, { useEffect } 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 { AiOutlineCheck } 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 { GrValidate } from 'react-icons/gr'
|
||||
import { toast } from "react-toastify";
|
||||
import { Token } from "nodemailer/lib/xoauth2";
|
||||
import { isGeneratorFunction } from "util/types";
|
||||
|
||||
|
||||
|
||||
const VerifyRequest = () => {
|
||||
|
||||
//va rediriger après 3 seconde
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
Router.push("/")
|
||||
}, 3000)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title>Page Confirmation email</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
|
||||
</Head>
|
||||
<div className={`${style['bloc-content']} col-12 col-lg-6`}>
|
||||
<h1 className="fw-light">Un email a été envoyé</h1>
|
||||
<GrValidate size={100} />
|
||||
<div className="d-grid mt-4">
|
||||
<button onClick={() => Router.push("/")} type="button" className={`btn btn-primary`}>
|
||||
Cliquez ici si vous n'êtes pas rediriger à l'accueil
|
||||
</button>
|
||||
</div>
|
||||
</div >
|
||||
</>)
|
||||
};
|
||||
|
||||
export default VerifyRequest;
|
||||
|
||||
|
||||
Reference in new issue
Block a user