mirror of
https://github.com/Ravinou/borgwarehouse
synced 2024-05-06 07:43:15 +02:00
feat: add specific logs for success and failed login
This commit is contained in:
parent
204c7fc384
commit
904207e48a
|
@ -5,11 +5,20 @@ import { verifyPassword } from '../../../helpers/functions/auth';
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const logLogin = async (message, req, success = false) => {
|
||||
const ipAddress = req.headers['x-forwarded-for'] || 'unknown';
|
||||
if (success) {
|
||||
console.log(`Login success from ${ipAddress} with user ${message}`);
|
||||
} else {
|
||||
console.log(`Login failed from ${ipAddress} : ${message}`);
|
||||
}
|
||||
};
|
||||
|
||||
////Use if need getServerSideProps and therefore getServerSession
|
||||
export const authOptions = {
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
async authorize(credentials) {
|
||||
async authorize(credentials, req) {
|
||||
const { username, password } = credentials;
|
||||
//Read the users file
|
||||
//Find the absolute path of the json directory
|
||||
|
@ -44,6 +53,7 @@ export const authOptions = {
|
|||
.map((user) => user.username)
|
||||
.indexOf(username.toLowerCase());
|
||||
if (userIndex === -1) {
|
||||
await logLogin(`Bad username ${req.body.username}`, req);
|
||||
throw new Error('Incorrect credentials.');
|
||||
}
|
||||
const user = usersList[userIndex];
|
||||
|
@ -51,6 +61,10 @@ export const authOptions = {
|
|||
//Step 2 : Is the password correct ?
|
||||
const isValid = await verifyPassword(password, user.password);
|
||||
if (!isValid) {
|
||||
await logLogin(
|
||||
`Wrong password for ${req.body.username}`,
|
||||
req
|
||||
);
|
||||
throw new Error('Incorrect credentials.');
|
||||
}
|
||||
|
||||
|
@ -62,6 +76,7 @@ export const authOptions = {
|
|||
roles: user.roles,
|
||||
};
|
||||
|
||||
await logLogin(req.body.username, req, true);
|
||||
return account;
|
||||
},
|
||||
}),
|
||||
|
|
Loading…
Reference in a new issue