Skip to content

Instantly share code, notes, and snippets.

@blogcacanid
Created November 12, 2020 07:45
Show Gist options
  • Save blogcacanid/51b447863daa1b708fc5af3e6af06339 to your computer and use it in GitHub Desktop.
Save blogcacanid/51b447863daa1b708fc5af3e6af06339 to your computer and use it in GitHub Desktop.
verifyRegister.js Authentication JWT Node.js
const db = require("../models");
const User = db.user;
checkDuplicateUsernameOrEmail = (req, res, next) => {
// Username
User.findOne({
where: {
username: req.body.username
}
}).then(user => {
if (user) {
res.status(400).send({
message: "Failed! Username is already in use!"
});
return;
}
// Email
User.findOne({
where: {
email: req.body.email
}
}).then(user => {
if (user) {
res.status(400).send({
message: "Failed! Email is already in use!"
});
return;
}
next();
});
});
};
checkRolesExisted = (req, res, next) => {
if (req.body.roles) {
for (let i = 0; i < req.body.roles.length; i++) {
if (!ROLES.includes(req.body.roles[i])) {
res.status(400).send({
message: "Failed! Role does not exist = " + req.body.roles[i]
});
return;
}
}
}
next();
};
const verifyRegister = {
checkDuplicateUsernameOrEmail: checkDuplicateUsernameOrEmail,
checkRolesExisted: checkRolesExisted
};
module.exports = verifyRegister;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment