You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
upload-file-backend/src/api/controllers/v1/auth.controller.js

65 lines
1.7 KiB

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);
});
}
};