-
Notifications
You must be signed in to change notification settings - Fork 12
Add auth - Dan Stineback #35
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
dstineback
wants to merge
13
commits into
sea-401d5-javascript:master
Choose a base branch
from
dstineback:add-auth
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
13 commits
Select commit
Hold shift + click to select a range
0046633
added 2nd resource
dstineback 61aba4c
added more test
dstineback 416f899
fixed path issue
dstineback d91638a
trying to push after github started working
dstineback 1d18286
started to add a third router
dstineback f76283e
update
dstineback f8a4d66
update
dstineback 0058e9e
added auth routes
dstineback c3697e9
fixed all lint bugs
dstineback 5f37ec6
added test
dstineback ecc3835
changed some messages
dstineback 5e7aab2
commented out auth for class
dstineback 9a1e233
added auth again
dstineback 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,6 @@ | ||
| **/node_modules/* | ||
| **/vendor/* | ||
| **/*.min.js | ||
| /*.md | ||
| /package.json | ||
| /npm-debug.log |
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| node_modules | ||
| /node_modules | ||
| /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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| '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,27 @@ | ||
| '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.headers.token || req.body.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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 'use strict'; | ||
|
|
||
| const mongoose = require('mongoose'); | ||
|
|
||
| const Cat = new mongoose.Schema({ | ||
| name: String, | ||
| age: Number | ||
| }); | ||
|
|
||
| module.exports = mongoose.model('cat', Cat); |
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,10 @@ | ||
| 'use strict'; | ||
|
|
||
| const mongoose = require('mongoose'); | ||
|
|
||
| const Dog = new mongoose.Schema({ | ||
| name: String, | ||
| age: Number | ||
| }); | ||
|
|
||
| module.exports = mongoose.model('dog', Dog); |
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.hashedPassword = 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
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,40 @@ | ||
| 'use strict'; | ||
| const express = require('express'); | ||
| const Cat = require('../model/cats'); | ||
| const bodyParser = require('body-parser').json(); | ||
| const jwtAuth = require('../lib/jwt_auth'); | ||
|
|
||
|
|
||
| const catRouter = module.exports = exports = express.Router(); | ||
|
|
||
|
|
||
| catRouter.get('/', (req, res, next) => { | ||
| Cat.find({}, (err, cat) => { | ||
| if(err) return next(err); | ||
| res.json(cat); | ||
| }); | ||
| }); | ||
|
|
||
| catRouter.post('/', bodyParser, jwtAuth, (req, res, next) => { | ||
| let newCat = new Cat(req.body); | ||
| newCat.save((err, cat)=> { | ||
| if (err) return next(err); | ||
| res.json(cat); | ||
| }); | ||
| }); | ||
|
|
||
| catRouter.put('/', bodyParser, jwtAuth, (req, res, next) => { | ||
| let _id = req.body._id; | ||
| Cat.findOneAndUpdate({_id}, req.body, (err, cat) =>{ | ||
| if (err) return next(err); | ||
| res.json({message:'successfully updated', data: cat}); | ||
| }); | ||
| }); | ||
|
|
||
| catRouter.delete('/:id', jwtAuth, (req, res, next) => { | ||
| let _id = req.params.id; | ||
| Cat.findOneAndRemove({_id}, null, (err, cat) => { | ||
| if (err) return next(err); | ||
| res.json({message: 'successfully deleted', data: cat}); | ||
| }); | ||
| }); |
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,39 @@ | ||
| 'use strict'; | ||
| const express = require('express'); | ||
| const Dog = require('../model/dogs'); | ||
| const bodyParser = require('body-parser').json(); | ||
| const jwtAuth = require('../lib/jwt_auth'); | ||
|
|
||
| const dogRouter = module.exports = exports = express.Router(); | ||
|
|
||
|
|
||
| dogRouter.get('/', (req, res, next) => { | ||
| Dog.find({}, (err, dog) => { | ||
| if(err) return next(err); | ||
| res.json(dog); | ||
| }); | ||
| }); | ||
|
|
||
| dogRouter.post('/', bodyParser, jwtAuth, (req, res, next) => { | ||
| let newDog = new Dog(req.body); | ||
| newDog.save((err, dog)=> { | ||
| if (err) return next(err); | ||
| res.json(dog); | ||
| }); | ||
| }); | ||
|
|
||
| dogRouter.put('/', bodyParser, jwtAuth, (req, res, next) => { | ||
| let _id = req.body._id; | ||
| Dog.findOneAndUpdate({_id}, req.body, (err, dog) =>{ | ||
| if (err) return next(err); | ||
| res.json({message: 'successfully updated', data: dog}); | ||
| }); | ||
| }); | ||
|
|
||
| dogRouter.delete('/:id', jwtAuth, (req, res, next) => { | ||
| let _id = req.params.id; | ||
| Dog.findOneAndRemove({_id}, null, (err, dog) => { | ||
| if (err) return next(err); | ||
| res.json({message: 'successfully deleted', data: dog}); | ||
| }); | ||
| }); |
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,30 @@ | ||
| '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.hashedPassword(); | ||
| newUser.password = hashedPassword; | ||
| req.body.password = null; | ||
| User.findOne({username: req.body.username}, (err, user) => { | ||
| if (err || user) return next(new Error('User name already used')); | ||
| newUser.save((err, user) => { | ||
| if (err) return next(new Error('Could not save user')); | ||
| res.json({message: req.body.username + ' We mad a token for you', token: user.generateToken()}); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| router.post('/signin', basicHTTP, (req, res, next) => { | ||
| User.findOne({username: req.auth.username}, (err, user) => { | ||
| if (err || !user) return next(new Error('Could not sign in')); | ||
| if (!user.comparePassword(req.auth.password)) return next(new Error('wrong password')); | ||
|
|
||
| res.json({message: req.auth.username + ' Here is your token', token: user.generateToken()}); | ||
| }); | ||
| }); | ||
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 app = express(); | ||
| const mongoose = require('mongoose'); | ||
| const morgan = require('morgan'); | ||
| const cors = require('cors'); | ||
|
|
||
|
|
||
| const dbPort = process.env.MONGODB_URI || 'mongodb://localhost/dev_db'; | ||
| mongoose.connect(dbPort); | ||
|
|
||
| const dogRouter = require(__dirname + '/routes/dog-route'); | ||
| const catRouter = require(__dirname + '/routes/cat-route'); | ||
| const authRouter = require(__dirname + '/routes/router'); | ||
|
|
||
| app.use(cors()); | ||
| app.use(morgan('dev')); | ||
| app.use('/dogs', dogRouter); | ||
| app.use('/cats', catRouter); | ||
| app.use('/', authRouter); | ||
|
|
||
| app.use((err, req, res, next) => { | ||
| res.status(500).json({message: err.message}); | ||
| next(err); | ||
| }); | ||
|
|
||
| app.use((req, res)=> { | ||
| res.status(404).json({message: 'not found'}); | ||
| }); | ||
| app.listen(3000, () => console.log('server is up on 3000')); |
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.
We made a token..