verivy email before log in
This commit is contained in:
@ -10,6 +10,7 @@ import CredentialsProvider from "next-auth/providers/credentials"
|
||||
import { PrismaAdapter } from "@auth/prisma-adapter"
|
||||
import bcrypt from "bcrypt"
|
||||
|
||||
const emailHelper = require('../../../src/helpers/email');
|
||||
//microsoft
|
||||
import AzureADProvider from "next-auth/providers/azure-ad";
|
||||
|
||||
@ -87,32 +88,51 @@ export const authOptions: NextAuthOptions = {
|
||||
const prisma = common.getPrismaClient();
|
||||
const user = await prisma.user.findUnique({ where: { email: credentials.username } });
|
||||
if (user) {
|
||||
const match = await bcrypt.compare(credentials?.password, user.passwordHashLocalAccount);
|
||||
if (match) {
|
||||
console.log("User authenticated successfully.");
|
||||
//create access token
|
||||
user.accessToken = await getAccessToken();
|
||||
|
||||
return user;
|
||||
if (!user.emailVerified) {
|
||||
const mailVerifyToken = await bcrypt.hash(credentials.username, 10);
|
||||
const date = new Date().getTime();
|
||||
const emailVerifyToken = date + "_" + mailVerifyToken;
|
||||
await prisma.user.update({
|
||||
where: { email: credentials.username },
|
||||
data: { emailVerifyToken: emailVerifyToken }
|
||||
});
|
||||
emailHelper.SendEmail_ValidateTemplate(credentials.username, emailVerifyToken);
|
||||
throw new Error('Моля потвърди имейла си преди да влезеш в системата.');
|
||||
}
|
||||
else {
|
||||
console.log("Password mismatch.");
|
||||
throw new Error('невалидна парола');
|
||||
const match = await bcrypt.compare(credentials?.password, user.passwordHashLocalAccount);
|
||||
if (match) {
|
||||
console.log("User authenticated successfully.");
|
||||
//create access token
|
||||
user.accessToken = await getAccessToken();
|
||||
|
||||
return user;
|
||||
}
|
||||
else {
|
||||
console.log("Password mismatch.");
|
||||
throw new Error('невалидна парола');
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
const pub = await prisma.publisher.findUnique({ where: { email: credentials.username } });
|
||||
if (pub) {
|
||||
const passHash = await bcrypt.hash(credentials.password, 10);
|
||||
const mailVerifyToken = await bcrypt.hash(pub.email, 10);
|
||||
const date = new Date().getTime();
|
||||
const emailVerifyToken = date + "_" + mailVerifyToken;
|
||||
const newUser = await prisma.user.create({
|
||||
data: {
|
||||
name: credentials.username,
|
||||
email: credentials.username,
|
||||
passwordHashLocalAccount: passHash
|
||||
passwordHashLocalAccount: passHash,
|
||||
emailVerifyToken: emailVerifyToken
|
||||
}
|
||||
});
|
||||
console.log("New local credential user created for publisher ", pub.firstName, " ", pub.lastName, " (", pub.email, ")");
|
||||
return newUser;
|
||||
emailHelper.SendEmail_ValidateTemplate(pub.email, emailVerifyToken, pub.firstName, pub.lastName);
|
||||
//return newUser;
|
||||
throw new Error("Моля проверете вашия имейл '" + credentials?.username + "' за да потвърдите регистрацията си.");
|
||||
}
|
||||
else {
|
||||
|
||||
|
Reference in New Issue
Block a user