add reset password

This commit is contained in:
Le Viet
2022-03-09 17:46:51 +07:00
parent 5a22668153
commit d04fd654eb
31 changed files with 539 additions and 9563 deletions
Regular → Executable
View File
Regular → Executable
+54 -61
View File
@@ -1,13 +1,11 @@
var Sequelize = require('sequelize');
const nodemailer = require('nodemailer');
const { body, validationResult } = require('express-validator');
const member = require("../models").member;
const memberServices = require("../services").members;
const memberServices = require("../services").member;
module.exports = {
validate(method) {
switch (method) {
case 'createUser':
case 'register':
{
return [
body('username', 'userName doesn\'t exists').exists(),
@@ -23,14 +21,17 @@ module.exports = {
console.log(errors)
return res.status(400).json({ errors: errors.array() });
}
return memberServices.create(req)
.then(member => res.status(200).send("Register successfully"))
.catch(error => res.status(400).send(error.errors));
.then(member => {
res.status(200).send("Register succesfully")
})
.catch(error => {
res.status(400).send(error.errors)
});
},
async login(req, res) {
return memberServices.login(req)
login(req, res) {
memberServices.login(req)
.then(accessToken => res.status(200).send(accessToken))
.catch(error => res.status(400).send(error.errors));
},
@@ -41,44 +42,41 @@ module.exports = {
.catch(error => res.status(400).send(error.errors));
},
async forgotPassword(req, res) {
try {
const email = req.body.email;
const user = await member.findOne({
where: { username: email }
});
if (user) {
return res.status(200).send('Please check email');
} else {
res.send("Email doen't exist");
}
} catch (error) {
console.log(error);
res.status(500).send("Internal Server error Occured");
}
forgotPassword(req, res) {
memberServices.forgotPassword(req)
.then(result => {
console.log(result)
})
.catch(error => res.status(400).send(error.errors));
},
async resetPassword(req, res) {
const email = req.body.email;
const user = await member.findOne({
where: { username: email }
});
const salt = await bcrypt.genSaltSync(10);
const password = bcrypt.hashSync(req.body.password, salt);
user.password = password;
user.save();
res.status(200).send(user.email);
return member
.update({
password: password
}, { returning: true, where: { id: user.id } })
.then(([rowsUpdate, [updatedRow]]) => res.status(200).send(updatedRow))
.catch(error => res.status(400).send(error));
resetPassword(req, res) {
memberServices.resetPassword(req)
.then(result => {
res.status(200).send(result)
})
.catch(error => res.status(400).send(error.errors));
},
update(req, res) {
return member
.update({
confirmResetPassword(req, res) {
memberServices.confirmResetPassword(req)
.then(result => {
res.status(200).send(result)
})
.catch(error => res.status(400).send(error.errors));
},
completeResetPassword(req, res) {
memberServices.completeResetPassword(req)
.then(result => {
res.status(200).send(result)
})
.catch(error => res.status(400).send(error.errors));
},
updateProfile(req, res) {
return memberServices
.updateProfile({
name: req.body.name,
size: req.body.size,
price: req.body.price,
@@ -88,25 +86,20 @@ module.exports = {
.catch(error => res.status(400).send(error));
},
delete(req, res) {
return member
.destroy({
where: {
id: req.params.id
}
})
.then(rowDeleted => {
if (rowDeleted !== 1) {
throw ({
"name": "ValidationError",
"errors": [{
message: 'Item not found'
}]
});
}
return res.status(200).json({ message: "Deleted successfully" });
})
updatePassword(req, res) {
return memberServices
.updatePassword({
name: req.body.name,
size: req.body.size,
price: req.body.price,
status: req.body.status
}, { returning: true, where: { id: req.params.id } })
.then(([rowsUpdate, [updatedRow]]) => res.status(200).send(updatedRow))
.catch(error => res.status(400).send(error));
},
deactivate(req, res) {
}
};