From 187edf36ee6d76b0c330199cebaad920d77b8867 Mon Sep 17 00:00:00 2001 From: Roghashin Timbiti Date: Mon, 13 May 2019 19:42:56 +0300 Subject: [PATCH] ch(.env): add .env file to codebase: - add .env file [Maintains #165972826] --- .env-sample | 1 + .eslintrc.js | 1 + .gitignore | 1 + dist/config.js | 5 ----- package-lock.json | 6 ++++++ package.json | 1 + src/actions/signupSigninActions.js | 5 ++--- src/helpers/axiosInstance.js | 15 ++++++++------- webpack.config.js | 11 ++++++++++- 9 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 .env-sample delete mode 100644 dist/config.js diff --git a/.env-sample b/.env-sample new file mode 100644 index 00000000..f81341ea --- /dev/null +++ b/.env-sample @@ -0,0 +1 @@ +baseURL= diff --git a/.eslintrc.js b/.eslintrc.js index f78bf91c..882721e3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -14,6 +14,7 @@ module.exports = { }, "plugins": ["jest", "react"], "rules": { + "no-param-reassign":"off", "max-len": [1, 80, 2], "react/jsx-uses-react":1, "react/react-in-jsx-scope": 1, diff --git a/.gitignore b/.gitignore index 85c4576f..86e10a2f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ coverage/ src/.DS_Store +.env diff --git a/dist/config.js b/dist/config.js deleted file mode 100644 index c7b4f2a7..00000000 --- a/dist/config.js +++ /dev/null @@ -1,5 +0,0 @@ -const baseURL = 'https://ah-backend.herokuapp.com/api/users'; - -export default { - baseURL, -}; diff --git a/package-lock.json b/package-lock.json index 68493e03..fe0bfec7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3155,6 +3155,12 @@ "domelementtype": "1" } }, + "dotenv": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz", + "integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==", + "dev": true + }, "duplexify": { "version": "3.7.1", "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz", diff --git a/package.json b/package.json index 563c9d0e..8447ca3f 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "babel-eslint": "^10.0.1", "babel-loader": "^8.0.5", "coveralls": "^3.0.3", + "dotenv": "^8.0.0", "enzyme-to-json": "^3.3.5", "eslint": "^5.16.0", "eslint-config-airbnb": "^17.1.0", diff --git a/src/actions/signupSigninActions.js b/src/actions/signupSigninActions.js index 617c44c2..05aade98 100644 --- a/src/actions/signupSigninActions.js +++ b/src/actions/signupSigninActions.js @@ -1,10 +1,9 @@ import { notify } from 'react-notify-toast'; import axios from 'axios'; -import config from '../../dist/config'; export const signUpUser = (user, history) => () => { axios - .post(`${config.baseURL}/register/`, user) + .post(`${process.env.baseURL}/users/register/`, user) .then(() => { notify.show('Registration successful', 'success', 4000); history.push('/login'); @@ -18,7 +17,7 @@ export const signUpUser = (user, history) => () => { export const signInUser = (user, history) => () => { axios - .post(`${config.baseURL}/login/`, user) + .post(`${process.env.baseURL}/users/login/`, user) .then((response) => { localStorage.setItem('accessToken', response.data.user.token); notify.show('Login successful', 'success', 4000); diff --git a/src/helpers/axiosInstance.js b/src/helpers/axiosInstance.js index 18bcda92..0cb57960 100644 --- a/src/helpers/axiosInstance.js +++ b/src/helpers/axiosInstance.js @@ -1,15 +1,16 @@ import axios from 'axios'; const axiosInstance = axios.create({ - baseURL: 'https://ah-backend.herokuapp.com', - headers: { Authorization: `Bearer ${localStorage.getItem('accessToken')}` } + baseURL: process.env.baseURL, + headers: { Authorization: `Bearer ${localStorage.getItem('accessToken')}` }, }); axiosInstance.interceptors.request.use((config) => { - if (localStorage.getItem('accessToken') && config.headers.Authorization === 'Bearer null') { - config.headers.Authorization = `Bearer ${localStorage.getItem('accessToken')}`; - } - return config; - }); + if (localStorage.getItem('accessToken') + && config.headers.Authorization === 'Bearer null') { + config.headers.Authorization = `Bearer ${localStorage.getItem('accessToken')}`; + } + return config; +}); export default axiosInstance; diff --git a/webpack.config.js b/webpack.config.js index b3fbdb5a..6fadff3d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,6 +1,15 @@ const path = require('path'); +const webpack = require('webpack'); +const dotenv = require('dotenv'); const HtmlWebPackPlugin = require('html-webpack-plugin'); +const env = dotenv.config().parsed; + +const envKeys = Object.keys(env).reduce((prev, next) => { + const previous = prev; + previous[`process.env.${next}`] = JSON.stringify(env[next]); + return previous; +}, {}); module.exports = { entry: './src/index.jsx', @@ -39,13 +48,13 @@ module.exports = { }, ], }, - ], }, plugins: [ new HtmlWebPackPlugin({ template: './src/index.html', }), + new webpack.DefinePlugin(envKeys), ], devServer: { historyApiFallback: true,