all setup with jwt and bcrypt with whole file structure with auth middleware and login
This commit is contained in:
+22
-25
@@ -1,32 +1,29 @@
|
||||
const Sequelize = require('sequelize'),
|
||||
DonorsModel = require('../models/donors.model.js'),
|
||||
CampaignsModel = require('../models/campaigns.model.js');
|
||||
|
||||
DonorsModel = require('../models/donors.model.js'),
|
||||
CampaignsModel = require('../models/campaigns.model.js');
|
||||
|
||||
const host = 'am1shyeyqbxzy8gc.cbetxkdyhwsb.us-east-1.rds.amazonaws.com',
|
||||
username = 'fyro63k2989tyibh',
|
||||
password = 'ykjkyenyvxig208z',
|
||||
port = '3306',
|
||||
database = 'n0j9gxnf4ijr7g8t';
|
||||
username = 'fyro63k2989tyibh',
|
||||
password = 'ykjkyenyvxig208z',
|
||||
port = '3306',
|
||||
database = 'n0j9gxnf4ijr7g8t';
|
||||
|
||||
const sequelize = new Sequelize(
|
||||
database,
|
||||
username,
|
||||
password,
|
||||
{
|
||||
host: host,
|
||||
dialect: 'mysql',
|
||||
operatorsAliases: false,
|
||||
const sequelize = new Sequelize(database, username, password, {
|
||||
host: host,
|
||||
dialect: 'mysql',
|
||||
operatorsAliases: false,
|
||||
|
||||
// research for pool/connections
|
||||
pool: {
|
||||
max: 5,
|
||||
min: 0,
|
||||
acquire: 30000,
|
||||
idle: 10000
|
||||
}
|
||||
}
|
||||
);
|
||||
// research for pool/connections
|
||||
pool: {
|
||||
max: 5,
|
||||
min: 0,
|
||||
acquire: 30000,
|
||||
idle: 10000
|
||||
},
|
||||
|
||||
// disable logging; default: console.log
|
||||
logging: false
|
||||
});
|
||||
|
||||
const db = {};
|
||||
|
||||
@@ -39,4 +36,4 @@ db.campaigns = Campaigns;
|
||||
db.Sequelize = Sequelize;
|
||||
db.sequelize = sequelize;
|
||||
|
||||
module.exports = db;
|
||||
module.exports = db;
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
const db = require('../config/db.config.js'),
|
||||
bcrypt = require('bcrypt-nodejs'),
|
||||
jwt = require('jsonwebtoken'),
|
||||
checkAuth = require('../middleware/check-auth');
|
||||
|
||||
const Donors = db.donors;
|
||||
|
||||
// Find a Donor by email + login with JWT
|
||||
exports.login = (req, res) => {
|
||||
Donors.findAll({
|
||||
where: {
|
||||
email: req.body.email
|
||||
}
|
||||
})
|
||||
.then(donors => {
|
||||
if (donors.length < 1) {
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
}
|
||||
bcrypt.compare(
|
||||
req.body.password,
|
||||
donors[0].password,
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
}
|
||||
if (result) {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
email: donors[0].email,
|
||||
id: donors[0].id
|
||||
},
|
||||
process.env.JWT_KEY,
|
||||
{
|
||||
expiresIn: '1h'
|
||||
}
|
||||
);
|
||||
return res.status(200).json({
|
||||
message: 'Auth successful',
|
||||
donor: donors[0],
|
||||
token
|
||||
});
|
||||
}
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
}
|
||||
);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
|
||||
return res.status(500).json({
|
||||
error
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -1,58 +1,104 @@
|
||||
const db = require('../config/db.config.js');
|
||||
const db = require('../config/db.config.js'),
|
||||
bcrypt = require('bcrypt-nodejs');
|
||||
|
||||
const Donors = db.donors;
|
||||
|
||||
// Post a Donor
|
||||
exports.create = (req, res) => {
|
||||
// Save to MySQL database
|
||||
Donors.create({
|
||||
first_name: req.body.first_name,
|
||||
middle_name: req.body.middle_name,
|
||||
last_name: req.body.last_name,
|
||||
email: req.body.email,
|
||||
age: req.body.age,
|
||||
phone: req.body.phone,
|
||||
address: req.body.address,
|
||||
city: req.body.city,
|
||||
state: req.body.state,
|
||||
country: req.body.country
|
||||
}).then(donor => {
|
||||
// Send created donor to client
|
||||
res.send(donor);
|
||||
});
|
||||
|
||||
// Create/post a Donor
|
||||
exports.create = (req, res) => {
|
||||
// see if user already in db
|
||||
Donors.findAll({
|
||||
where: {
|
||||
email: req.body.email
|
||||
}
|
||||
})
|
||||
.then(donors => {
|
||||
if (donors.length >= 1) {
|
||||
return res.status(409).json({
|
||||
message: 'Email exists'
|
||||
});
|
||||
} 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
|
||||
});
|
||||
} else {
|
||||
Donors.create({
|
||||
first_name: req.body.first_name,
|
||||
middle_name: req.body.middle_name,
|
||||
last_name: req.body.last_name,
|
||||
email: req.body.email,
|
||||
password: hash, //hashed password
|
||||
age: req.body.age,
|
||||
phone: req.body.phone,
|
||||
address: req.body.address,
|
||||
city: req.body.city,
|
||||
state: req.body.state,
|
||||
country: req.body.country
|
||||
})
|
||||
.then(donor => {
|
||||
// Send created donor to client
|
||||
return res.status(201).json({
|
||||
message: 'User created',
|
||||
donor
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
|
||||
return res.status(500).json({
|
||||
error
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
|
||||
return res.status(500).json({
|
||||
error
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// FETCH all Donors
|
||||
exports.findAll = (req, res) => {
|
||||
Donors.findAll().then(donors => {
|
||||
// Send all donors to Client
|
||||
res.send(donors);
|
||||
// Send all donors to Client
|
||||
res.status(200).send(donors);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// Find a Donor by Id
|
||||
exports.findById = (req, res) => {
|
||||
exports.findById = (req, res) => {
|
||||
Donors.findById(req.params.donor_id).then(donor => {
|
||||
res.send(donor);
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// Delete a Donor by Id
|
||||
exports.delete = (req, res) => {
|
||||
const id = req.params.donor_id;
|
||||
Donors.destroy({
|
||||
where: { id: id }
|
||||
where: { id: id }
|
||||
}).then(() => {
|
||||
res.status(200).send('deleted successfully a donor with id = ' + id);
|
||||
res.status(200).send('deleted successfully a donor with id = ' + id);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// // Update a Donor
|
||||
// exports.update = (req, res) => {
|
||||
// const id = req.params.donor_id;
|
||||
// Donors.update( { firstname: req.body.firstname, lastname: req.body.lastname, age: req.body.age },
|
||||
// Donors.update( { firstname: req.body.firstname, lastname: req.body.lastname, age: req.body.age },
|
||||
// { where: {id: req.params.donorId} }
|
||||
// ).then(() => {
|
||||
// res.status(200).send("updated successfully a donor with id = " + id);
|
||||
// });
|
||||
// };
|
||||
// };
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
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
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
return res.status(401).json({
|
||||
message: 'Auth failed'
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -9,6 +9,7 @@ module.exports = (sequelize, DataTypes) => {
|
||||
middle_name: DataTypes.STRING,
|
||||
last_name: DataTypes.STRING,
|
||||
email: DataTypes.STRING,
|
||||
password: DataTypes.STRING,
|
||||
age: DataTypes.INTEGER,
|
||||
phone: DataTypes.BIGINT,
|
||||
address: DataTypes.STRING,
|
||||
@@ -21,4 +22,4 @@ module.exports = (sequelize, DataTypes) => {
|
||||
}
|
||||
);
|
||||
return Donors;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
const express = require('express'),
|
||||
router = express.Router(),
|
||||
checkAuth = require('../middleware/check-auth');
|
||||
|
||||
const auth = require('../controllers/auth.controller.js');
|
||||
|
||||
// Check database for donor
|
||||
router.post('/donor/login', auth.login);
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,13 +1,14 @@
|
||||
const express = require('express'),
|
||||
router = express.Router();
|
||||
router = express.Router(),
|
||||
checkAuth = require('../middleware/check-auth');
|
||||
|
||||
const donors = require('../controllers/donors.controller.js');
|
||||
|
||||
// Create a new Donor
|
||||
router.post('/', donors.create);
|
||||
|
||||
// Retrieve all Donor
|
||||
router.get('/', donors.findAll);
|
||||
// Retrieve all Donors
|
||||
router.get('/', checkAuth, donors.findAll);
|
||||
|
||||
// Retrieve a single Donor by Id
|
||||
router.get('/:DonorId', donors.findById);
|
||||
@@ -18,4 +19,4 @@ router.delete('/:DonorId', donors.delete);
|
||||
// // Update a Donor with Id
|
||||
// router.put('/api/donors/:DonorId', donors.update);
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user