diff --git a/backend/controllers/auth.controller.js b/backend/controllers/auth.controller.js index de7bff7..625f72e 100644 --- a/backend/controllers/auth.controller.js +++ b/backend/controllers/auth.controller.js @@ -6,7 +6,6 @@ const Role = db.role; const Op = db.Sequelize.Op; const jwt = require("jsonwebtoken"); -const bcrypt = require("bcryptjs"); exports.signup = (req, res) => { // Save User to Database @@ -14,8 +13,8 @@ exports.signup = (req, res) => { firstName: req.body.firstName, lastName: req.body.lastName, email: req.body.email, - dateOfBirth: req.body.dateOfBirth, - password: bcrypt.hashSync(req.body.password, 8) + dateOfBirth: new Date(req.body.dateOfBirth), + password: req.body.password }) .then(user => { if (req.body.roles) { @@ -45,7 +44,7 @@ exports.signup = (req, res) => { exports.signin = (req, res) => { User.findOne({ where: { - username: req.body.username + email: req.body.email } }) .then(user => { @@ -53,10 +52,11 @@ exports.signin = (req, res) => { return res.status(404).send({ message: "User Not found." }); } - const passwordIsValid = bcrypt.compareSync( - req.body.password, - user.password - ); + const passwordIsValid = user.validPassword(req.body.password); + // const passwordIsValid = bcrypt.compareSync( + // req.body.password, + // user.password + // ); if (!passwordIsValid) { return res.status(401).send({ @@ -90,4 +90,4 @@ exports.signin = (req, res) => { .catch(err => { res.status(500).send({ message: err.message }); }); -}; \ No newline at end of file +}; diff --git a/backend/database/example-db.sqlite b/backend/database/example-db.sqlite deleted file mode 100644 index 8519508..0000000 Binary files a/backend/database/example-db.sqlite and /dev/null differ diff --git a/backend/index.js b/backend/index.js index 185c467..84026f8 100644 --- a/backend/index.js +++ b/backend/index.js @@ -1,5 +1,6 @@ const express = require("express"); const cors = require("cors"); +const bcrypt = require("bcryptjs"); const app = express(); @@ -49,10 +50,10 @@ function initial() { { name: 'administrato' }, ]); User.bulkCreate([ - { firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: 'nolan' }, - { firstName: 'Sandy', lastName: 'Koufax', dateOfBirth: new Date(1935, 12, 30), email: 'sandy.koufax@bullpen.com', password: 'sandy' }, - { firstName: 'Pedro', lastName: 'Martinez', dateOfBirth: new Date(1971, 10, 25), email: 'pedro.martinez@bullpen.com', password: 'pedro' }, - { firstName: 'randy', lastName: 'johnson', dateOfBirth: new Date(1963, 9, 10), email: 'randy.johnson@bullpen.com', password: 'randy' }, + { firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: bcrypt.hashSync('nolan', 8) }, + { firstName: 'Sandy', lastName: 'Koufax', dateOfBirth: new Date(1935, 12, 30), email: 'sandy.koufax@bullpen.com', password: bcrypt.hashSync('sandy', 8) }, + { firstName: 'Pedro', lastName: 'Martinez', dateOfBirth: new Date(1971, 10, 25), email: 'pedro.martinez@bullpen.com', password: bcrypt.hashSync('pedro', 8) }, + { firstName: 'randy', lastName: 'johnson', dateOfBirth: new Date(1963, 9, 10), email: 'randy.johnson@bullpen.com', password: bcrypt.hashSync('randy', 8) } ]); PitchType.bulkCreate([ { name: 'Fastball', abbreviation: 'FB' }, diff --git a/backend/models/user.model.js b/backend/models/user.model.js index 44f48cd..21b1cff 100644 --- a/backend/models/user.model.js +++ b/backend/models/user.model.js @@ -24,12 +24,7 @@ module.exports = (sequelize) => { email: { type: DataTypes.STRING, allowNull: false, - unique: true, - validate: { - // We require usernames to have length of at least 3, and - // only use letters, numbers and underscores. - is: /^\w{3,}$/ - } + unique: true }, password: { type: DataTypes.STRING, @@ -47,9 +42,9 @@ module.exports = (sequelize) => { } }); - User.prototype.validPassword = async function (password) { - return bcrypt.compare(password, this.password); + User.prototype.validPassword = function (password) { + return bcrypt.compareSync(password, this.password); }; return User; -}; \ No newline at end of file +};