Skip to content

Commit

Permalink
dotenv set up
Browse files Browse the repository at this point in the history
  • Loading branch information
jwill9999 committed Jul 7, 2017
1 parent 44903bb commit 922c329
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ typings/
# dotenv environment variables file
.env

#configuration
.configuration.js



# End of https://www.gitignore.io/api/node
11 changes: 5 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const Now = require('./mymiddleware');

const Cors = require('cors');

const index = require('./routes/index');
const users = require('./routes/users');

const mongoose = require('mongoose');

/************************db setup*********************************/
/********add and replace your own database connection here********/
/*****************************************************************/
mongoose.connect('mongodb://localhost:auth/authorization');
/************************db setup***********************************************/
/********add and replace your own database connection here in .env file ********/
/*******************************************************************************/
mongoose.connect(process.env.DATABASE_CONNECTION);


const app = express();
Expand All @@ -29,7 +29,6 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(Now);
app.use(Cors());

app.use('/', index);
Expand Down
4 changes: 4 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env node

//required for npm module dotenv to work
require('dotenv/config');
require('./www');

/**
* Module dependencies.
*/
Expand Down
3 changes: 0 additions & 3 deletions configuration.js

This file was deleted.

4 changes: 2 additions & 2 deletions controllers/authentication.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const User = require('../models/user');
const jwt = require('jwt-simple');
const JWT_SECRET = require('../configuration');



//create jwt-token and set subject, timestamp and secret
function createJwtToken(user) {
const timestamp = new Date().getDate();
return jwt.encode({ sub: user.id, iat: timestamp }, JWT_SECRET.ID)
return jwt.encode({ sub: user.id, iat: timestamp }, process.env.JWT_SECRET)
};


Expand Down
11 changes: 0 additions & 11 deletions mymiddleware.js

This file was deleted.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"start": "nodemon ./bin/www",
"dev": "nodemon ./bin/www"
},
"author": {
Expand All @@ -16,18 +16,20 @@
"cookie-parser": "~1.4.3",
"cors": "^2.8.3",
"debug": "~2.6.3",
"dotenv": "^4.0.0",
"ejs": "~2.5.6",
"express": "~4.15.2",
"jwt-simple": "^0.5.1",
"mongoose": "^4.9.8",
"morgan": "~1.8.1",
"nodemon": "^1.11.0",
"passport": "^0.3.2",
"passport-jwt": "^2.2.1",
"passport-local": "^1.0.0",
"serve-favicon": "~2.4.2"
},
"devDependencies": {
"nodemon": "^1.11.0"

},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions services/passport_jwt.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const passport = require('passport');
const User = require('../models/user');
const JWT_SECRET = require('../configuration');

const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;


//Options for jwt_strategy note key in header for token is authorization
const JwtOptions = {
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: JWT_SECRET.ID
secretOrKey: process.env.JWT_SECRET
};

//Passport_jwt strategy 1. create new strategy 2. supply options as above 3.find user by sub which is user.id
Expand Down

0 comments on commit 922c329

Please sign in to comment.