auth with local credentials
This commit is contained in:
@ -8,6 +8,7 @@ import AppleProvider from "next-auth/providers/apple"
|
||||
import EmailProvider from "next-auth/providers/email"
|
||||
import CredentialsProvider from "next-auth/providers/credentials"
|
||||
import { PrismaAdapter } from "@auth/prisma-adapter"
|
||||
import bcrypt from "bcrypt"
|
||||
|
||||
//microsoft
|
||||
import AzureADProvider from "next-auth/providers/azure-ad";
|
||||
@ -16,6 +17,7 @@ import AzureADProvider from "next-auth/providers/azure-ad";
|
||||
|
||||
const common = require("../../../src/helpers/common");
|
||||
import { isLoggedIn, setAuthTokens, clearAuthTokens, getAccessToken, getRefreshToken } from 'axios-jwt'
|
||||
import { create } from "domain"
|
||||
|
||||
|
||||
console.log("appleID:", process.env.APPLE_APP_ID);
|
||||
@ -52,6 +54,7 @@ export const authOptions: NextAuthOptions = {
|
||||
// tenantId: process.env.AZURE_AD_TENANT_ID,
|
||||
// }),
|
||||
CredentialsProvider({
|
||||
id: 'credentials',
|
||||
// The name to display on the sign in form (e.g. 'Sign in with...')
|
||||
name: 'Credentials',
|
||||
credentials: {
|
||||
@ -80,17 +83,45 @@ export const authOptions: NextAuthOptions = {
|
||||
{ id: "3", name: "popov", email: "popov@example.com", password: "popov123", role: "ADMIN" }
|
||||
];
|
||||
|
||||
// Check if a user with the given username and password exists
|
||||
const user = users.find(user =>
|
||||
user.name === credentials.username && user.password === credentials.password
|
||||
);
|
||||
|
||||
// If a matching user is found, return the user data, otherwise return null
|
||||
if (user) {
|
||||
return user; //{ id: user.id, name: user.name, email: user.email };
|
||||
return user;
|
||||
}
|
||||
else {
|
||||
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 null;
|
||||
return user;
|
||||
}
|
||||
else {
|
||||
console.log("Password mismatch.");
|
||||
throw new Error('невалидна парола');
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new Error("Не можем да намерим твоя имейл '" + credentials?.username + "' в участниците в ССОМ. Моля свържи се с нас за да те регистрираме ако искаш да ползваш този имейл.");
|
||||
// console.log("Creating new user in the database...");
|
||||
// const passHash = await bcrypt.hash(credentials.password, 10);
|
||||
// const newUser = await prisma.user.create({
|
||||
// data: {
|
||||
// name: credentials.username,
|
||||
// email: credentials.username,
|
||||
// passwordHashLocalAccount: passHash
|
||||
// }
|
||||
// });
|
||||
// console.log("New user created in the database.");
|
||||
// return newUser;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
/*
|
||||
@ -132,35 +163,35 @@ export const authOptions: NextAuthOptions = {
|
||||
var prisma = common.getPrismaClient();
|
||||
|
||||
console.log("[nextauth] signIn:", account.provider, user.email)
|
||||
if (account.provider === 'google') {
|
||||
try {
|
||||
// Check user in your database and assign roles
|
||||
const dbUser = await prisma.publisher.findUnique({
|
||||
where: { email: user.email }
|
||||
//if (account.provider === 'google' ) {
|
||||
try {
|
||||
// Check user in your database and assign roles
|
||||
const dbUser = await prisma.publisher.findUnique({
|
||||
where: { email: user.email }
|
||||
});
|
||||
|
||||
if (dbUser) {
|
||||
// Assign roles from your database to the session
|
||||
user.role = dbUser.role;
|
||||
user.id = dbUser.id;
|
||||
//user.permissions = dbUser.permissions;
|
||||
const session = { ...user };
|
||||
|
||||
await prisma.publisher.update({
|
||||
where: { id: dbUser.id },
|
||||
data: { lastLogin: new Date() }
|
||||
});
|
||||
|
||||
if (dbUser) {
|
||||
// Assign roles from your database to the session
|
||||
user.role = dbUser.role;
|
||||
user.id = dbUser.id;
|
||||
//user.permissions = dbUser.permissions;
|
||||
const session = { ...user };
|
||||
|
||||
await prisma.publisher.update({
|
||||
where: { id: dbUser.id },
|
||||
data: { lastLogin: new Date() }
|
||||
});
|
||||
return true; // Sign-in successful
|
||||
} else {
|
||||
// Optionally create a new user in your DB
|
||||
// Or return false to deny access
|
||||
//Let's customize the error message to give a better user experience
|
||||
throw new Error(`Твоят имейл '${user.email}' не е регистриран в системата. Моля свържи се с нас за да те регистрираме ако искаш да ползваш този имейл.`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return true; // Sign-in successful
|
||||
} else {
|
||||
// Optionally create a new user in your DB
|
||||
// Or return false to deny access
|
||||
//Let's customize the error message to give a better user experience
|
||||
throw new Error(`Твоят имейл '${user.email}' не е регистриран в системата. Моля свържи се с нас за да те регистрираме ако искаш да ползваш този имейл.`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
//}
|
||||
|
||||
return true; // Allow other providers or default behavior
|
||||
},
|
||||
@ -207,6 +238,13 @@ export const authOptions: NextAuthOptions = {
|
||||
};
|
||||
},
|
||||
},
|
||||
pages: {
|
||||
signIn: "/auth/signin",
|
||||
signOut: "/auth/signout",
|
||||
error: "/message", // Error code passed in query string as ?error=
|
||||
verifyRequest: "/auth/verify-request", // (used for check email message)
|
||||
newUser: null // If set, new users will be directed here on first sign in
|
||||
},
|
||||
}
|
||||
|
||||
export default NextAuth(authOptions)
|
||||
export default NextAuth(authOptions)
|
Reference in New Issue
Block a user