fix bug on user name management (#799)

Signed-off-by: Denis barbaron <denis.barbaron@orange.com>
This commit is contained in:
Denis Barbaron
2024-07-24 23:18:19 +02:00
committed by GitHub
parent 489ba0427e
commit a199bae42b
2 changed files with 53 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/**
* Copyright © 2019 contains code contributed by Orange SA, authors: Denis Barbaron - Licensed under the Apache license 2.0
* Copyright © 2019-2024 contains code contributed by Orange SA, authors: Denis Barbaron - Licensed under the Apache license 2.0
**/
var http = require('http')
@@ -117,6 +117,15 @@ module.exports = function(options) {
req.checkBody('name').notEmpty()
req.checkBody('email').isEmail()
})
.then(function() {
return dbapi.checkUserBeforeLogin(req.body)
})
.then(function(isValidCredential) {
if (!isValidCredential) {
return Promise.reject('InvalidCredentialsError')
}
return isValidCredential
})
.then(function() {
log.info('Authenticated "%s"', req.body.email)
var token = jwtutil.encode({
@@ -146,12 +155,22 @@ module.exports = function(options) {
})
})
.catch(function(err) {
log.error('Unexpected error', err.stack)
res.status(500)
.json({
success: false
, error: 'ServerError'
})
if (err === 'InvalidCredentialsError') {
log.warn('Authentication failure for "%s"', req.body.email)
res.status(400)
.json({
success: false
, error: 'InvalidCredentialsError'
})
}
else {
log.error('Unexpected error', err.stack)
res.status(500)
.json({
success: false
, error: 'ServerError'
})
}
})
break
default: