added roles with checking in middleware
- still need signup for orgs - login for admin
This commit is contained in:
@@ -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
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user