fix(Auth): Error type in json payload

This commit is contained in:
Berkeley Martinez
2018-01-01 15:39:14 -08:00
committed by mrugesh mohapatra
parent e1c5b8a894
commit a1530c041a
3 changed files with 24 additions and 9 deletions

View File

@ -66,13 +66,13 @@ module.exports = function enableAuthentication(app) {
const passwordlessGetValidators = [
check('email')
.isBase64()
.withMessage('email should be a base64 encoded string'),
.withMessage('Email should be a base64 encoded string.'),
check('token')
.exists()
.withMessage('token should exist')
.withMessage('Token should exist.')
// based on strongloop/loopback/common/models/access-token.js#L15
.isLength({ min: 64, max: 64 })
.withMessage('token is not the right length')
.withMessage('Token is not the right length.')
];
function getPasswordlessAuth(req, res, next) {
@ -83,7 +83,7 @@ module.exports = function enableAuthentication(app) {
} = {}
} = req;
const validation = validationResult(req)
.formatWith(createValidatorErrorFormatter('info', '/email-signup'));
.formatWith(createValidatorErrorFormatter('errors', '/email-signup'));
if (!validation.isEmpty()) {
const errors = validation.array();
@ -193,12 +193,12 @@ module.exports = function enableAuthentication(app) {
const passwordlessPostValidators = [
check('email')
.isEmail()
.withMessage('email is not a valid email address')
.withMessage('Email is not a valid email address.')
];
function postPasswordlessAuth(req, res, next) {
const { body: { email } = {} } = req;
const validation = validationResult(req)
.formatWith(createValidatorErrorFormatter('info', '/email-signup'));
.formatWith(createValidatorErrorFormatter('errors', '/email-signup'));
if (!validation.isEmpty()) {
const errors = validation.array();
return next(errors.pop());

View File

@ -89,6 +89,7 @@ export default function prodErrorHandler() {
} else if (type === 'json') {
res.setHeader('Content-Type', 'application/json');
return res.send({
type: handled.type || 'errors',
message
});
// plain text

View File

@ -74,11 +74,25 @@ block content
}
})
.done(data =>{
if(data && data.message){
if(data && data.message) {
var alertType = 'alert-';
switch (data.type) {
case 'errors': {
alertType += 'danger';
break
}
case 'success': {
alertType += 'success';
break
}
default: {
alertType += 'info';
}
}
$('#flash-content').html(data.message);
$('#flash-board')
.removeClass('alert-info')
.addClass('alert-success')
.removeClass('alert-info alert-success alert-error')
.addClass(alertType)
.fadeIn();
disableMagicButton(false);
}