fixed rest api signup and signin
This commit is contained in:
parent
3cbb467457
commit
515041a6ff
|
|
@ -6,7 +6,6 @@ const Role = db.role;
|
||||||
const Op = db.Sequelize.Op;
|
const Op = db.Sequelize.Op;
|
||||||
|
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
const bcrypt = require("bcryptjs");
|
|
||||||
|
|
||||||
exports.signup = (req, res) => {
|
exports.signup = (req, res) => {
|
||||||
// Save User to Database
|
// Save User to Database
|
||||||
|
|
@ -14,8 +13,8 @@ exports.signup = (req, res) => {
|
||||||
firstName: req.body.firstName,
|
firstName: req.body.firstName,
|
||||||
lastName: req.body.lastName,
|
lastName: req.body.lastName,
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
dateOfBirth: req.body.dateOfBirth,
|
dateOfBirth: new Date(req.body.dateOfBirth),
|
||||||
password: bcrypt.hashSync(req.body.password, 8)
|
password: req.body.password
|
||||||
})
|
})
|
||||||
.then(user => {
|
.then(user => {
|
||||||
if (req.body.roles) {
|
if (req.body.roles) {
|
||||||
|
|
@ -45,7 +44,7 @@ exports.signup = (req, res) => {
|
||||||
exports.signin = (req, res) => {
|
exports.signin = (req, res) => {
|
||||||
User.findOne({
|
User.findOne({
|
||||||
where: {
|
where: {
|
||||||
username: req.body.username
|
email: req.body.email
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(user => {
|
.then(user => {
|
||||||
|
|
@ -53,10 +52,11 @@ exports.signin = (req, res) => {
|
||||||
return res.status(404).send({ message: "User Not found." });
|
return res.status(404).send({ message: "User Not found." });
|
||||||
}
|
}
|
||||||
|
|
||||||
const passwordIsValid = bcrypt.compareSync(
|
const passwordIsValid = user.validPassword(req.body.password);
|
||||||
req.body.password,
|
// const passwordIsValid = bcrypt.compareSync(
|
||||||
user.password
|
// req.body.password,
|
||||||
);
|
// user.password
|
||||||
|
// );
|
||||||
|
|
||||||
if (!passwordIsValid) {
|
if (!passwordIsValid) {
|
||||||
return res.status(401).send({
|
return res.status(401).send({
|
||||||
|
|
@ -90,4 +90,4 @@ exports.signin = (req, res) => {
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
res.status(500).send({ message: err.message });
|
res.status(500).send({ message: err.message });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1,5 +1,6 @@
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
|
const bcrypt = require("bcryptjs");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
|
@ -49,10 +50,10 @@ function initial() {
|
||||||
{ name: 'administrato' },
|
{ name: 'administrato' },
|
||||||
]);
|
]);
|
||||||
User.bulkCreate([
|
User.bulkCreate([
|
||||||
{ firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: 'nolan' },
|
{ 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: 'sandy' },
|
{ 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: 'pedro' },
|
{ 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: 'randy' },
|
{ firstName: 'randy', lastName: 'johnson', dateOfBirth: new Date(1963, 9, 10), email: 'randy.johnson@bullpen.com', password: bcrypt.hashSync('randy', 8) }
|
||||||
]);
|
]);
|
||||||
PitchType.bulkCreate([
|
PitchType.bulkCreate([
|
||||||
{ name: 'Fastball', abbreviation: 'FB' },
|
{ name: 'Fastball', abbreviation: 'FB' },
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,7 @@ module.exports = (sequelize) => {
|
||||||
email: {
|
email: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
unique: true,
|
unique: true
|
||||||
validate: {
|
|
||||||
// We require usernames to have length of at least 3, and
|
|
||||||
// only use letters, numbers and underscores.
|
|
||||||
is: /^\w{3,}$/
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
password: {
|
password: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
|
|
@ -47,9 +42,9 @@ module.exports = (sequelize) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
User.prototype.validPassword = async function (password) {
|
User.prototype.validPassword = function (password) {
|
||||||
return bcrypt.compare(password, this.password);
|
return bcrypt.compareSync(password, this.password);
|
||||||
};
|
};
|
||||||
|
|
||||||
return User;
|
return User;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue