Skip to content
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

managing global configuration #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ node_modules/
jspm_packages/

# Configuration file
src/config.ts
config/default.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the default.json exist by default?
The default flow should work without any modifications to the repo. And if the user wants to change the default mechanism, we will have to add in another json, and pass that to the config library (through env or any other mechanism)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will update the PR


# TypeScript v1 declaration files
typings/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- Install dependencies: ``` npm install ```


**Note:** Make sure to add a `config.js` file in the `src` folder. See the `config.example.js` under `src/` directory for more details.
**Note:** Make sure to add a `default.json` file in the `config` folder. Refer to `config/example.json` for an example. For production, create a `production.json` and follow the same format.



Expand Down
12 changes: 12 additions & 0 deletions config/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"app": {
"PORT": 4000,
"auth": {
"secretKey": "<Your Secret Here>",
"tokenExpiry": 1
}
},
"db": {
"mongoUrl": "mongodb://127.0.0.1:27017/posterior"
}
}
25 changes: 23 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"homepage": "https://github.com/pes-alcoding-club/posterior#readme",
"dependencies": {
"config": "^3.3.1",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.22",
Expand All @@ -41,6 +42,7 @@
"passport-local-mongoose": "^6.0.1"
},
"devDependencies": {
"@types/config": "0.0.36",
"@types/express": "^4.17.7",
"@types/mongoose": "^5.7.31",
"@types/node": "^14.0.22",
Expand Down
12 changes: 8 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import mongoose from 'mongoose';

import passport from 'passport';

import config from './config';
import config from 'config';

import AuthRouter from './routers/auth.router';

Expand All @@ -12,18 +12,22 @@ app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(passport.initialize());

app.listen(config.PORT, (err: any) => {
const appConfig : any = config.get('app');

app.listen(appConfig.PORT, (err: any) => {
if (err) {
console.error(`Unable to start app. Found error: ${err.message}`);
return;
}
console.error(`Server Running at PORT: ${config.PORT}`);
console.error(`Server Running at PORT: ${appConfig.PORT}`);
});

app.use('/api/auth', AuthRouter);

const dbConfig : any = config.get('db');

mongoose.set('useCreateIndex', true);
mongoose.connect(config.mongoUrl, {
mongoose.connect(dbConfig.mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
Expand Down
5 changes: 0 additions & 5 deletions src/config.example.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/middleware/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ const ExtractJwt = passportJwt.ExtractJwt;

import User from '../models/user.model';

import config from '../config';
import config from 'config';

import { NextFunction } from 'express';

export const local = passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

const authConfig : any = config.get('app.auth');

const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.secretKey
secretOrKey: authConfig.secretKey
};

export const jwtPasport = passport.use(
Expand Down
6 changes: 4 additions & 2 deletions src/utils/auth.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import jwt from 'jsonwebtoken';
import config from '../config';
import config from 'config';

const authConfig : any = config.get('app.auth');

export const getToken = function (user: any) {
return jwt.sign(user, config.secretKey, { expiresIn: 30 * 24 * 3600 }); // Expires in 30 days
return jwt.sign(user, authConfig.secretKey, { expiresIn: authConfig.tokenExpiry * 24 * 3600 });
};