Commit c3b799ec by Denys Tito Urbano

Router auth modificado (salida de post_login) y Helper error_json agregado

parent e5b4691f
const service = require('../services/auth.service'); const service = require('../services/auth.service');
const { generate_jwt } = require('../helpers'); const { generate_jwt, error_json } = require('../helpers');
const post_login = async (req, res) => { const post_login = async (req, res) => {
try { try {
const { email, password } = req.body; const { email, password } = req.body;
const { json: user } = await service.post_login({ email, password }); const salida = await service.post_login({ email, password });
const { json } = salida;
const { data: { documento } } = json;
const access_token = await generate_jwt({ email, documento });
json['data'].access_token = access_token;
if (!user) { res.json(json);
res.status(400).json({ message: 'Usuario / Contraseña incorrecta' });
}
const accessToken = await generate_jwt({ email, documento: user.documento });
res.json({ accessToken, user });
} catch (error) { } catch (error) {
res.status(400).json({ error }); res.status(400).json(error_json(error.message));
} }
}; };
const get_account = async (req, res) => { const get_account = async (req, res) => {
try { try {
const { documento } = req.params; const { documento } = req.params;
const { json: user } = await service.get_account({ documento }); const salida = await service.get_account({ documento });
const { json } = salida;
res.json({ user }); res.json(json);
} catch (error) { } catch (error) {
res.status(400).json({ error }); res.status(400).json(error_json(error.message));
} }
}; };
......
const service = require('../services/horario.service'); const service = require('../services/horario.service');
const { error_json } = require('../helpers');
const get_horario = async (req, res) => { const get_horario = async (req, res) => {
try { try {
...@@ -7,7 +8,7 @@ const get_horario = async (req, res) => { ...@@ -7,7 +8,7 @@ const get_horario = async (req, res) => {
res.json(salida); res.json(salida);
} catch (error) { } catch (error) {
res.status(400).json({ error }); res.status(400).json(error_json(error.message));
} }
}; };
......
const service = require('../services/material.service'); const service = require('../services/material.service');
const { error_json } = require('../helpers');
const get_materiales = async (req, res) => { const get_materiales = async (req, res) => {
try { try {
...@@ -7,7 +8,7 @@ const get_materiales = async (req, res) => { ...@@ -7,7 +8,7 @@ const get_materiales = async (req, res) => {
res.json(salida); res.json(salida);
} catch (error) { } catch (error) {
res.status(400).json({ message: error.message }); res.status(400).json(error_json(error.message));
} }
}; };
...@@ -18,7 +19,7 @@ const get_material = async (req, res) => { ...@@ -18,7 +19,7 @@ const get_material = async (req, res) => {
res.json(salida); res.json(salida);
} catch (error) { } catch (error) {
res.status(400).json({ message: error.message }); res.status(400).json(error_json(error.message));
} }
}; };
......
const error_json = (input) => {
try {
return JSON.parse(input);
} catch (error) {
return { status: false, message: input };
}
};
module.exports = {
error_json
};
const { download_file } = require('../helpers/download_file'); const { download_file } = require('./download_file');
const { generate_jwt } = require('./generate_jwt'); const { generate_jwt } = require('./generate_jwt');
const { format_timestamp } = require('../helpers/format_timestamp'); const { format_timestamp } = require('./format_timestamp');
const { error_json } = require('./error_json');
module.exports = { module.exports = {
download_file, download_file,
generate_jwt, generate_jwt,
format_timestamp format_timestamp,
error_json
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment