feat : Implement last las login feature

feat : add transaction password column and write logic for transaction password.
This commit is contained in:
2025-07-22 12:49:44 +05:30
parent 32f6430a5c
commit d4d741b8cc
10 changed files with 141 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
const authService = require('../services/auth.service');
const { generateToken } = require('../util/jwt');
const { logger } = require('../util/logger');
const db = require('../config/db');
async function login(req, res) {
const { customerNo, password } = req.body;
@@ -10,18 +11,36 @@ async function login(req, res) {
.status(400)
.json({ error: 'customerNo and password are required' });
}
const currentTime = new Date().toISOString();
try {
const user = await authService.validateUser(customerNo, password);
if (!user) return res.status(401).json({ error: 'invalid credentials' });
const token = generateToken(user.customer_no, '1d');
res.json({ token });
const FirstTimeLogin = await authService.CheckFirstTimeLogin(customerNo);
await db.query('UPDATE users SET last_login = $1 WHERE customer_no = $2', [
currentTime,
customerNo,
]);
res.json({ token, FirstTimeLogin });
} catch (err) {
logger.error(err, 'login failed');
res.status(500).json({ error: 'something went wrong' });
}
}
async function fetchUserDetails(req, res) {
const customerNo = req.user;
try {
const user = await authService.findUserByCustomerNo(customerNo);
if (!user) return res.status(404).json({ message: 'USER_NOT_FOUND' });
return res.json(user);
} catch (err) {
logger.error(err, 'error occured while fetching user details');
res.status(500).json({ error: 'something went wrong' });
}
}
async function tpin(req, res) {
const customerNo = req.user;
try {
@@ -56,4 +75,31 @@ async function setTpin(req, res) {
}
}
module.exports = { login, tpin, setTpin };
async function setLoginPassword(req, res) {
const customerNo = req.user;
try {
const user = await authService.findUserByCustomerNo(customerNo);
if (!user) return res.status(404).json({ error: 'USER_NOT_FOUND' });
const { login_password } = req.body;
authService.setLoginPassword(customerNo, login_password);
return res.json({ message: 'Login Password set' });
} catch (error) {
logger.error(error);
return res.status(500).json({ error: 'SOMETHING_WENT_WRONG' });
}
}
async function setTransactionPassword(req, res) {
const customerNo = req.user;
try {
const user = await authService.findUserByCustomerNo(customerNo);
if (!user) return res.status(404).json({ error: 'USER_NOT_FOUND' });
const { transaction_password } = req.body;
authService.setTransactionPassword(customerNo, transaction_password);
return res.json({ message: 'Transaction Password set' });
} catch (error) {
logger.error(error);
return res.status(500).json({ error: 'SOMETHING_WENT_WRONG' });
}
}
module.exports = { login, tpin, setTpin, setLoginPassword, setTransactionPassword,fetchUserDetails };