Start using sequelize migrations

This commit is contained in:
canove
2020-07-17 11:40:15 -03:00
parent 77158df3a1
commit a41f7e63ac
12 changed files with 160 additions and 83 deletions

View File

@@ -1,6 +1,6 @@
const { validationResult } = require("express-validator");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const authConfig = require("../config/auth");
const User = require("../models/User");
@@ -9,17 +9,20 @@ exports.store = async (req, res, next) => {
const user = await User.findOne({ where: { email: email } });
if (!user) {
return res.status(400).json({ error: "No user found with this email" });
return res.status(401).json({ error: "No user found with this email" });
}
const isEqual = await bcrypt.compare(password, user.password);
if (!isEqual) {
if (!(await user.checkPassword(password))) {
return res.status(401).json({ error: "Password does not match" });
}
const token = jwt.sign({ email: user.email, userId: user.id }, "mysecret", {
expiresIn: "24h",
});
const token = jwt.sign(
{ email: user.email, userId: user.id },
authConfig.secret,
{
expiresIn: authConfig.expiresIn,
}
);
return res
.status(200)

View File

@@ -1,5 +1,4 @@
const { validationResult } = require("express-validator");
const bcrypt = require("bcryptjs");
const User = require("../models/User");
@@ -12,14 +11,7 @@ exports.store = async (req, res, next) => {
.json({ error: "Validation failed", data: errors.array() });
}
const { name, password, email } = req.body;
const hashedPw = await bcrypt.hash(password, 12);
const user = User.build({
email: email,
password: hashedPw,
name: name,
});
const result = await user.save();
res.status(201).json({ message: "User created!", userId: result.id });
const { name, id, email } = await User.create(req.body);
// const result = await user.save();
res.status(201).json({ message: "User created!", userId: id });
};