feat: create user if there's no user in db

This commit is contained in:
Sonny
2023-04-16 19:52:52 +02:00
parent 9db1f93d05
commit 8e893ccc79

View File

@@ -1,64 +1,99 @@
import NextAuth from 'next-auth'; import { PrismaClient } from "@prisma/client";
import GoogleProvider from 'next-auth/providers/google'; import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient(); const prisma = new PrismaClient();
export default NextAuth({ export default NextAuth({
providers: [ providers: [
GoogleProvider({ GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID, clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET, clientSecret: process.env.GOOGLE_CLIENT_SECRET,
authorization: { authorization: {
params: { params: {
prompt: 'consent', prompt: "consent",
access_type: 'offline', access_type: "offline",
response_type: 'code' response_type: "code",
} },
} },
}) }),
], ],
callbacks: { callbacks: {
async signIn({ account: accountParam, profile }) { // TODO: Auth async signIn({ account: accountParam, profile }) {
console.log('Connexion via', accountParam.provider, accountParam.providerAccountId, profile.email, profile.name) // TODO: Auth
if (accountParam.provider !== 'google') { console.log(
return '/signin?error=' + encodeURI('Authentitifcation via Google requise'); "Connexion via",
} accountParam.provider,
accountParam.providerAccountId,
const email = profile?.email; profile.email,
if (email === '') { profile.name
return '/signin?error=' + encodeURI('Impossible de récupérer l\'email associé à ce compte Google'); );
} if (accountParam.provider !== "google") {
return (
const googleId = profile?.sub; "/signin?error=" +
if (googleId === '') { encodeURI("Authentitifcation via Google requise")
return '/signin?error=' + encodeURI('Impossible de récupérer l\'identifiant associé à ce compte Google'); );
} }
const email = profile?.email;
try { if (email === "") {
const account = await prisma.user.findFirst({ return (
where: { "/signin?error=" +
google_id: googleId, encodeURI(
email "Impossible de récupérer l'email associé à ce compte Google"
} )
}); );
}
if (!account) { const googleId = profile?.sub;
return '/signin?error=' + encodeURI('Vous n\'êtes pas autorisé à vous connecter avec ce compte Google'); if (googleId === "") {
} else { return (
return true; "/signin?error=" +
} encodeURI(
} catch (error) { "Impossible de récupérer l'identifiant associé à ce compte Google"
console.error(error); )
return '/signin?error=' + encodeURI('Une erreur est survenue lors de l\'authentification'); );
} }
} try {
}, const account = await prisma.user.findFirst({
pages: { where: {
signIn: '/signin', google_id: googleId,
error: '/signin' email,
}, },
session: { });
maxAge: 60 * 60 * 6 // Session de 6 heures const accountCount = await prisma.user.count();
} if (!account) {
if (accountCount === 0) {
await prisma.user.create({
data: {
email,
google_id: googleId,
},
});
return true;
}
return (
"/signin?error=" +
encodeURI(
"Vous n'êtes pas autorisé à vous connecter avec ce compte Google"
)
);
} else {
return true;
}
} catch (error) {
console.error(error);
return (
"/signin?error=" +
encodeURI(
"Une erreur est survenue lors de l'authentification"
)
);
}
},
},
pages: {
signIn: "/signin",
error: "/signin",
},
session: {
maxAge: 60 * 60 * 6, // Session de 6 heures
},
}); });