added roles with checking in middleware
- still need signup for orgs - login for admin
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = {
|
||||||
|
organization: require('./organization.controller')
|
||||||
|
};
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
const db = require('../../config/db.config.js'),
|
||||||
|
errorMaker = require('../../helpers/error.maker');
|
||||||
|
|
||||||
|
const { Grant, Cause, Region, Organization } = db;
|
||||||
|
|
||||||
|
// PUT to change specific org's verify column to true
|
||||||
|
exports.verifyOrg = (req, res, next) => {
|
||||||
|
const charity_id = req.params.charity_id;
|
||||||
|
};
|
||||||
@@ -3,34 +3,41 @@ const db = require('../config/db.config.js'),
|
|||||||
jwt = require('jsonwebtoken'),
|
jwt = require('jsonwebtoken'),
|
||||||
errorMaker = require('../helpers/error.maker');
|
errorMaker = require('../helpers/error.maker');
|
||||||
|
|
||||||
const Donor = db.Donor;
|
const { Donor, Organization } = db;
|
||||||
|
|
||||||
// Find a Donor by email + login with JWT
|
// Find a Donor/Org by email + login with JWT
|
||||||
exports.login = (req, res, next) => {
|
exports.login = (type, role) => (req, res, next) => {
|
||||||
Donor.findAll({
|
const curr_types = {
|
||||||
where: {
|
donor: Donor,
|
||||||
email: req.body.email
|
org: Organization
|
||||||
}
|
};
|
||||||
})
|
|
||||||
.then(donors => {
|
curr_types[type]
|
||||||
if (donors.length < 1) {
|
.findAll({
|
||||||
|
where: {
|
||||||
|
email: req.body.email
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(users => {
|
||||||
|
if (users.length < 1) {
|
||||||
return next(errorMaker(401, 'Invalid or nonexistent email'));
|
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) {
|
if (error) {
|
||||||
return next(error);
|
return next(error);
|
||||||
}
|
}
|
||||||
if (result) {
|
if (result) {
|
||||||
const token = jwt.sign(
|
const token = jwt.sign(
|
||||||
{
|
{
|
||||||
email: donors[0].email,
|
email: users[0].email,
|
||||||
id: donors[0].id
|
id: users[0].id,
|
||||||
|
role: role
|
||||||
},
|
},
|
||||||
process.env.JWT_KEY
|
process.env.JWT_KEY
|
||||||
);
|
);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: 'Auth successful',
|
message: 'Auth successful',
|
||||||
donor: donors[0],
|
donor: users[0],
|
||||||
token
|
token
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ exports.create = (req, res, next) => {
|
|||||||
|
|
||||||
// Find grants with causes, regions, and organizations by donor_id
|
// Find grants with causes, regions, and organizations by donor_id
|
||||||
exports.findByDonorId = (req, res, next) => {
|
exports.findByDonorId = (req, res, next) => {
|
||||||
const donor_id = req.user_data.id;
|
const donor_id = req.user.id;
|
||||||
|
|
||||||
Grant.findAll({
|
Grant.findAll({
|
||||||
where: {
|
where: {
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module.exports = {
|
||||||
|
ADMIN: 'ADMIN',
|
||||||
|
ORGANIZATION: 'ORGANIZATION',
|
||||||
|
DONOR: 'DONOR'
|
||||||
|
};
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
const jwt = require('jsonwebtoken'),
|
const jwt = require('jsonwebtoken'),
|
||||||
errorMaker = require('../helpers/error.maker');
|
errorMaker = require('../helpers/error.maker');
|
||||||
|
|
||||||
module.exports = (req, res, next) => {
|
module.exports = role => (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const token = req.headers.authorization.split(' ')[1];
|
const token = req.headers.authorization.split(' ')[1];
|
||||||
const decoded = jwt.verify(token, process.env.JWT_KEY);
|
const decoded = jwt.verify(token, process.env.JWT_KEY);
|
||||||
req.user_data = decoded; //for use till end of request
|
req.user = decoded; //for use till end of request
|
||||||
|
|
||||||
|
if (role != req.user.role) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
},
|
},
|
||||||
middle_name: DataTypes.STRING,
|
|
||||||
last_name: {
|
last_name: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
},
|
},
|
||||||
monthly: {
|
monthly: {
|
||||||
type: DataTypes.BOOLEAN,
|
type: DataTypes.BOOLEAN,
|
||||||
allowNull: false
|
allowNull: false,
|
||||||
|
defaultValue: false
|
||||||
},
|
},
|
||||||
num_causes: {
|
num_causes: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
|
|||||||
@@ -6,6 +6,30 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
autoIncrement: true
|
autoIncrement: true
|
||||||
},
|
},
|
||||||
name: { type: DataTypes.STRING, allowNull: false },
|
name: { type: DataTypes.STRING, allowNull: false },
|
||||||
|
first_name: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
last_name: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
email: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
validate: {
|
||||||
|
isEmail: true
|
||||||
|
},
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
password: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
verified: {
|
||||||
|
type: DataTypes.BOOLEAN,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: false
|
||||||
|
},
|
||||||
short_description: DataTypes.STRING,
|
short_description: DataTypes.STRING,
|
||||||
primary_cause: { type: DataTypes.STRING, allowNull: false },
|
primary_cause: { type: DataTypes.STRING, allowNull: false },
|
||||||
primary_region: { type: DataTypes.STRING, allowNull: false },
|
primary_region: { type: DataTypes.STRING, allowNull: false },
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
const express = require('express'),
|
||||||
|
router = express.Router(),
|
||||||
|
checkAuth = require('../middleware/check-auth'),
|
||||||
|
roles = require('../helpers/roles');
|
||||||
|
|
||||||
|
const controllers = require('../controllers/admin');
|
||||||
|
|
||||||
|
// PUT Manual verification of a charity
|
||||||
|
router.put(
|
||||||
|
'/org/:charity_id',
|
||||||
|
checkAuth(roles.ADMIN),
|
||||||
|
controllers.organization.verifyOrg
|
||||||
|
);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
const express = require('express'),
|
const express = require('express'),
|
||||||
router = express.Router(),
|
router = express.Router(),
|
||||||
checkAuth = require('../middleware/check-auth');
|
checkAuth = require('../middleware/check-auth'),
|
||||||
|
roles = require('../helpers/roles');
|
||||||
|
|
||||||
const auth = require('../controllers/auth.controller.js');
|
const auth = require('../controllers/auth.controller.js');
|
||||||
|
|
||||||
// Check database for donor
|
// Check database for donor and get token with donor role
|
||||||
router.post('/donor/login', auth.login);
|
router.post('/donor/login', auth.login('donor', roles.DONOR));
|
||||||
|
|
||||||
|
// Check database for org and get token with org role
|
||||||
|
router.post('/org/login', auth.login('org', roles.ORGANIZATION));
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const express = require('express'),
|
const express = require('express'),
|
||||||
router = express.Router(),
|
router = express.Router(),
|
||||||
checkAuth = require('../middleware/check-auth');
|
checkAuth = require('../middleware/check-auth'),
|
||||||
|
roles = require('../helpers/roles');
|
||||||
|
|
||||||
const controllers = require('../controllers/donor');
|
const controllers = require('../controllers/donor');
|
||||||
|
|
||||||
@@ -8,33 +9,41 @@ const controllers = require('../controllers/donor');
|
|||||||
router.post('/', controllers.donor.create);
|
router.post('/', controllers.donor.create);
|
||||||
|
|
||||||
// GET Retrieve all Donors
|
// GET Retrieve all Donors
|
||||||
router.get('/', checkAuth, controllers.donor.findAll);
|
router.get('/', checkAuth(roles.DONOR), controllers.donor.findAll);
|
||||||
|
|
||||||
// GET Retrieve grants with causes and regions and charities details by donor_id
|
// GET Retrieve grants with causes and regions and charities details by donor_id
|
||||||
router.get('/grants/', checkAuth, controllers.grant.findByDonorId);
|
router.get('/grants/', checkAuth(roles.DONOR), controllers.grant.findByDonorId);
|
||||||
|
|
||||||
/** POST Create grants with following body:
|
/** POST Create grants with following body:
|
||||||
* - list of id's of selected causes and regions
|
* - list of id's of selected causes and regions
|
||||||
* - FINAL list of id's of selected organizations
|
* - FINAL list of id's of selected organizations
|
||||||
* - donor_id
|
* - donor_id
|
||||||
* */
|
* */
|
||||||
router.post('/grants/:donor_id', checkAuth, controllers.grant.create);
|
router.post(
|
||||||
|
'/grants/:donor_id',
|
||||||
|
checkAuth(roles.DONOR),
|
||||||
|
controllers.grant.create
|
||||||
|
);
|
||||||
|
|
||||||
// DELECT a grant of a donor
|
// DELECT a grant of a donor
|
||||||
router.delete('/grants/:grant_id', checkAuth, controllers.grant.delete);
|
router.delete(
|
||||||
|
'/grants/:grant_id',
|
||||||
|
checkAuth(roles.DONOR),
|
||||||
|
controllers.grant.delete
|
||||||
|
);
|
||||||
|
|
||||||
// POST to get suggested organizations to distribute to
|
// POST to get suggested organizations to distribute to
|
||||||
// running "the algorithm"
|
// running "the algorithm"
|
||||||
router.post(
|
router.post(
|
||||||
'/organizations/',
|
'/organizations/',
|
||||||
checkAuth,
|
checkAuth(roles.DONOR),
|
||||||
controllers.organization.findSuggested
|
controllers.organization.findSuggested
|
||||||
);
|
);
|
||||||
|
|
||||||
// GET min and optimal amount to choose
|
// GET min and optimal amount to choose
|
||||||
router.get(
|
router.get(
|
||||||
'/organizations/amounts',
|
'/organizations/amounts',
|
||||||
checkAuth,
|
checkAuth(roles.DONOR),
|
||||||
controllers.organization.findAmounts
|
controllers.organization.findAmounts
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,8 @@ app.use('/api/donors', require('./app/routes/donor.route.js'));
|
|||||||
app.use('/api/auth', require('./app/routes/auth.route.js'));
|
app.use('/api/auth', require('./app/routes/auth.route.js'));
|
||||||
//general routes
|
//general routes
|
||||||
app.use('/api', require('./app/routes/general.route.js'));
|
app.use('/api', require('./app/routes/general.route.js'));
|
||||||
|
//admin routes
|
||||||
|
app.use('/admin', require('./app/routes/admin.route'));
|
||||||
|
|
||||||
//404 not found error handling on any other routes
|
//404 not found error handling on any other routes
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user