Add:Log user and ip on successful login #1740

This commit is contained in:
advplyr 2023-04-28 16:16:47 -05:00
parent ff8a58c7bc
commit 4db26f9f79

View File

@ -126,12 +126,12 @@ class Auth {
async login(req, res) { async login(req, res) {
const ipAddress = requestIp.getClientIp(req) const ipAddress = requestIp.getClientIp(req)
var username = (req.body.username || '').toLowerCase() const username = (req.body.username || '').toLowerCase()
var password = req.body.password || '' const password = req.body.password || ''
var user = this.users.find(u => u.username.toLowerCase() === username) const user = this.users.find(u => u.username.toLowerCase() === username)
if (!user || !user.isActive) { if (!user?.isActive) {
Logger.warn(`[Auth] Failed login attempt ${req.rateLimit.current} of ${req.rateLimit.limit} from ${ipAddress}`) Logger.warn(`[Auth] Failed login attempt ${req.rateLimit.current} of ${req.rateLimit.limit} from ${ipAddress}`)
if (req.rateLimit.remaining <= 2) { if (req.rateLimit.remaining <= 2) {
Logger.error(`[Auth] Failed login attempt for username ${username} from ip ${ipAddress}. Attempts: ${req.rateLimit.current}`) Logger.error(`[Auth] Failed login attempt for username ${username} from ip ${ipAddress}. Attempts: ${req.rateLimit.current}`)
@ -145,13 +145,15 @@ class Auth {
if (password) { if (password) {
return res.status(401).send('Invalid root password (hint: there is none)') return res.status(401).send('Invalid root password (hint: there is none)')
} else { } else {
Logger.info(`[Auth] ${user.username} logged in from ${ipAddress}`)
return res.json(this.getUserLoginResponsePayload(user)) return res.json(this.getUserLoginResponsePayload(user))
} }
} }
// Check password match // Check password match
var compare = await bcrypt.compare(password, user.pash) const compare = await bcrypt.compare(password, user.pash)
if (compare) { if (compare) {
Logger.info(`[Auth] ${user.username} logged in from ${ipAddress}`)
res.json(this.getUserLoginResponsePayload(user)) res.json(this.getUserLoginResponsePayload(user))
} else { } else {
Logger.warn(`[Auth] Failed login attempt ${req.rateLimit.current} of ${req.rateLimit.limit} from ${ipAddress}`) Logger.warn(`[Auth] Failed login attempt ${req.rateLimit.current} of ${req.rateLimit.limit} from ${ipAddress}`)