mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-02-14 17:29:35 +01:00
Fix:Change password api endpoint
This commit is contained in:
parent
6651ad0d45
commit
9beee3ed65
@ -566,6 +566,69 @@ class Auth {
|
|||||||
Source: global.Source
|
Source: global.Source
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} password
|
||||||
|
* @param {*} user
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
comparePassword(password, user) {
|
||||||
|
if (user.type === 'root' && !password && !user.pash) return true
|
||||||
|
if (!password || !user.pash) return false
|
||||||
|
return bcrypt.compare(password, user.pash)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User changes their password from request
|
||||||
|
*
|
||||||
|
* @param {import('express').Request} req
|
||||||
|
* @param {import('express').Response} res
|
||||||
|
*/
|
||||||
|
async userChangePassword(req, res) {
|
||||||
|
let { password, newPassword } = req.body
|
||||||
|
newPassword = newPassword || ''
|
||||||
|
const matchingUser = req.user
|
||||||
|
|
||||||
|
// Only root can have an empty password
|
||||||
|
if (matchingUser.type !== 'root' && !newPassword) {
|
||||||
|
return res.json({
|
||||||
|
error: 'Invalid new password - Only root can have an empty password'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check password match
|
||||||
|
const compare = await this.comparePassword(password, matchingUser)
|
||||||
|
if (!compare) {
|
||||||
|
return res.json({
|
||||||
|
error: 'Invalid password'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let pw = ''
|
||||||
|
if (newPassword) {
|
||||||
|
pw = await this.hashPass(newPassword)
|
||||||
|
if (!pw) {
|
||||||
|
return res.json({
|
||||||
|
error: 'Hash failed'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
matchingUser.pash = pw
|
||||||
|
|
||||||
|
const success = await Database.updateUser(matchingUser)
|
||||||
|
if (success) {
|
||||||
|
Logger.info(`[Auth] User "${matchingUser.username}" changed password`)
|
||||||
|
res.json({
|
||||||
|
success: true
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
res.json({
|
||||||
|
error: 'Unknown error'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Auth
|
module.exports = Auth
|
Loading…
Reference in New Issue
Block a user