.
This commit is contained in:
parent
a28814a420
commit
a513dcb32e
@ -1,19 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const passportJWT = require("passport-jwt");
|
const passport = require('passport');
|
||||||
const JWTStrategy = passportJWT.Strategy;
|
|
||||||
const ExtractJWT = passportJWT.ExtractJwt;
|
|
||||||
const config = require('../config');
|
|
||||||
|
|
||||||
/**
|
exports.isRegisteresUser = passport.authenticate('local', { session: false });
|
||||||
* Login Required middleware.
|
exports.isLoggedUser = passport.authenticate('jwt', { session: false });
|
||||||
*/
|
|
||||||
exports.isAuthenticated = (req, res, next) => {
|
|
||||||
if (req.isAuthenticated()) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
res.redirect('/login');
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Authorization Required middleware.
|
* Authorization Required middleware.
|
||||||
|
|||||||
@ -28,42 +28,18 @@ const VG_CT_VIDEOGAME_DELETED_SUCCESSFULLY = 'Videogame deleted successfully';
|
|||||||
|
|
||||||
async function login(req, res, next) {
|
async function login(req, res, next) {
|
||||||
try {
|
try {
|
||||||
passport.authenticate('local', { session: false }, async (error, user, info) => {
|
const data = {
|
||||||
try {
|
id: req.user.id,
|
||||||
if (!user) {
|
email: req.user.email
|
||||||
return res.status(httpStatus.NOT_FOUND).json(messageHelper.buildMessage(NOT_FOUND));
|
};
|
||||||
}
|
|
||||||
|
|
||||||
req.login(user, { session: false }, async (error) => {
|
|
||||||
if (error) {
|
|
||||||
return controllerHelper.handleErrorResponse(MODULE_NAME, login.name, error, res);
|
|
||||||
}
|
|
||||||
|
|
||||||
//We don't want to store the sensitive information such as the
|
|
||||||
//user password in the token so we pick only the email and id
|
|
||||||
const data = {
|
|
||||||
id: user.id,
|
|
||||||
email: user.email
|
|
||||||
};
|
|
||||||
|
|
||||||
//Send back the token to the user
|
|
||||||
return res.json({
|
|
||||||
token: securityHelper.generateToken({ user: data }),
|
|
||||||
user: {
|
|
||||||
id: data.id,
|
|
||||||
email: data.email
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
return next(error);
|
|
||||||
}
|
|
||||||
})(req, res, next);
|
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
token: securityHelper.generateToken(data),
|
||||||
|
user: data,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
controllerHelper.handleErrorResponse(MODULE_NAME, login.name, error, res)
|
controllerHelper.handleErrorResponse(MODULE_NAME, login.name, error, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@ -2,8 +2,8 @@ const routes = require('express').Router();
|
|||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
const authController = require('./auth.controller');
|
const authController = require('./auth.controller');
|
||||||
const authValidation = require('./auth.validations');
|
const authValidation = require('./auth.validations');
|
||||||
const securityHelper = require('../../helpers/security.helper');
|
|
||||||
const SchemaValidator = require('../../middlewares/schemaValidator');
|
const SchemaValidator = require('../../middlewares/schemaValidator');
|
||||||
|
const AccessValidator = require('../../middlewares/accessValidator');
|
||||||
|
|
||||||
//const postService = require('./post.service')(models.Post);
|
//const postService = require('./post.service')(models.Post);
|
||||||
//const postController = require('./post.controller')(postService);
|
//const postController = require('./post.controller')(postService);
|
||||||
@ -12,24 +12,15 @@ const SchemaValidator = require('../../middlewares/schemaValidator');
|
|||||||
//const postHandler = new ModelHandler(models.Post);
|
//const postHandler = new ModelHandler(models.Post);
|
||||||
|
|
||||||
routes.post('/auth',
|
routes.post('/auth',
|
||||||
SchemaValidator(authValidation.login, true),
|
SchemaValidator(authValidation.LoginInputType, true),
|
||||||
passport.authenticate('local'),
|
AccessValidator.isRegisteresUser,
|
||||||
function (req, res, next) {
|
authController.login,
|
||||||
const data = {
|
|
||||||
id: req.user.id,
|
|
||||||
email: req.user.email
|
|
||||||
};
|
|
||||||
|
|
||||||
return res.json({
|
|
||||||
token: securityHelper.generateToken(data),
|
|
||||||
user: data,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
//authController.login
|
|
||||||
);
|
);
|
||||||
|
|
||||||
routes.get('/pepepe', passport.authenticate('jwt', { session: false }), function (req, res, next) {
|
routes.get('/pepepe', AccessValidator.isLoggedUser,
|
||||||
res.send(req.user.email);
|
function (req, res, next) {
|
||||||
});
|
res.send(req.user.email);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
module.exports = routes;
|
module.exports = routes;
|
||||||
@ -11,5 +11,6 @@ const LoginOutputType = Joi.object().keys({
|
|||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
login: LoginInputType,
|
LoginInputType,
|
||||||
|
LoginOutputType
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user