Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
fccd2da
post request working, all files initialized
stefuhnee May 24, 2016
24be540
get and post requests working
stefuhnee May 24, 2016
fa363bd
delete route working
stefuhnee May 24, 2016
97ed4cb
put request finished
stefuhnee May 24, 2016
5d7d38d
made all generic methods dealing with reading and writing files into …
stefuhnee May 24, 2016
93588a6
tests complete for all methods
stefuhnee May 24, 2016
51a793c
tests passing, now post doesn't overwrite a file if it already exists
stefuhnee May 24, 2016
a9bc017
reorganized
stefuhnee May 24, 2016
247fb2b
removed unnecessary console log
stefuhnee May 24, 2016
72875a1
took out unnecessary res.end()s
stefuhnee May 24, 2016
bae8270
removed unnecessary res.end in server.js
stefuhnee May 24, 2016
f2fb347
mongo works with all routes correctly
stefuhnee May 26, 2016
b7a5306
mapping out second resources- uses
stefuhnee May 26, 2016
4b99def
added second resource
stefuhnee May 27, 2016
c10bc77
tests mostly done, just need nonstandard crud route
stefuhnee May 27, 2016
ba274e0
added non-crud type route to represent range of zones
stefuhnee May 27, 2016
6a7032f
various linter fixes
stefuhnee May 27, 2016
0f9332d
reorganized project to fit class standards
stefuhnee Jun 2, 2016
0af18be
Merge pull request #1 from stefuhnee/two-resource
stefuhnee Jun 2, 2016
88a744e
added more files for user auth and project organization
stefuhnee Jun 2, 2016
783666b
added user model and bcrypt to package.json
stefuhnee Jun 2, 2016
dc47d38
signin route complete, jwt added to dependencies
stefuhnee Jun 2, 2016
6fe3fb6
basic auth middleware finished
stefuhnee Jun 2, 2016
956d37c
signup route finished
stefuhnee Jun 2, 2016
f1ac52b
added jwt-auth helper
stefuhnee Jun 2, 2016
cf3a94a
routes working
stefuhnee Jun 2, 2016
1be4a06
init auth tests page
stefuhnee Jun 2, 2016
f8b9b8d
plant tests working, trying to get promises to work
stefuhnee Jun 3, 2016
b434cdf
all tests passing, need to figure out promises and jwt
stefuhnee Jun 3, 2016
c62470b
fixing linter errors
stefuhnee Jun 3, 2016
55208ba
back to old way
stefuhnee Jun 3, 2016
2049ebb
delete posts are now jwt authorized
stefuhnee Jun 3, 2016
06c2162
jwt auth test done
stefuhnee Jun 3, 2016
a21bf44
can't get promises to work correctly
stefuhnee Jun 3, 2016
9ff7662
linter errors, error handling changed
stefuhnee Jun 3, 2016
44f983c
added error checking for jwt auth function
stefuhnee Jun 4, 2016
c93d71e
Merge pull request #2 from stefuhnee/user-auth
stefuhnee Jun 20, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stefanie-hansen/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
/*.md
/package.json
41 changes: 41 additions & 0 deletions stefanie-hansen/.eslintrc
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"
}
86 changes: 86 additions & 0 deletions stefanie-hansen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

# application specific
db/

# Created by https://www.gitignore.io/api/node,osx,vim

### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history


### OSX ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Vim ###
# swap
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags
1 change: 1 addition & 0 deletions stefanie-hansen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Authenticated two-resource REST API
62 changes: 62 additions & 0 deletions stefanie-hansen/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const gulp = require('gulp');
const mocha = require('gulp-mocha');
const lint = require('gulp-eslint');
const opts = {
'extends': 'eslint:recommended',
'ecmaFeatures': {
'modules': true
},
'rules': {
'no-alert': 0,
'no-bitwise': 0,
'camelcase': 1,
'no-console': 1,
'curly': 1,
'eqeqeq': 0,
'no-eq-null': 0,
'guard-for-in': 1,
'no-empty': 1,
'no-use-before-define': 0,
'no-obj-calls': 2,
'no-unused-vars': 0,
'new-cap': 1,
'no-shadow': 0,
'strict': 1,
'no-invalid-regexp': 2,
'comma-dangle': 2,
'no-undef': 1,
'no-new': 1,
'no-extra-semi': 1,
'no-debugger': 2,
'no-caller': 1,
'semi': 1,
'quotes': 0,
'no-unreachable': 2
},
'globals': {
'$': false
},
'env': {
'node': true,
'es6': true
}
};


gulp.task('linter', () => {
return gulp.src(['./*.js', './model/*.js', './route/*.js', './test/*.js', './lib/*.js'])
.pipe(lint(opts))
.pipe(lint.format());
});

gulp.task('tests', () => {
return gulp.src(['./test/*.js'], {read: false})
.pipe(mocha({reporter: 'nyan'}));
});

gulp.task('watch', () => {
gulp.watch(['./*.js', './model/*.js', './route/*.js', './test/*.js', './lib/*.js'], ['linter', 'tests']);
});

gulp.task('default', ['linter', 'tests', 'watch'], () => {
});
18 changes: 18 additions & 0 deletions stefanie-hansen/lib/basic-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = function(req, res, next) {
let authString = req.headers.authorization.split(' ').pop();
let userBuf = new Buffer(authString, 'base64');
let userArr = userBuf.toString().split(':');
userBuf.fill(0);

req.auth = {
username: userArr[0],
password: userArr[1]
};

if (!req.auth.username || !req.auth.password) {
return next(new Error('Username or Password missing'));
}
next();
};
22 changes: 22 additions & 0 deletions stefanie-hansen/lib/jwt-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const jwt = require('jsonwebtoken');
const User = require('../model/user');
const secret = process.env.SECRET || 'testPass';

module.exports = function(req, res, next) {
let token = req.headers.token || req.body.token;
if (!token) return next(new Error('No token provided'));

try {
token = jwt.verify(token, secret);
} catch(err) {
return next(new Error('Invalid token'));
}

User.findOne({_id: token}, (err, user) => {
if (err) return next(new Error('Cannot find user'));
req.user = user;
next();
});
};
14 changes: 14 additions & 0 deletions stefanie-hansen/model/plant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

const Plant = mongoose.Schema({
commonName: {type: String, required: true},
scientificName: {type: String, required: true},
medicinalUses: {type: Array, required: true},
nutritionalValue: {type: Array, required: true},
zone: {type: Number, required: true}
});

module.exports = mongoose.model('plant', Plant);
12 changes: 12 additions & 0 deletions stefanie-hansen/model/supplement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

const Supplement = mongoose.Schema({
name: {type: String, required: true},
medicinalEffects: {type: Array, required: true},
sideEffects: {type: Array, required: false}
});

module.exports = mongoose.model('supplement', Supplement);
26 changes: 26 additions & 0 deletions stefanie-hansen/model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const secret = process.env.SECRET || 'testPass';

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);
36 changes: 36 additions & 0 deletions stefanie-hansen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "stefanie-hansen",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^0.8.6",
"express": "^4.13.4",
"jsonwebtoken": "^7.0.0",
"mongoose": "^4.4.19"
},
"devDependencies": {
"body-parser": "^1.15.1",
"chai": "^3.5.0",
"chai-http": "^2.0.1",
"eslint": "^2.10.2",
"eslint-config-standard": "^5.3.1",
"eslint-plugin-promise": "^1.1.0",
"eslint-plugin-standard": "^1.3.2",
"gulp": "^3.9.1",
"gulp-eslint": "^2.0.0",
"gulp-mocha": "^2.2.0",
"mocha": "^2.5.2",
"morgan": "^1.7.0"
},
"directories": {
"test": "test"
},
"description": ""
}
31 changes: 31 additions & 0 deletions stefanie-hansen/route/auth-routes.js
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 basicAuth = require('../lib/basic-auth');
const User = require('../model/user');
const router = module.exports = express.Router();

router.get('/login', basicAuth, (req, res, next) => {
let username = req.auth.username;
User.findOne({username}, (err, user) => {
if (err || !user) return next(new Error('Cannot find user'));
if (!user.comparePassword(req.auth.password)) {
return next(new Error('Invalid password'));
}
return res.json({token: user.generateToken()});
});
});

router.post('/signup', bodyParser, (req, res, next) => {
let newUser = new User(req.body);
newUser.password = newUser.hashPassword();
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()});
});
});
});
Loading