📂Better folder structures and file names in backend

This commit is contained in:
canove
2020-07-08 11:48:11 -03:00
parent c9cc973088
commit b1bed3623e
11 changed files with 55 additions and 68 deletions

View File

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