-
Notifications
You must be signed in to change notification settings - Fork 12
Submitting token again #39
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
drewferris
wants to merge
65
commits into
sea-401d5-javascript:master
Choose a base branch
from
drewferris:submitting-token-again
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
65 commits
Select commit
Hold shift + click to select a range
8c07def
finished setting up
879b538
finished making CRUD operations
d1fc5e7
finished making CRUD operations
dc74c44
made the final tests work
cf34eb0
setup almost done
f181fed
made rooney file in schema
c59ff67
made rooney get request
09e6a01
made rooney post
84c3476
made rooney put
b58d92c
made rooney delete
ebbb76f
deleleted consts in server
1a979a9
made messi schema
838c1a8
made messi router setup
bdc6380
made messi router get request
3e8fd75
made messi router post request
4dd07b1
made messi router put request
d803610
made messi router delete request
9ff5d1e
made rooney router now a man united player router
e048b9c
made messi router a barca player router
39e0650
added position property to barca players
7a8ff40
added number and goals property to barca players
d4c3d71
added functionality to add total goals for each collection
75cd372
added most goals path and is working
9caaa86
started making manUnitedRoute file and modifying server and added err…
8c65448
finished modularizing
073ab87
finished deleting old commented out code in server and started tests
9e5fdac
declared consts in test
6984a8a
wrote united get test
7792315
wrote united post test
38fe85f
wrote united put test
effcbae
wrote next err for man u routes
7003eaf
wrote next err for barca routes
bf70da9
made man u delete tests
bc128a8
made barca tests
b8405ac
made compare route work again ready to start tests
3d2379f
started writing compare test but realized db is dropped evry time
984e12b
beginning compare test again
94cf2de
beginning compare test again
af739e9
made new test for compare
c1e8de6
put in notes for tics
78fa397
fixed linter errors
44a6d06
wrote user.js file and renamed schema folder to model
b14f614
wrote basic_http.js file
d4c595d
wrote auth_routes post request
9cbff38
wrote auth_routes login route
22cd0b5
updated server.js with auth route
138659f
changed schema to model in routes files
f6c68c0
changed schema to model in test also and started writing auth tessts
502e66d
declared neccessary consts for auth_test
5d9a0e3
wrote after block for authtests
f5bae51
wrote signup route test
3889bdb
wrote login route test
f3f3165
update package.json with jbt and started auth file
70a3e13
made jwt_auth file
b2756bf
updated user.js file with generate token method
22bd240
updated server.js with test routes
6471681
updated auth_routes with generate token function implemented
db4ba6d
error fix: changed req to res
2c393c5
token test file set up ready to write tests
daa1b4f
made change to server get test route so its test will work and is wor…
bc0b911
working on tests
0826652
got first route test towork for login
6d92d6e
made changes in package.json to scripts
176ab7b
fixed lintrc
1f40ee3
added token generator
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,41 @@ | ||
| { | ||
| "rules": { | ||
| "no-console": 0, | ||
| "indent": [ | ||
| 2, | ||
| 2 | ||
| ], | ||
| "quotes": [ | ||
| 2, | ||
| "single" | ||
| ], | ||
| "linebreak-style": [ | ||
| 2, | ||
| "unix" | ||
| ], | ||
| "semi": [ | ||
| 2, | ||
| "always" | ||
| ] | ||
| }, | ||
| "env": { | ||
| "es6": true, | ||
| "node": true, | ||
| "browser": true, | ||
| "mocha": true | ||
| }, | ||
| "globals": { | ||
| "describe": false, | ||
| "it": false, | ||
| "beforeEach": false, | ||
| "afterEach": false, | ||
| "before": false, | ||
| "after": false | ||
| }, | ||
| "ecmaFeatures": { | ||
| "modules": true, | ||
| "experimentalObjectRestSpread": true, | ||
| "impliedStrict": true | ||
| }, | ||
| "extends": "eslint:recommended" | ||
| } |
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,3 @@ | ||
| node_modules | ||
| data | ||
| db |
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,20 +1 @@ | ||
| #REST API | ||
|
|
||
| ##To Submit this Assignment | ||
| * fork this repository | ||
| * write all of your code in a folder containing your name | ||
| * push to your repository | ||
| * submit a pull request to this repository | ||
| * submit a link to your PR in canvas | ||
|
|
||
| ##Description | ||
| Create a single resource (like /penguins or /seals) REST API with Express. | ||
|
|
||
| Write a full set of CRUD routes for your resource. You can persist it however you like for the sake of testing, we'll be connecting it to Mongo later in the week. | ||
|
|
||
|
|
||
| ##Rubric | ||
| CRUD Routes: 4pts | ||
| Use of Express: 2pts | ||
| Tests: 2pts | ||
| Project Organization: 2pts |
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,26 @@ | ||
| const gulp = require('gulp'); | ||
| const eslint = require('gulp-eslint'); | ||
| const mocha = require('gulp-mocha'); | ||
|
|
||
| gulp.task('lint', () => { | ||
| gulp.src(['./*.js', './test/*.js']) | ||
| .pipe(eslint({ | ||
| env: {} | ||
| })) | ||
| .pipe(eslint.format()); | ||
| }); | ||
|
|
||
| gulp.task('mocha', () => { | ||
| return gulp.src('./test/*.js', { | ||
| read: false | ||
| }) | ||
| .pipe(mocha({ | ||
| reporter: 'nyan' | ||
| })); | ||
| }); | ||
|
|
||
| gulp.task('watch', () => { | ||
| gulp.watch(['./*.js', './test/*.js' ], ['mocha', 'lint']); | ||
| }); | ||
|
|
||
| gulp.task('default', ['mocha', 'lint', 'watch']); |
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,21 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = function (req, res, next) { | ||
| let basicAuth = req.headers.authorization; | ||
| let authString = basicAuth.split(' ').pop(); | ||
| let authBuff = new Buffer(authString, 'base64'); | ||
| let asciiAuth = authBuff.toString(); | ||
| let authArray = asciiAuth.split(':'); | ||
| authBuff.fill(0); | ||
|
|
||
| req.auth = { | ||
| username: authArray[0], | ||
| password: authArray[1] | ||
| }; | ||
|
|
||
| if(!req.auth.username || !req.auth.password) { | ||
| return next(new Error ('Username or Password missing')); | ||
| } | ||
|
|
||
| next(); | ||
| }; |
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,5 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = function(err, req, res) { | ||
| res.status(500).json({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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use strict'; | ||
|
|
||
| const jwt = require('jsonwebtoken'); | ||
| const User = require('../model/user'); | ||
| const secret = process.env.SECRET || 'changeme'; | ||
|
|
||
| module.exports = function (req, res, next) { | ||
| let token = req.body.token || req.headers.token; | ||
| let tokenErr = new Error('Authorization Failure'); | ||
| let decodedToken; | ||
|
|
||
| if (!token) return next(tokenErr); | ||
|
|
||
| try { | ||
| decodedToken = jwt.verify(token, secret); | ||
| } catch(e) { | ||
| return next(tokenErr); | ||
| } | ||
|
|
||
| User.findOne({_id:decodedToken._id}, (err, user) => { | ||
| if (!user || err) return next(tokenErr); | ||
| req.user = user; | ||
| next(); | ||
| }); | ||
| }; |
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,12 @@ | ||
| 'use strict'; | ||
|
|
||
| const mongoose = require('mongoose'); | ||
|
|
||
| const BarcaPlayer = new mongoose.Schema({ | ||
| name: String, | ||
| position: String, | ||
| number: Number, | ||
| goals: Number | ||
| }); | ||
|
|
||
| module.exports = mongoose.model('barca', BarcaPlayer); |
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,12 @@ | ||
| 'use strict'; | ||
|
|
||
| const mongoose = require('mongoose'); | ||
|
|
||
| const ManUnitedPlayer = new mongoose.Schema({ | ||
| name: String, | ||
| position: String, | ||
| number: Number, | ||
| goals: Number | ||
| }); | ||
|
|
||
| module.exports = mongoose.model('manUnitedPlayer', ManUnitedPlayer); |
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,25 @@ | ||
| 'use strict'; | ||
|
|
||
| const mongoose = require('mongoose'); | ||
| const bcrypt = require('bcrypt'); | ||
| const secret = process.env.SECRET || 'changeme'; | ||
| const jwt = require('jsonwebtoken'); | ||
|
|
||
| const User = new mongoose.Schema({ | ||
| username: {type: String, required: true}, | ||
| password: {type: String, required: true} | ||
| }); | ||
|
|
||
| User.methods.hashPassword = function() { | ||
| return bcrypt.hashSync(this.password, 8); | ||
| }; | ||
|
|
||
| User.methods.comparePassword = function(password) { | ||
| return bcrypt.compareSync(password, this.password); | ||
| }; | ||
|
|
||
| User.methods.generateToken = function() { | ||
| return jwt.sign({_id: this._id}, secret); | ||
| }; | ||
|
|
||
| module.exports = mongoose.model('user', User); |
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,43 @@ | ||
| 0 info it worked if it ends with ok | ||
| 1 verbose cli [ '/Users/drewferris/.node/bin/node', | ||
| 1 verbose cli '/Users/drewferris/.node/bin/npm', | ||
| 1 verbose cli 'run', | ||
| 1 verbose cli 'lint' ] | ||
| 2 info using npm@2.15.1 | ||
| 3 info using node@v4.4.3 | ||
| 4 verbose run-script [ 'prelint', 'lint', 'postlint' ] | ||
| 5 info prelint man-united@1.0.0 | ||
| 6 info lint man-united@1.0.0 | ||
| 7 verbose unsafe-perm in lifecycle true | ||
| 8 info man-united@1.0.0 Failed to exec lint script | ||
| 9 verbose stack Error: man-united@1.0.0 lint: `./node_modules/eslint/bin/eslint.js *` | ||
| 9 verbose stack Exit status 1 | ||
| 9 verbose stack at EventEmitter.<anonymous> (/Users/drewferris/.node/lib/node_modules/npm/lib/utils/lifecycle.js:217:16) | ||
| 9 verbose stack at emitTwo (events.js:87:13) | ||
| 9 verbose stack at EventEmitter.emit (events.js:172:7) | ||
| 9 verbose stack at ChildProcess.<anonymous> (/Users/drewferris/.node/lib/node_modules/npm/lib/utils/spawn.js:24:14) | ||
| 9 verbose stack at emitTwo (events.js:87:13) | ||
| 9 verbose stack at ChildProcess.emit (events.js:172:7) | ||
| 9 verbose stack at maybeClose (internal/child_process.js:827:16) | ||
| 9 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5) | ||
| 10 verbose pkgid man-united@1.0.0 | ||
| 11 verbose cwd /Users/drewferris/401/my_assignmnents/drew_rest_api | ||
| 12 error Darwin 14.5.0 | ||
| 13 error argv "/Users/drewferris/.node/bin/node" "/Users/drewferris/.node/bin/npm" "run" "lint" | ||
| 14 error node v4.4.3 | ||
| 15 error npm v2.15.1 | ||
| 16 error code ELIFECYCLE | ||
| 17 error man-united@1.0.0 lint: `./node_modules/eslint/bin/eslint.js *` | ||
| 17 error Exit status 1 | ||
| 18 error Failed at the man-united@1.0.0 lint script './node_modules/eslint/bin/eslint.js *'. | ||
| 18 error This is most likely a problem with the man-united package, | ||
| 18 error not with npm itself. | ||
| 18 error Tell the author that this fails on your system: | ||
| 18 error ./node_modules/eslint/bin/eslint.js * | ||
| 18 error You can get information on how to open an issue for this project with: | ||
| 18 error npm bugs man-united | ||
| 18 error Or if that isn't available, you can get their info via: | ||
| 18 error | ||
| 18 error npm owner ls man-united | ||
| 18 error There is likely additional logging output above. | ||
| 19 verbose exit [ 1, true ] |
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,45 @@ | ||
| { | ||
| "name": "man-united", | ||
| "version": "1.0.0", | ||
| "description": "Class Projest- Router", | ||
| "main": "index.js", | ||
| "directories": { | ||
| "test": "mocha" | ||
| }, | ||
| "scripts": { | ||
| "test": "./node_modules/mocha/bin/mocha", | ||
| "start": "node server.js", | ||
| "lint": "./node_modules/eslint/bin/eslint.js *" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/drewferris/man-united-router.git" | ||
| }, | ||
| "keywords": [ | ||
| "router", | ||
| "server" | ||
| ], | ||
| "author": "Drew Ferris", | ||
| "license": "ISC", | ||
| "bugs": { | ||
| "url": "https://github.com/drewferris/man-united-router/issues" | ||
| }, | ||
| "homepage": "https://github.com/drewferris/man-united-router#readme", | ||
| "devDependencies": { | ||
| "chai": "^3.5.0", | ||
| "chai-http": "^2.0.1", | ||
| "gulp": "^3.9.1", | ||
| "gulp-eslint": "^2.0.0", | ||
| "gulp-mocha": "^2.2.0", | ||
| "gulp-watch": "^4.3.5", | ||
| "mocha": "^2.4.5", | ||
| "mongoose": "^4.4.19", | ||
| "morgan": "^1.7.0" | ||
| }, | ||
| "dependencies": { | ||
| "bcrypt": "^0.8.6", | ||
| "body-parser": "^1.15.1", | ||
| "express": "^4.13.4", | ||
| "jsonwebtoken": "^7.0.0" | ||
| } | ||
| } |
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,31 @@ | ||
| 'use strict'; | ||
|
|
||
| const express = require('express'); | ||
| const bodyParser = require('body-parser').json(); | ||
| const User = require('../model/user'); | ||
| const basicHTTP = require('../lib/basic_http'); | ||
|
|
||
| const router = module.exports = exports = express.Router(); | ||
|
|
||
| router.post('/signup', bodyParser, (req, res, next) => { | ||
| let newUser = new User(req.body); | ||
| let hashedPassword = newUser.hashPassword(); | ||
| newUser.password = hashedPassword; | ||
| req.body.password = null; | ||
| User.findOne({username: req.body.username}, (err, user) => { | ||
| if(err || user) return next(new Error('could not create user')); | ||
| newUser.save((err, user) => { | ||
| if (err) return next(new Error('could not create user')); | ||
| res.json({token: user.generateToken()}); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| router.get('/login', basicHTTP, (req, res, next) => { | ||
| User.findOne({username: req.auth.username}, (err, user) => { | ||
| if(err || !user) return next(new Error('Could not login')); | ||
| if (!user.comparePassword(req.auth.password)) return next('Could not sign in'); | ||
|
|
||
| res.json({token: user.generateToken()}); | ||
|
Contributor
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. Good Job! |
||
| }); | ||
| }); | ||
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,41 @@ | ||
| 'use strict'; | ||
|
|
||
| const express = require('express'); | ||
| const bodyParser = require('body-parser').json(); | ||
| const BarcaPlayer = require('../model/barca_Player'); | ||
|
|
||
| const router = module.exports = exports = express.Router(); | ||
|
|
||
| router.get('/', (req, res) => { | ||
| BarcaPlayer.find({}, (err, data) => { | ||
| if(err) return res.json({ | ||
| message: err.message | ||
| }); | ||
| res.json(data); | ||
| }); | ||
| }); | ||
|
|
||
| router.post('/', bodyParser, (req, res) => { | ||
| let newBarcaPlayer = new BarcaPlayer(req.body); | ||
| newBarcaPlayer.save((err, data) => { | ||
| if(err) return res.json({message: err.message}); | ||
| res.json(data); | ||
| }); | ||
| }); | ||
|
|
||
| router.put('/', bodyParser, (req, res, next) => { | ||
| BarcaPlayer.findOneAndUpdate({_id: req.body._id}, req.body, (err) => { | ||
| if(err) return next(err); | ||
| let message = 'successfully updated'; | ||
| res.json({message}); | ||
| }); | ||
| }); | ||
|
|
||
| router.delete('/:id', bodyParser, (req, res, next) => { | ||
| let _id = req.params.id; | ||
| BarcaPlayer.findOneAndRemove({_id}, null, (err) => { | ||
| if(err) return next(err); | ||
| let message = 'successfully deleted'; | ||
| res.json({message}); | ||
| }); | ||
| }); |
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.
Good Job!