import { hash, compare } from 'bcryptjs'; import messages from '../../../config/messages'; import { handler as ErrorHandler } from '../../middlewares/error'; import User from '../../../common/models/user.model'; exports.register = async (req, res, next) => { const user = new User( req.body ); user.save() .then(data => { res.json({ code: 0, message: messages.CREATE_SUCCESS, data: User.transform(data) }); }).catch(ex => { ErrorHandler(ex, req, res, next); }); }; exports.login = async (req, res, next) => { res.json({ code: 0, message: null, data: { token: req.locals.token, user: req.locals.user } }); }; exports.loginToken = async (req, res, next) => res.json({ code: 0, message: messages.CREATE_SUCCESS, data: { token: req.locals.token, data: req.locals.user } }); exports.updatePassword = async (req, res, next) => { const { current_password, new_password } = req.body; // cónt const { user } = req.locals; // console.log(user); if (user) { const rounds = 10; const new_pass = await hash(new_password, rounds); return User.update( { password: new_pass }, { where: { id: user.id } }, ).then(async () => { const user = await User.get(12); console.log(user); res.json({ test: 1, code: 0, message: messages.UPDATE_SUCCESS }); }).catch(ex => { ErrorHandler(ex, req, res, next); }); } };