-
Notifications
You must be signed in to change notification settings - Fork 26
Week 5 Auth Exercise completed with all the routes #10
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
marnel-mangrubang
wants to merge
1
commit into
pce-uw-jscript400:master
Choose a base branch
from
marnel-mangrubang:master
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
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,100 @@ | ||
| const REQUIRED_KEYS = [ 'username', 'password' ] | ||
| const { SECRET } = process.env | ||
| const jwt = require('jsonwebtoken') | ||
|
|
||
| //Middleware for validating account signup | ||
| const signupValidation = (req, res, next) => { | ||
| const {username, password, admin} = req.body | ||
|
|
||
| //Checks to see all required fields are provided | ||
| if (!req.body) next({ status: 401, message: 'Missing request POST body!' }) | ||
|
|
||
| //Checks to see if username is empty | ||
| if(!username) next({ status: 400, message: 'Please enter a username!' }) | ||
|
|
||
| //Checks to see if password is provided | ||
| if(!password){ | ||
| next({ status: 400, message: 'Please enter a password!' }) | ||
| }else{ | ||
| //If password is provided validate for its length, error out if less then minimum of 8 characters | ||
| if(password.length < 8) next({ status: 400, message: 'Invalid password length. Please enter at least 8 characters.' }) | ||
| } | ||
|
|
||
| next() | ||
| } | ||
|
|
||
| //This is the middleware to only allow admin users to change user permissions | ||
| const userAdmins = (req, res, next) => { | ||
| const {admin} = req.body | ||
| //Check to see if req body has the admin key and boolean value | ||
| if(!req.body) next({ status: 400, message: 'Request body does not include an admin key!' }) | ||
| if(admin !== false && admin !== true) next({ status: 400, message: 'The request body does not include an admin key with a boolean value!' }) | ||
|
|
||
| //Check to see if there is a auth bearer attached to the request | ||
| if(!req.headers.authorization) next({ status: 401, message: 'A valid JWT token is not provided' }) | ||
| const token = req.headers.authorization.split('Bearer ')[1] | ||
|
|
||
| //Verify that the token is a valid token | ||
| const payload = jwt.verify(token, SECRET) | ||
| console.log(payload.admin) | ||
| //Checks if the logged in user token is an admin | ||
| if(payload.admin === false) next({status:401, message: 'The JWT token is for a user who is not an admin'}) | ||
|
|
||
| next() | ||
|
|
||
| } | ||
|
|
||
|
|
||
| //This is the middleware for the book creation route to prevent non-admins from posting | ||
| const bookAdmins = (req, res, next) => { | ||
| //Check to see if there is a auth bearer attached to the request | ||
| if(!req.headers.authorization) next({ status: 401, message: 'A valid JWT token is not provided' }) | ||
| const token = req.headers.authorization.split('Bearer ')[1] | ||
|
|
||
| //Verify that the token is a valid token | ||
| const payload = jwt.verify(token, SECRET) | ||
| console.log(payload.admin) | ||
| //Checks if the logged in user token is an admin | ||
| if(payload.admin === false) next({status:401, message: 'You are not an ADMIN, you CANNOT access this route.'}) | ||
|
|
||
| next() | ||
| } | ||
|
|
||
|
|
||
|
|
||
| //This is the middleware for the book reserve route | ||
| const reserver = (req, res, next) => { | ||
| //Check to see if there is a auth bearer attached to the request | ||
| if(!req.headers.authorization) next({ status: 401, message: 'A valid JWT token is not provided.' }) | ||
| const token = req.headers.authorization.split('Bearer ')[1] | ||
|
|
||
| //Verify that the token is a valid token | ||
| const payload = jwt.verify(token, SECRET) | ||
|
|
||
| //Set userId to logged in users id so that I can use it in the route to set the reserved memberId to this. | ||
| req.userId = payload.id | ||
| if(!payload) next({status: 401, message: 'You are not allowed to RESERVE a book.'}) | ||
|
|
||
| next() | ||
| } | ||
|
|
||
|
|
||
| //This is the middleware for the book return route | ||
| const returner = (req, res, next) => { | ||
| //Check to see if there is a auth bearer attached to the request | ||
| if(!req.headers.authorization) next({ status: 401, message: 'A valid JWT token is not provided.' }) | ||
| const token = req.headers.authorization.split('Bearer ')[1] | ||
|
|
||
| //Verify that the token is a valid token | ||
| const payload = jwt.verify(token, SECRET) | ||
|
|
||
| //Set userId to logged in users id so that I can use it in the route to set the reserved memberId to this. | ||
| req.userId = payload.id | ||
| if(!payload) next({status: 401, message: 'You are not allowed to RETURN this book.'}) | ||
|
|
||
| next() | ||
| } | ||
|
|
||
|
|
||
|
|
||
| module.exports = { signupValidation, userAdmins, bookAdmins, reserver, returner } | ||
|
Collaborator
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. These are all excellent. Well done using middleware. I would say some of the names could use some work, but that's one of the hardest things to do. :) |
||
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| const mongoose = require('mongoose') | ||
|
|
||
| const userSchema = new mongoose.Schema({ | ||
| username: { | ||
| type:String, | ||
| required: true | ||
| }, | ||
| password: { | ||
| type: String, | ||
| required: true | ||
| }, | ||
| admin: { | ||
| type: Boolean, | ||
| default: false | ||
| } | ||
| }) | ||
|
|
||
| module.exports = mongoose.model('User', userSchema) |
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.
For all of these, I would add a
returnstatement here.