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.
 
 
 

53 lines
1.3 KiB

import {createUserService, getUserByEmailServices} from '../services/users.services.js';
import bcrypt from 'bcrypt';
export async function createUser(req,res) {
const { username, email, password } = req.body;
const user = {
username,
email,
password
}
user.password = await bcrypt.hash(password, 12);
try {
await createUserService(user);
res.status(201).send({
message: 'User created successfully',
data:{
"username": username,
"email": email,
}
});
} catch (error) {
console.log(error);
res.status(500).json({
error: 500,
message: error.message,
})
}
}
export async function logUser(req,res) {
const email = req.body.email;
const password = req.body.password;
const response = await getUserByEmailServices(email);
const userData = response.rows[0];
const isPasswordValid = await bcrypt.compare(password, userData.password);
if (isPasswordValid) {
res.status(200).send({
message: 'User logged successfully',
})
} else {
res.status(401).send({
"error": 401,
"message": "Bad Credentials"
})
}
}