-
Notifications
You must be signed in to change notification settings - Fork 70
code improvement and remove body-parser and added new package #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zubair-saif
wants to merge
2
commits into
bezkoder:master
Choose a base branch
from
zubair-saif:code-improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| POSTGRES_URI=postgres://postgres:admin@localhost:5432/testdb | ||
| SESSION_SECRET=bezkoder-secret-key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,87 +1,92 @@ | ||
| const jwt = require("jsonwebtoken"); | ||
| const bcrypt = require("bcrypt"); | ||
| const db = require("../models"); | ||
| const config = require("../config/auth.config"); | ||
| const User = db.user; | ||
| const Role = db.role; | ||
|
|
||
| const Op = db.Sequelize.Op; | ||
|
|
||
| var jwt = require("jsonwebtoken"); | ||
| var bcrypt = require("bcryptjs"); | ||
|
|
||
| exports.signup = (req, res) => { | ||
| exports.signup = async (req, res) => { | ||
| // Save User to Database | ||
| User.create({ | ||
| username: req.body.username, | ||
| email: req.body.email, | ||
| password: bcrypt.hashSync(req.body.password, 8) | ||
| }) | ||
| .then(user => { | ||
| try { | ||
| const user = await User.create({ | ||
| username: req.body.username, | ||
| email: req.body.email, | ||
| password: bcrypt.hashSync(req.body.password, 10) | ||
| }); | ||
| if (user) { | ||
| if (req.body.roles) { | ||
| Role.findAll({ | ||
| const role = await Role.findAll({ | ||
| where: { | ||
| name: { | ||
| [Op.or]: req.body.roles | ||
| } | ||
| } | ||
| }).then(roles => { | ||
| user.setRoles(roles).then(() => { | ||
| res.send({ message: "User registered successfully!" }); | ||
| }); | ||
| }); | ||
| if (role) { | ||
| await user.setRoles(roles) | ||
| res.send({ message: "User registered successfully!" }); | ||
| } | ||
|
|
||
| } else { | ||
| // user role = 1 | ||
| user.setRoles([1]).then(() => { | ||
| res.send({ message: "User registered successfully!" }); | ||
| }); | ||
| } | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).send({ message: err.message }); | ||
| }); | ||
| }; | ||
| const setRoles = await user.setRoles([1]); | ||
| res.send({ message: "User registered successfully!" }); | ||
|
|
||
| exports.signin = (req, res) => { | ||
| User.findOne({ | ||
| where: { | ||
| username: req.body.username | ||
| } | ||
| }) | ||
| .then(user => { | ||
| if (!user) { | ||
| return res.status(404).send({ message: "User Not found." }); | ||
| } | ||
| } | ||
|
|
||
| var passwordIsValid = bcrypt.compareSync( | ||
| req.body.password, | ||
| user.password | ||
| ); | ||
| } catch (err) { | ||
| res.status(500).send({ message: err.message }); | ||
| } | ||
|
|
||
| if (!passwordIsValid) { | ||
| return res.status(401).send({ | ||
| accessToken: null, | ||
| message: "Invalid Password!" | ||
| }); | ||
|
|
||
| }; | ||
|
|
||
| exports.signin = async (req, res) => { | ||
|
|
||
| try { | ||
| const user = await User.findOne({ | ||
| where: { | ||
| username: req.body.username | ||
| } | ||
| }); | ||
| if (!user) { | ||
| return res.status(404).send({ message: "User Not found." }); | ||
| } | ||
|
|
||
| var token = jwt.sign({ id: user.id }, config.secret, { | ||
| expiresIn: 86400 // 24 hours | ||
| }); | ||
| const passwordIsValid = await bcrypt.compareSync( | ||
| req.body.password, | ||
| user.password | ||
| ); | ||
|
|
||
| var authorities = []; | ||
| user.getRoles().then(roles => { | ||
| for (let i = 0; i < roles.length; i++) { | ||
| authorities.push("ROLE_" + roles[i].name.toUpperCase()); | ||
| } | ||
| res.status(200).send({ | ||
| id: user.id, | ||
| username: user.username, | ||
| email: user.email, | ||
| roles: authorities, | ||
| accessToken: token | ||
| }); | ||
| if (!passwordIsValid) { | ||
| return res.status(401).send({ | ||
| accessToken: null, | ||
| message: "Invalid Password!" | ||
| }); | ||
| }) | ||
| .catch(err => { | ||
| res.status(500).send({ message: err.message }); | ||
| } | ||
|
|
||
| const token = await jwt.sign({ id: user.id }, process.env.SESSION_SECRET, { | ||
| expiresIn: 86400 // 24 hours | ||
| }); | ||
| }; | ||
|
|
||
| const authorities = []; | ||
| const roles = await user.getRoles() | ||
| if (roles) { | ||
| for (let i = 0; i < roles.length; i++) { | ||
| authorities.push("ROLE_" + roles[i].name.toUpperCase()); | ||
| } | ||
| res.status(200).send({ | ||
| id: user.id, | ||
| username: user.username, | ||
| email: user.email, | ||
| roles: authorities, | ||
| accessToken: token | ||
| }); | ||
| } | ||
| } catch (err) { | ||
| res.status(500).send({ message: err.message }); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| const jwt = require("jsonwebtoken"); | ||
| const config = require("../config/auth.config.js"); | ||
| const db = require("../models"); | ||
| const User = db.user; | ||
|
|
||
| verifyToken = (req, res, next) => { | ||
| verifyToken = async (req, res, next) => { | ||
|
|
||
| let token = req.headers["x-access-token"]; | ||
|
|
||
| if (!token) { | ||
|
|
@@ -12,7 +12,7 @@ verifyToken = (req, res, next) => { | |
| }); | ||
| } | ||
|
|
||
| jwt.verify(token, config.secret, (err, decoded) => { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added secret from env file |
||
| jwt.verify(token, process.env.SESSION_SECRET, (err, decoded) => { | ||
| if (err) { | ||
| return res.status(401).send({ | ||
| message: "Unauthorized!" | ||
|
|
@@ -23,44 +23,51 @@ verifyToken = (req, res, next) => { | |
| }); | ||
| }; | ||
|
|
||
| isAdmin = (req, res, next) => { | ||
| User.findByPk(req.userId).then(user => { | ||
| user.getRoles().then(roles => { | ||
| isAdmin = async (req, res, next) => { | ||
| const user = await User.findByPk(req.userId); | ||
| if (user) { | ||
| const roles = await user.getRoles(); | ||
| if (roles) { | ||
| for (let i = 0; i < roles.length; i++) { | ||
| if (roles[i].name === "admin") { | ||
| next(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| res.status(403).send({ | ||
| message: "Require Admin Role!" | ||
| }); | ||
| return; | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| isModerator = async (req, res, next) => { | ||
|
|
||
| isModerator = (req, res, next) => { | ||
| User.findByPk(req.userId).then(user => { | ||
| user.getRoles().then(roles => { | ||
| const user = await User.findByPk(req.userId); | ||
| if (user) { | ||
| const roles = await user.getRoles(); | ||
| if (roles) { | ||
| for (let i = 0; i < roles.length; i++) { | ||
| if (roles[i].name === "moderator") { | ||
| next(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| res.status(403).send({ | ||
| message: "Require Moderator Role!" | ||
| }); | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| isModeratorOrAdmin = (req, res, next) => { | ||
| User.findByPk(req.userId).then(user => { | ||
| user.getRoles().then(roles => { | ||
| isModeratorOrAdmin = async (req, res, next) => { | ||
|
|
||
| const user = await User.findByPk(req.userId); | ||
| if (user) { | ||
| const roles = await user.getRoles(); | ||
| if (roles) { | ||
| for (let i = 0; i < roles.length; i++) { | ||
| if (roles[i].name === "moderator") { | ||
| next(); | ||
|
|
@@ -71,14 +78,13 @@ isModeratorOrAdmin = (req, res, next) => { | |
| next(); | ||
| return; | ||
| } | ||
| res.status(403).send({ | ||
| message: "Require Moderator or Admin Role!" | ||
| }); | ||
| } | ||
|
|
||
| res.status(403).send({ | ||
| message: "Require Moderator or Admin Role!" | ||
| }); | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const authJwt = { | ||
| verifyToken: verifyToken, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just added string in connection that's why i removed