From 36b26ed8a7e071e76021154d8843fd81b39e1f0d Mon Sep 17 00:00:00 2001 From: "Umang G. Patel" <23169768+robonetphy@users.noreply.github.com> Date: Sat, 23 Apr 2022 12:03:32 +0530 Subject: [PATCH] replace multiple dotenv config with one --- src/backend/app.ts | 2 ++ src/backend/routes/auth.ts | 9 ++------- src/backend/routes/middlewares/token.ts | 2 -- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/backend/app.ts b/src/backend/app.ts index 57836d0..c5f5289 100644 --- a/src/backend/app.ts +++ b/src/backend/app.ts @@ -5,7 +5,9 @@ import morgan from 'morgan'; import rcParser from './utils/rcparser'; import routes from './routes'; import HttpException from './exceptions/httpException'; +import * as dotenv from 'dotenv'; +dotenv.config(); const app = express(); const config = rcParser.getConfiguration(); diff --git a/src/backend/routes/auth.ts b/src/backend/routes/auth.ts index e09d522..8ee3a66 100644 --- a/src/backend/routes/auth.ts +++ b/src/backend/routes/auth.ts @@ -2,9 +2,6 @@ import express, { Request, Response } from 'express'; import jwt from 'jsonwebtoken'; import config from 'config'; import csrf from 'csurf'; -import * as dotenv from 'dotenv'; - -dotenv.config(); const router = express.Router(); const csrfProtection = csrf({ cookie: true }); @@ -25,8 +22,6 @@ router.get('/auth', csrfProtection, function (req: Request, res: Response) { */ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Response) => { try { - const password = process.env.PASSWORD; - if (!process.env.PASSWORD) { res.render('auth', { title: 'Login page', @@ -37,7 +32,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon return; } - if (req.body.password !== password) { + if (req.body.password !== process.env.PASSWORD) { res.render('auth', { title: 'Login page', header: 'Wrong password', @@ -51,7 +46,7 @@ router.post('/auth', parseForm, csrfProtection, async (req: Request, res: Respon iss: 'Codex Team', sub: 'auth', iat: Date.now(), - }, password + config.get('secret')); + }, process.env.PASSWORD + config.get('secret')); res.cookie('authToken', token, { httpOnly: true, diff --git a/src/backend/routes/middlewares/token.ts b/src/backend/routes/middlewares/token.ts index 041d7fe..2afb5c4 100644 --- a/src/backend/routes/middlewares/token.ts +++ b/src/backend/routes/middlewares/token.ts @@ -1,9 +1,7 @@ -import * as dotenv from 'dotenv'; import config from 'config'; import { NextFunction, Request, Response } from 'express'; import jwt from 'jsonwebtoken'; -dotenv.config(); /** * Middleware for checking jwt token