added roles with checking in middleware

- still need signup for orgs
- login for admin
This commit is contained in:
Arjun Patel
2019-01-26 20:23:06 -08:00
parent 794980ff61
commit 2bf26fccbf
13 changed files with 111 additions and 29 deletions
+21 -14
View File
@@ -3,34 +3,41 @@ const db = require('../config/db.config.js'),
jwt = require('jsonwebtoken'),
errorMaker = require('../helpers/error.maker');
const Donor = db.Donor;
const { Donor, Organization } = db;
// Find a Donor by email + login with JWT
exports.login = (req, res, next) => {
Donor.findAll({
where: {
email: req.body.email
}
})
.then(donors => {
if (donors.length < 1) {
// Find a Donor/Org by email + login with JWT
exports.login = (type, role) => (req, res, next) => {
const curr_types = {
donor: Donor,
org: Organization
};
curr_types[type]
.findAll({
where: {
email: req.body.email
}
})
.then(users => {
if (users.length < 1) {
return next(errorMaker(401, 'Invalid or nonexistent email'));
}
bcrypt.compare(req.body.password, donors[0].password, (error, result) => {
bcrypt.compare(req.body.password, users[0].password, (error, result) => {
if (error) {
return next(error);
}
if (result) {
const token = jwt.sign(
{
email: donors[0].email,
id: donors[0].id
email: users[0].email,
id: users[0].id,
role: role
},
process.env.JWT_KEY
);
return res.status(200).json({
message: 'Auth successful',
donor: donors[0],
donor: users[0],
token
});
}