This commit is contained in:
David Arranz 2019-08-05 12:55:24 +02:00
parent afb5d0883c
commit 093f7244da
4 changed files with 21 additions and 10 deletions

View File

@ -97,7 +97,9 @@ passport.use('local-phone', new LocalStrategy(localPhoneOptions, async (phone, f
// JWT // JWT
passport.use('jwt', new CustomStrategy(async (req, done) => { passport.use('jwt', new CustomStrategy(async (req, done) => {
const token = ((req && req.headers && req.headers['x-access-token']) ? req.headers['x-access-token'] : null); const token = ((req && req.headers && req.headers['x-access-token']) ? req.headers['x-access-token'] : null);
console.log('PASSPORT JWT', token);
if (!token) { if (!token) {
return done(null, false, { message: 'Unauthorized. Token missing.'}); return done(null, false, { message: 'Unauthorized. Token missing.'});
@ -107,7 +109,8 @@ passport.use('jwt', new CustomStrategy(async (req, done) => {
if (result) { if (result) {
//recuperamos el usuario de la petición //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){ if (user){
return done(null, user, { message: 'Logged in Successfully' }); return done(null, user, { message: 'Logged in Successfully' });
} }

View File

@ -126,16 +126,27 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) =>
const defaultService = { const defaultService = {
fetchAssociation: async(params, context) => { 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); const associationName = hasAssociation(params);
console.log('associationName => ', associationName); console.log('associationName => ', associationName);
delete params.params.association; delete params.params.association;
const associationInfo = foundModelAssociation(model, associationName); const associationInfo = foundModelAssociation(model, associationName);
if (associationInfo) { 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 = { const details = {
rows: await master[associationInfo.getFunc](), rows: detailRows,
count: await master[associationInfo.countFunc]() count: detailRows.length
} }
return details; return details;
} else { } else {
@ -155,9 +166,6 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) =>
// Necesario para el cálculo del count // Necesario para el cálculo del count
// https://github.com/sequelize/sequelize/issues/10557 // https://github.com/sequelize/sequelize/issues/10557
findOptions.distinct = true; findOptions.distinct = true;
// findOptions.col = model.primaryKey;
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
console.log(findOptions);
const result = await model.scope(context.scopes).findAndCountAll(findOptions); const result = await model.scope(context.scopes).findAndCountAll(findOptions);

View File

@ -60,6 +60,7 @@ async function loginWithPhone(req, res, next) {
//Comprobamos si el usuario ya existe en nuestro sistema //Comprobamos si el usuario ya existe en nuestro sistema
let _user = await authService.extraMethods.findUser(findOptions); let _user = await authService.extraMethods.findUser(findOptions);
_user = _user.toJSON();
const tokenData = { const tokenData = {
id: _user.id, id: _user.id,
@ -113,8 +114,7 @@ async function register(req, res, next) {
accessibility: req.body.accesssibility accessibility: req.body.accesssibility
}; };
newUser = await authService.extraMethods.createUser(data); newUser = await authService.extraMethods.createUser(data);
newUser = newUser.toJSON();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -123,6 +123,7 @@ async function register(req, res, next) {
}; };
if (newUser) { if (newUser) {
newUser = newUser.toJSON();
newUser = cleanAdminData(newUser); newUser = cleanAdminData(newUser);
newUser.token = tokens.token; newUser.token = tokens.token;
} }

View File

@ -39,7 +39,6 @@ module.exports = function (sequelize, DataTypes) {
attributes: ['name', 'surname', 'profile_picture'], attributes: ['name', 'surname', 'profile_picture'],
}] }]
}, },
}); });
Comment.addScope('onlyEvents', () => { Comment.addScope('onlyEvents', () => {