migrate heroku env

This commit is contained in:
Priyatham Sai Chand 2021-03-24 23:17:28 +05:30
parent 14562bca3c
commit 069000dc9a
3 changed files with 21 additions and 2 deletions

15
config/config.js Normal file
View File

@ -0,0 +1,15 @@
import dotenv from 'dotenv';
import _ from 'lodash';
const result = dotenv.config();
let envs;
if (!('error' in result)) {
envs = result.parsed;
} else {
envs = {};
_.each(process.env, (value, key) => envs[key] = value);
}
module.exports = envs;

View File

@ -5,6 +5,7 @@ const auth = require("../middleware/auth");
const User = require("../schemas/User"); const User = require("../schemas/User");
const config = require("config"); const config = require("config");
import { jwtSecret } from '../config/config';
router.post("/register", async (req, res) => { router.post("/register", async (req, res) => {
try { try {
@ -60,7 +61,8 @@ router.post("/login", async (req, res) => {
const isMatch = await bcrypt.compare(password, user.password); const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) return res.status(400).json({ msg: "Invalid credentials." }); if (!isMatch) return res.status(400).json({ msg: "Invalid credentials." });
const token = jwt.sign({ id: user._id },config.get('jwtSecret')); //const token = jwt.sign({ id: user._id },config.get('jwtSecret'));
const token = jwt.sign({ id: user._id }, jwtSecret);
if(token) return res if(token) return res
.json({ .json({
token, token,

View File

@ -2,6 +2,7 @@ const express = require("express");
const config = require("config"); const config = require("config");
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const cors = require("cors"); const cors = require("cors");
import { mongoURI } from './config/config';
// set up express // set up express
@ -16,7 +17,8 @@ app.listen(PORT, () => console.log(`The server has started on port: ${PORT}`));
// set up mongoose // set up mongoose
mongoose.connect( mongoose.connect(
config.get('mongoURI'), //config.get('mongoURI'),
mongoURI,
{ {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,