Skip to content

Commit 6278f6a

Browse files
Ashley BibizadehAshley Bibizadeh
authored andcommitted
Switch to config dep to handle env vars
1 parent 4b7d38f commit 6278f6a

File tree

8 files changed

+70
-27
lines changed

8 files changed

+70
-27
lines changed

config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const config = require('config');
2+
3+
module.exports = function () {
4+
if (!config.get('dbString')) {
5+
throw new Error('Environment variable \'dbString\' is not defined');
6+
}
7+
8+
if (!config.get('jwtPrivateKey')) {
9+
throw new Error('Environment variable \'jwt_secret_key\' is not defined');
10+
}
11+
12+
if (!config.get('mailServer.auth.user')) {
13+
throw new Error('Environment variable \'mailServer_user\' is not defined');
14+
}
15+
16+
if (!config.get('mailServer.auth.pass')) {
17+
throw new Error('Environment variable \'mailServer_pass\' is not defined');
18+
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dbString": "db_connection_string",
3+
"jwtPrivateKey": "jwt_secret_key",
4+
"mailServer": {
5+
"auth": {
6+
"user": "mailServer_user",
7+
"pass": "mailServer_pass"
8+
}
9+
}
10+
}

config/default.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"dbString": "",
3+
"jwtPrivateKey": "",
4+
"mailServer": {
5+
"host": "-HOST-",
6+
"auth": {
7+
"user": "",
8+
"pass": ""
9+
},
10+
"from": "-FROM-EMAIL-",
11+
"subject": "Password Reset"
12+
}
13+
}

config/test.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"dbString": "mongodb://localhost/playground_tests",
3+
"jwtPrivateKey": "1234",
4+
"mailServer": {
5+
"host": "[email protected]",
6+
"auth": {
7+
"user": "test",
8+
"pass": "123"
9+
},
10+
"from": "Test",
11+
"subject": "Password Reset"
12+
}
13+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
}
3232
},
3333
"dependencies": {
34+
"config": "3.1.0",
3435
"apollo-boost": "0.3.1",
3536
"apollo-cache-inmemory": "1.5.1",
3637
"apollo-client": "2.5.1",

server.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'babel-polyfill';
22
import 'isomorphic-unfetch';
3-
require('dotenv').config({ path: 'variables.env' });
3+
import config from 'config';
44
import path from 'path';
55
import fs from 'fs';
66
import express from 'express';
@@ -33,12 +33,15 @@ import { resolvers } from './src/resolvers';
3333
import User from './src/models/User';
3434

3535
// Connect MongoDB
36-
mongoose.connect(process.env.DB_CONNECTION_STRING, { useNewUrlParser: true }).then(() => {
36+
mongoose.connect(config.get('dbString'), { useNewUrlParser: true }).then(() => {
3737
console.log('Connection to DB successful');
3838
}).catch(err => {
3939
console.log(`Connection to DB Error: ${err}`);
4040
});
4141

42+
// check env vars
43+
require('./config')();
44+
4245
const app = express();
4346
const PORT = process.env.PORT || 3000;
4447

@@ -76,7 +79,7 @@ app.use(async (req, res, next) => {
7679
const token = req.cookies.token ? req.cookies.token : null;
7780
if (token !== null) {
7881
try {
79-
const currentUser = await jwt.verify(token, process.env.JWT_SECRET);
82+
const currentUser = await jwt.verify(token, config.get('jwtPrivateKey'));
8083
req.currentUser = currentUser;
8184
} catch (err) {
8285
// console.error(err);
@@ -159,10 +162,10 @@ app.get(['*/:param', '*'], (req, res) => {
159162
app.post('/password-reset', (req, response) => {
160163

161164
var mailer = nodemailer.createTransport({
162-
host: process.env.NODEMAILER_HOST,
165+
host: config.get('mailServer.host'),
163166
auth: {
164-
user: process.env.NODEMAILER_AUTH_USER,
165-
pass: process.env.NODEMAILER_AUTH_PW
167+
user: config.get('mailServer.auth.user'),
168+
pass: config.get('mailServer.auth.pass')
166169
}
167170
});
168171

@@ -172,9 +175,9 @@ app.post('/password-reset', (req, response) => {
172175
}));
173176

174177
mailer.sendMail({
175-
from: process.env.NODEMAILER_FROM_EMAIL,
178+
from: config.get('mailServer.from'),
176179
to: req.body.email,
177-
subject: 'React Starter Kit - Password Reset',
180+
subject: config.get('mailServer.subject'),
178181
template: 'passwordReset',
179182
context: {
180183
email: req.body.email,

src/resolvers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const bcrypt = require('bcrypt');
33
var generator = require('generate-password');
44
const axios = require('axios');
55
const webConfig = require('./../webConfig');
6+
const config = require('config');
67

78
const createToken = (user, secret, expiresIn) => {
89

@@ -67,7 +68,7 @@ exports.resolvers = {
6768
password
6869
}).save();
6970

70-
return { token: createToken(newUser, process.env.JWT_SECRET, "1hr") };
71+
return { token: createToken(newUser, config.get('jwtPrivateKey'), "1hr") };
7172
},
7273

7374
signinUser: async (root, { email, password }, { User }) => {
@@ -83,7 +84,7 @@ exports.resolvers = {
8384
throw new Error('inValid password');
8485
}
8586

86-
return { token: createToken(user, process.env.JWT_SECRET, "1hr") };
87+
return { token: createToken(user, config.get('jwtPrivateKey'), "1hr") };
8788

8889
},
8990

variables.env

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)