Skip to content

Commit 871a477

Browse files
updated config to use JSON file
1 parent 244e851 commit 871a477

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
node_modules/
44
dist/
55
package-lock.json
6+
src/config/config.json

src/config/config.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const env = process.env.NODE_ENV || 'development';
22

3-
if(env === 'development') {
4-
process.env.MONGODB_URI = 'mongodb://localhost:27018/TodoApp';
5-
process.env.PORT = '3001';
6-
} else if(env === 'test') {
7-
process.env.MONGODB_URI = 'mongodb://localhost:27018/TodoAppTest';
8-
process.env.PORT = '3001';
3+
if(env === 'development' || env === 'test') {
4+
const config = require('./config.json');
5+
const envConfig = config[env];
6+
Object.keys(envConfig).forEach((key) => {
7+
process.env[key] = envConfig[key];
8+
});
99
}

src/models/user.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
import mongoose from 'mongoose';
32
import validator from 'validator';
43
import jwt from 'jsonwebtoken';
54
import bcrypt from 'bcryptjs';
65

7-
let UserSchema = new mongoose.Schema({
6+
const UserSchema = new mongoose.Schema({
87
email: {
98
type: String,
109
required: true,
@@ -14,7 +13,7 @@ let UserSchema = new mongoose.Schema({
1413
validate: {
1514
validator: validator.isEmail,
1615
message: '{VALUE} is not a valid email'
17-
}
16+
},
1817
},
1918
password: {
2019
type: String,
@@ -29,40 +28,37 @@ let UserSchema = new mongoose.Schema({
2928
token: {
3029
type: String,
3130
required: true,
32-
}
33-
}]
31+
},
32+
}],
3433
});
3534

3635
UserSchema.methods.generateAuthToken = function () {
3736
const user = this;
3837
const access = 'auth';
3938
const {_id} = user;
40-
let token = jwt.sign({
41-
_id : _id.toHexString(),
42-
access
43-
}, '123ABC').toString();
39+
const token = jwt.sign({ _id : _id.toHexString(), access }, process.env.JWT_SECRET).toString();
4440
user.tokens = user.tokens.concat([{access, token}]);
4541
return user.save()
46-
.then(() => token)
42+
.then(() => token);
4743
};
4844

4945
UserSchema.methods.toJSON = function () {
5046
const user = this;
5147
const userObject = user.toObject();
52-
const {_id, email} = userObject;
53-
return {_id, email};
48+
const { _id, email } = userObject;
49+
return { _id, email };
5450
};
5551

5652
UserSchema.methods.removeToken = function (token) {
57-
let user = this;
58-
return user.update({$pull: {tokens: {token}}});
53+
const user = this;
54+
return user.update({ $pull: { tokens: { token } } });
5955
};
6056

6157
UserSchema.statics.findByToken = function (token) {
62-
let User = this;
58+
const User = this;
6359
let decoded;
6460
try {
65-
decoded = jwt.verify(token, '123ABC');
61+
decoded = jwt.verify(token, process.env.JWT_SECRET);
6662
} catch (err) {
6763
return Promise.reject();
6864
}
@@ -74,17 +70,17 @@ UserSchema.statics.findByToken = function (token) {
7470
};
7571

7672
UserSchema.statics.findByCredentials = function (email, password) {
77-
let User = this;
73+
const User = this;
7874
return User.findOne({email})
79-
.then((user) => {
80-
if(!user) return Promise.reject();
81-
return new Promise((resolve, reject) => {
82-
bcrypt.compare(password, user.password, (err, res) => {
83-
if(res) resolve(user);
84-
reject();
85-
})
86-
})
87-
})
75+
.then((user) => {
76+
if(!user) return Promise.reject();
77+
return new Promise((resolve, reject) => {
78+
bcrypt.compare(password, user.password, (err, res) => {
79+
if(res) resolve(user);
80+
reject();
81+
});
82+
});
83+
});
8884
};
8985

9086
UserSchema.pre('save', function (next) {

src/test/seed/seed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export const users = [
1313
password: 'userOnePass',
1414
tokens: [{
1515
access: 'auth',
16-
token: jwt.sign({ _id: userOneId, access: 'auth' }, '123ABC').toString(),
16+
token: jwt.sign({ _id: userOneId, access: 'auth' }, process.env.JWT_SECRET).toString(),
1717
}],
1818
}, {
1919
_id: userTwoId,
2020
email: 'adityasmksaxena@yahoo.com',
2121
password: 'userTwoPass',
2222
tokens: [{
2323
access: 'auth',
24-
token: jwt.sign({ _id: userTwoId, access: 'auth' }, '123ABC').toString(),
24+
token: jwt.sign({ _id: userTwoId, access: 'auth' }, process.env.JWT_SECRET).toString(),
2525
}],
2626
},
2727
];

0 commit comments

Comments
 (0)