mirror of
https://github.com/Ravinou/borgwarehouse
synced 2024-06-02 14:02:16 +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 fs from 'fs';
|
||||||
import path from 'path';
|
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
|
////Use if need getServerSideProps and therefore getServerSession
|
||||||
export const authOptions = {
|
export const authOptions = {
|
||||||
providers: [
|
providers: [
|
||||||
CredentialsProvider({
|
CredentialsProvider({
|
||||||
async authorize(credentials) {
|
async authorize(credentials, req) {
|
||||||
const { username, password } = credentials;
|
const { username, password } = credentials;
|
||||||
//Read the users file
|
//Read the users file
|
||||||
//Find the absolute path of the json directory
|
//Find the absolute path of the json directory
|
||||||
|
@ -44,6 +53,7 @@ export const authOptions = {
|
||||||
.map((user) => user.username)
|
.map((user) => user.username)
|
||||||
.indexOf(username.toLowerCase());
|
.indexOf(username.toLowerCase());
|
||||||
if (userIndex === -1) {
|
if (userIndex === -1) {
|
||||||
|
await logLogin(`Bad username ${req.body.username}`, req);
|
||||||
throw new Error('Incorrect credentials.');
|
throw new Error('Incorrect credentials.');
|
||||||
}
|
}
|
||||||
const user = usersList[userIndex];
|
const user = usersList[userIndex];
|
||||||
|
@ -51,6 +61,10 @@ export const authOptions = {
|
||||||
//Step 2 : Is the password correct ?
|
//Step 2 : Is the password correct ?
|
||||||
const isValid = await verifyPassword(password, user.password);
|
const isValid = await verifyPassword(password, user.password);
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
|
await logLogin(
|
||||||
|
`Wrong password for ${req.body.username}`,
|
||||||
|
req
|
||||||
|
);
|
||||||
throw new Error('Incorrect credentials.');
|
throw new Error('Incorrect credentials.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,6 +76,7 @@ export const authOptions = {
|
||||||
roles: user.roles,
|
roles: user.roles,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
await logLogin(req.body.username, req, true);
|
||||||
return account;
|
return account;
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in a new issue