fixed rest api signup and signin

This commit is contained in:
Sascha Kühl 2025-03-12 15:49:10 +01:00
parent 3cbb467457
commit 515041a6ff
4 changed files with 18 additions and 22 deletions

View File

@ -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 });
});
};
};

Binary file not shown.

View File

@ -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' },

View File

@ -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;
};
};