From 093f7244dad1db5653618f338b895c1492d3c1ed Mon Sep 17 00:00:00 2001 From: david Date: Mon, 5 Aug 2019 12:55:24 +0200 Subject: [PATCH] . --- core/passport.js | 5 ++++- helpers/service.helper.js | 20 ++++++++++++++------ modules/auth/auth.controller.js | 5 +++-- modules/comments/comment.model.js | 1 - 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/core/passport.js b/core/passport.js index 3b31dd0..97c7daa 100644 --- a/core/passport.js +++ b/core/passport.js @@ -97,7 +97,9 @@ passport.use('local-phone', new LocalStrategy(localPhoneOptions, async (phone, f // JWT passport.use('jwt', new CustomStrategy(async (req, done) => { + const token = ((req && req.headers && req.headers['x-access-token']) ? req.headers['x-access-token'] : null); + console.log('PASSPORT JWT', token); if (!token) { return done(null, false, { message: 'Unauthorized. Token missing.'}); @@ -107,7 +109,8 @@ passport.use('jwt', new CustomStrategy(async (req, done) => { if (result) { //recuperamos el usuario de la petición - let user = await authService.extraMethods.findUser({ phone: result.phone}); + console.log(result); + let user = await authService.extraMethods.findUser({ id: result.id }); if (user){ return done(null, user, { message: 'Logged in Successfully' }); } diff --git a/helpers/service.helper.js b/helpers/service.helper.js index b320e7f..b907dff 100644 --- a/helpers/service.helper.js +++ b/helpers/service.helper.js @@ -126,16 +126,27 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) => const defaultService = { fetchAssociation: async(params, context) => { + + const _fetchOne = async (params, context) => { + const findOptions = parseParamsToFindOptions(params); + return await model.scope(context.scopes).findOne(findOptions); + }; + + const associationName = hasAssociation(params); console.log('associationName => ', associationName); delete params.params.association; const associationInfo = foundModelAssociation(model, associationName); if (associationInfo) { - const master = await defaultService.fetchOne(params, context); + console.log(associationInfo); + const master = await _fetchOne(params, context); + console.log(master); + const detailRows = await master[associationInfo.getFunc](); + const details = { - rows: await master[associationInfo.getFunc](), - count: await master[associationInfo.countFunc]() + rows: detailRows, + count: detailRows.length } return details; } else { @@ -155,9 +166,6 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) => // Necesario para el cálculo del count // https://github.com/sequelize/sequelize/issues/10557 findOptions.distinct = true; -// findOptions.col = model.primaryKey; -console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); -console.log(findOptions); const result = await model.scope(context.scopes).findAndCountAll(findOptions); diff --git a/modules/auth/auth.controller.js b/modules/auth/auth.controller.js index 7b18ecf..8325eb6 100644 --- a/modules/auth/auth.controller.js +++ b/modules/auth/auth.controller.js @@ -60,6 +60,7 @@ async function loginWithPhone(req, res, next) { //Comprobamos si el usuario ya existe en nuestro sistema let _user = await authService.extraMethods.findUser(findOptions); + _user = _user.toJSON(); const tokenData = { id: _user.id, @@ -113,8 +114,7 @@ async function register(req, res, next) { accessibility: req.body.accesssibility }; - newUser = await authService.extraMethods.createUser(data); - newUser = newUser.toJSON(); + newUser = await authService.extraMethods.createUser(data); } catch (error) { console.error(error); @@ -123,6 +123,7 @@ async function register(req, res, next) { }; if (newUser) { + newUser = newUser.toJSON(); newUser = cleanAdminData(newUser); newUser.token = tokens.token; } diff --git a/modules/comments/comment.model.js b/modules/comments/comment.model.js index d15e1f2..a9adad9 100644 --- a/modules/comments/comment.model.js +++ b/modules/comments/comment.model.js @@ -39,7 +39,6 @@ module.exports = function (sequelize, DataTypes) { attributes: ['name', 'surname', 'profile_picture'], }] }, - }); Comment.addScope('onlyEvents', () => {