all setup with jwt and bcrypt with whole file structure with auth middleware and login
This commit is contained in:
@@ -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);
|
||||
// });
|
||||
// };
|
||||
// };
|
||||
|
||||
Reference in New Issue
Block a user