diff --git a/config/config.js b/config/config.js new file mode 100644 index 0000000..4454a7e --- /dev/null +++ b/config/config.js @@ -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; \ No newline at end of file diff --git a/routes/users.js b/routes/users.js index 313911f..d4d3027 100644 --- a/routes/users.js +++ b/routes/users.js @@ -5,6 +5,7 @@ const auth = require("../middleware/auth"); const User = require("../schemas/User"); const config = require("config"); +import { jwtSecret } from '../config/config'; router.post("/register", async (req, res) => { try { @@ -60,7 +61,8 @@ router.post("/login", async (req, res) => { const isMatch = await bcrypt.compare(password, user.password); 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 .json({ token, diff --git a/server.js b/server.js index c36f518..8137bb1 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const express = require("express"); const config = require("config"); const mongoose = require("mongoose"); const cors = require("cors"); +import { mongoURI } from './config/config'; // set up express @@ -16,7 +17,8 @@ app.listen(PORT, () => console.log(`The server has started on port: ${PORT}`)); // set up mongoose mongoose.connect( - config.get('mongoURI'), + //config.get('mongoURI'), + mongoURI, { useNewUrlParser: true, useUnifiedTopology: true,