I have put together a login / registration web app with React, Express, Node and PostgreSQL. I think i have my backend connected to PostgreSQL pgAdmin 4 but i am having trouble getting my login and regisration posts working correctly with Postman/ Insomnia. I am also using Sequelize / Redux.
auth.controller.js
const db = require("../server/models");
const config = require("../config/auth.config");
const User = db.userLogin;
//const Role = db.userRoles;
//const Op = db.Sequelize.Op;
var jwt = require("jsonwebtoken");
var bcrypt = require("bcrypt");
exports.signUp = (req, res) => {
// user being added to the db
User.create({
userName: req.body.userName,
password: bcrypt.hashSync(req.body.password, 8),
userEmail: req.body.userEmail,
userFirstName: req.body.userFirstName,
userSurname: req.body.userSurname,
})
// set user role to 1 to get basic user access on sign up
.then(user => {
user.set(user.userRole[1]).then(() => {
res.send({ message: "User registered."});
});
})
.catch(err => {
res.status(500).send({ message: err.message });
});
};
exports.signIn = (req, res) => {
User.findOne({
where: {
// checks login input to stored details
userName: req.body.userName
}
})
.then(user => {
if (!user) {
return res.status(404).send({ message: "User not registered."});
}
var passwordIsValid = bcrypt.compareSync(
req.body.password,
user.password
);
if (!passwordIsValid) {
return res.status(401).send({
accessToken: null,
message: "Incorrect password"
});
}
var token = jwt.sign({ id: user.userID }, config.secretKey, {
expiresIn: 86400
});
var permissions = [];
user.get(user.userRole).then(userRole => {
permissions.push(userRole);
})
res.status(200).send({
userID: user.userID,
userName: user.userName,
userEmail: user.userEmail,
userRoles: permissions,
accessToken: token
});
})
.catch(err => {
res.status(500).send({ message: err.message});
});
};
// controllers interact with PostgreSQL db via sequelize and send
// HTTP res (token, user info etc) to client
authJWT.js
const jwt = require("jsonwebtoken");
const config = require("../config/auth.config");
const db = require("../server/models");
const User = db.userLogin;
verifyToken = (req, res, next) => {
let token = req.headers["x-access-token"];
if (!token){
return res.status(403).send({
message: "No token returned"
});
}
jwt.verify(token, config.secretKey, (err, decoded) => {
if (err) {
return res.status(401).send({
message: "You're not authorised"
});
}
req.userID = decoded.id;
next(); // stops execution
});
};
// check registered user access level
isRegUser = (req, res, next) => {
User.findByPK(req.userID).then(user => {
user.get(user.userRole).then(userRole => {
for (let i = 0; i < userRole.length; i++) {
if (userRole[i].name === "1") {
next();
return;
}
}
res.status(403).send({
message: "Requires registered user status"
});
});
});
};
// scuba school access level
// find by primary key
isSchool = (req, res, next) => {
User.findByPK(req.userID).then(user => {
user.get(user.userRole).then(userRole => {
for (let i = 0; i < userRole.length; i++) {
if (userRole[i].name === "2") {
next();
return;
}
}
res.status(403).send({
message: "Requires school access"
});
});
});
};
// site admin access level
isSiteAdmin = (req, res, next) => {
User.findByPK(req.userID).then(user => {
user.get(user.userRole).then(userRole => {
for (let i = 0; i < userRole.length; i++) {
if (userRole[i].name === "3") {
next();
return;
}
}
res.status(403).send({
message: "Requires site admin access"
});
});
});
};
// access for everything (testing)
isSuperUser = (req, res, next) => {
// Seq method that finds by primary key
User.findByPK(req.userID).then(user => {
user.get(user.userRole).then(userRole => {
for (let i = 0; i < userRole.length; i++) {
if (userRole[i].name === "4") {
next();
return;
}
}
res.status(403).send({
message: "NO ENTRY"
});
});
});
};
const authJWT = {
verifyToken: verifyToken,
isRegUser: isRegUser,
isSchool: isSchool,
isSiteAdmin: isSiteAdmin,
isSuperUser: isSuperUser
};
module.exports = authJWT;
As the postman queries are timing out and not giving error messages i assume it is a problem with my backend rather than my frontend. Any advice would be much appreciated.
An example below is the json i am posting to the signUp api for new users. The method above should automatically assign new users the userRole 1 by default.
{
“userName”: “AdamBell”,
“password”: “bell1”,
“userEmail”: “adamBell@hotmail.com”,
“userFirstName”: “Adam”,
“userSurname”: “Bell”
}
I have the same problem with the signIn api where the timer just starts and keeps going when i try to post.