-
Notifications
You must be signed in to change notification settings - Fork 26
Tim Willis HW5 #17
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
t1mwillis
wants to merge
4
commits into
pce-uw-jscript400:master
Choose a base branch
from
t1mwillis: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
Tim Willis HW5 #17
Changes from all commits
Commits
Show all changes
4 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,18 @@ | ||
| const mongoose = require('mongoose') | ||
|
|
||
| const schema = mongoose.Schema({ | ||
| username: { | ||
| type: String, | ||
| required: true | ||
| }, | ||
| password : { | ||
| type: String, | ||
| requried: true | ||
| }, | ||
| admin: { | ||
| type: Boolean, | ||
| default: false | ||
| } | ||
| }) | ||
|
|
||
| module.exports = mongoose.model('User', schema) |
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,115 @@ | ||
| const { SECRET_KEY } = process.env | ||
| const router = require('express').Router() | ||
| const User = require('../models/user') | ||
| const bcrypt = require('bcrypt') | ||
| const jsonwebtoken = require('jsonwebtoken') | ||
|
|
||
| router.post('/signup', async (req,res, next) => { | ||
| const status = 201 | ||
|
|
||
| try { | ||
| const { username, password } = req.body | ||
|
|
||
| //throw errors if malformed input | ||
| if (!(username && password)) throw new Error(`Username & password are required!`) | ||
| if (password.length < 8) throw new Error(`Please choose a longer password`) | ||
|
|
||
| const guest = await User.findOne({username}) | ||
|
|
||
| //if user already exists, throw error | ||
| if (guest) throw new Error(`User: ${username} already exists!`) | ||
|
|
||
| //store user in database | ||
| const saltRounds = 10 | ||
| const hashed = await bcrypt.hash(password, saltRounds) | ||
| const user = await User.create({ | ||
| username, | ||
| password: hashed | ||
| }) | ||
| console.log(`User ${username} created!`) | ||
|
|
||
| //return success | ||
| const payload = { id: user._id } //setup payload | ||
| const options = { expiresIn: '1 day' } //add expiration | ||
| const token = jsonwebtoken.sign(payload, SECRET_KEY, options) //create token | ||
|
|
||
| res.status(status).json({status, token}) | ||
|
|
||
| } catch (e) { | ||
| e.status = 400 | ||
| next(e) | ||
| } | ||
| }) | ||
|
|
||
| router.post('/login', async (req, res, next)=> { | ||
| const status = 201 | ||
| try{ | ||
| const { username, password } = req.body | ||
| //throw error if no username | ||
| if (!username) throw new Error(`Username required to login`) | ||
| const guest = await User.findOne({username}) | ||
| const isValid = await bcrypt.compare(password, guest.password) | ||
| //throw error if issue with username/password | ||
| if (!isValid) throw new Error(`Username and password do not match`) | ||
|
|
||
| const payload = { id: guest._id } | ||
| const options = { expiresIn: '1 day' } | ||
| const token = jsonwebtoken.sign(payload, SECRET_KEY, options) | ||
| res.status(status).json({status, token}) | ||
| } catch (e) { | ||
| e.status = 401 | ||
| next(e) | ||
| } | ||
| }) | ||
|
|
||
| router.patch('/users/:id/permissions', async (req, res, next) => { | ||
| const status = 204 | ||
| try { | ||
| //make sure request body is OK - 400 | ||
| const permissions = req.body.admin | ||
| const { id } = req.params | ||
| if ( !(permissions === "true" || permissions === "false")) { | ||
| const error = new Error(`There was a problem with your request body`) | ||
| error.status = 400 | ||
| next(error) | ||
| } | ||
|
|
||
| //make sure headers contain auth - 401 | ||
| const token = req.headers.authorization.split('Bearer ')[1] | ||
| if (!token) { | ||
| const error = new Error(`There was a problem with your request`) | ||
| error.status = 401 | ||
| next(error) | ||
| } | ||
|
|
||
| const payload = jsonwebtoken.verify(token, SECRET_KEY) | ||
|
|
||
| const user = await User.findOne({ _id: payload.id }).select('-__v -password') | ||
| //make sure request maker is an admin - 401 | ||
| const { admin } = user | ||
| if (admin !== true) { | ||
| const error = new Error(`There was a problem with your request`) | ||
| error.status = 401 | ||
| next(error) | ||
| } | ||
|
|
||
| //make sure user exists - 404 | ||
| const updatedUser = await User.findOne({_id: id}) | ||
| if (!updatedUser) { | ||
| const error = new Error(`User ID: ${id} does not exist!`) | ||
| error.status = 404 | ||
| next(error) | ||
| } | ||
|
|
||
| //need to verify this and make sure it is correct way to do.. | ||
| updatedUser.admin = permissions | ||
| await updatedUser.save() | ||
|
|
||
| res.status(status).send() | ||
|
|
||
| } catch (e) { | ||
| next(e) | ||
| } | ||
| }) | ||
|
|
||
| module.exports = router |
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
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.
This is what you want to use for whether or not the person is authorized. Just having a token is not enough.