env variables through different file and optimizing error handling with middleware
This commit is contained in:
+13
-12
@@ -1,16 +1,10 @@
|
||||
const Sequelize = require('sequelize'),
|
||||
DonorsModel = require('../models/donors.model.js'),
|
||||
CampaignsModel = require('../models/campaigns.model.js');
|
||||
GrantsModel = require('../models/grants.model.js');
|
||||
|
||||
const host = 'am1shyeyqbxzy8gc.cbetxkdyhwsb.us-east-1.rds.amazonaws.com',
|
||||
username = 'fyro63k2989tyibh',
|
||||
password = 'ykjkyenyvxig208z',
|
||||
port = '3306',
|
||||
database = 'n0j9gxnf4ijr7g8t';
|
||||
const connection_uri = process.env.JAWSDB_MARIA_URL;
|
||||
|
||||
const sequelize = new Sequelize(database, username, password, {
|
||||
host: host,
|
||||
dialect: 'mysql',
|
||||
const sequelize = new Sequelize(connection_uri, {
|
||||
operatorsAliases: false,
|
||||
|
||||
// research for pool/connections
|
||||
@@ -21,17 +15,24 @@ const sequelize = new Sequelize(database, username, password, {
|
||||
idle: 10000
|
||||
},
|
||||
|
||||
// default options for all models
|
||||
define: {
|
||||
underscored: true, // true: use underscore for automatically added attributes like timestamps below
|
||||
freezeTableName: true, // true: table name/first parameter of define method will be table name
|
||||
timestamps: true // createdAt and updatedAt automatically added
|
||||
},
|
||||
|
||||
// disable logging; default: console.log
|
||||
logging: false
|
||||
});
|
||||
|
||||
const db = {};
|
||||
|
||||
//creating tables/models from imported function
|
||||
//creating tables/models from imported functions
|
||||
const Donors = DonorsModel(sequelize, Sequelize);
|
||||
const Campaigns = CampaignsModel(sequelize, Sequelize);
|
||||
const Grants = GrantsModel(sequelize, Sequelize);
|
||||
db.donors = Donors;
|
||||
db.campaigns = Campaigns;
|
||||
db.grants = Grants;
|
||||
|
||||
db.Sequelize = Sequelize;
|
||||
db.sequelize = sequelize;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
const db = require('../config/db.config.js'),
|
||||
bcrypt = require('bcrypt-nodejs'),
|
||||
jwt = require('jsonwebtoken');
|
||||
jwt = require('jsonwebtoken'),
|
||||
errorMaker = require('../helpers/error.maker');
|
||||
|
||||
const Donors = db.donors;
|
||||
|
||||
// Find a Donor by email + login with JWT
|
||||
exports.login = (req, res) => {
|
||||
exports.login = (req, res, next) => {
|
||||
Donors.findAll({
|
||||
where: {
|
||||
email: req.body.email
|
||||
@@ -13,15 +14,11 @@ exports.login = (req, res) => {
|
||||
})
|
||||
.then(donors => {
|
||||
if (donors.length < 1) {
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
return next(errorMaker(401, 'Invalid or nonexistent email'));
|
||||
}
|
||||
bcrypt.compare(req.body.password, donors[0].password, (error, result) => {
|
||||
if (error) {
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
return next(error);
|
||||
}
|
||||
if (result) {
|
||||
const token = jwt.sign(
|
||||
@@ -40,16 +37,8 @@ exports.login = (req, res) => {
|
||||
token
|
||||
});
|
||||
}
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
return next(errorMaker(401, 'Invalid user'));
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
|
||||
return res.status(500).json({
|
||||
error
|
||||
});
|
||||
});
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
@@ -13,17 +13,13 @@ exports.create = (req, res) => {
|
||||
})
|
||||
.then(donors => {
|
||||
if (donors.length >= 1) {
|
||||
return res.status(409).json({
|
||||
message: 'Email exists'
|
||||
});
|
||||
return next(errorMaker(409, `Email Exists: ${req.body.email}`));
|
||||
} else {
|
||||
// hash and store
|
||||
bcrypt.hash(req.body.password, null, null, function(error, hash) {
|
||||
// Store hash in your password DB.
|
||||
if (error) {
|
||||
return res.status(500).json({
|
||||
error
|
||||
});
|
||||
return next(error);
|
||||
} else {
|
||||
Donors.create({
|
||||
first_name: req.body.first_name,
|
||||
@@ -45,20 +41,12 @@ exports.create = (req, res) => {
|
||||
donor
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
return res.status(500).json({
|
||||
error
|
||||
});
|
||||
});
|
||||
.catch(error => next(error));
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
return res.status(500).json({
|
||||
error
|
||||
});
|
||||
});
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
// FETCH all Donors
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = (status, message) => {
|
||||
const error = new Error(message);
|
||||
error.status = status;
|
||||
return error;
|
||||
};
|
||||
@@ -1,15 +1,14 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
const jwt = require('jsonwebtoken'),
|
||||
errorMaker = require('../helpers/error.maker');
|
||||
|
||||
module.exports = (req, res, next) => {
|
||||
try {
|
||||
const token = req.headers.authorization.split(' ')[1];
|
||||
const decoded = jwt.verify(token, process.env.JWT_KEY);
|
||||
req.donorData = decoded; //for use till end of request
|
||||
req.userData = decoded; //for use till end of request
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Campaigns = sequelize.define('campaigns', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
first_name: DataTypes.STRING,
|
||||
middle_name: DataTypes.STRING,
|
||||
last_name: DataTypes.STRING,
|
||||
email: DataTypes.STRING,
|
||||
phone: DataTypes.STRING,
|
||||
address: DataTypes.STRING,
|
||||
city: DataTypes.STRING,
|
||||
state: DataTypes.STRING,
|
||||
country: DataTypes.STRING,
|
||||
},
|
||||
{
|
||||
freezeTableName: true,
|
||||
}
|
||||
);
|
||||
return Campaigns;
|
||||
}
|
||||
+30
-36
@@ -1,40 +1,34 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Donors = sequelize.define(
|
||||
'donors',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
first_name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
middle_name: DataTypes.STRING,
|
||||
last_name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
isEmail: true,
|
||||
allowNull: false
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
age: DataTypes.INTEGER,
|
||||
phone: DataTypes.BIGINT,
|
||||
address: DataTypes.STRING,
|
||||
city: DataTypes.STRING,
|
||||
state: DataTypes.STRING,
|
||||
country: DataTypes.STRING
|
||||
const Donors = sequelize.define('donors', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
{
|
||||
freezeTableName: true
|
||||
}
|
||||
);
|
||||
first_name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
middle_name: DataTypes.STRING,
|
||||
last_name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
isEmail: true,
|
||||
allowNull: false
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
age: DataTypes.INTEGER,
|
||||
phone: DataTypes.BIGINT,
|
||||
address: DataTypes.STRING,
|
||||
city: DataTypes.STRING,
|
||||
state: DataTypes.STRING,
|
||||
country: DataTypes.STRING
|
||||
});
|
||||
return Donors;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Campaigns = sequelize.define('campaigns', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
// user_id: {
|
||||
// type: DataTypes.INTEGER,
|
||||
// references: {
|
||||
// // This is a reference to another model
|
||||
// model: Donors,
|
||||
// // This is the column name of the referenced model
|
||||
// key: 'id'
|
||||
// }
|
||||
// },
|
||||
name: DataTypes.STRING,
|
||||
amount: DataTypes.INTEGER
|
||||
});
|
||||
return Campaigns;
|
||||
};
|
||||
@@ -4,7 +4,7 @@ const express = require('express'),
|
||||
|
||||
const donors = require('../controllers/donors.controller.js');
|
||||
|
||||
// Create a new Donor
|
||||
// Signup route
|
||||
router.post('/', donors.create);
|
||||
|
||||
// Retrieve all Donors
|
||||
|
||||
Reference in New Issue
Block a user