├── .gitignore
├── README.md
├── client
├── .gitignore
├── README.md
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ └── manifest.json
├── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── actions
│ │ ├── authActions.js
│ │ └── types.js
│ ├── components
│ │ ├── auth
│ │ │ ├── Login.js
│ │ │ └── Register.js
│ │ ├── dashboard
│ │ │ └── Dashboard.js
│ │ ├── layout
│ │ │ ├── Landing.js
│ │ │ └── Navbar.js
│ │ └── private-route
│ │ │ └── PrivateRoute.js
│ ├── index.css
│ ├── index.js
│ ├── reducers
│ │ ├── authReducer.js
│ │ ├── errorReducer.js
│ │ └── index.js
│ ├── serviceWorker.js
│ ├── store.js
│ └── utils
│ │ └── setAuthToken.js
└── yarn.lock
├── config
├── keys.js
└── passport.js
├── models
└── User.js
├── package.json
├── routes
└── api
│ └── users.js
├── server.js
└── validation
├── login.js
└── register.js
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 | package-lock.json
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # mern-auth
2 |
3 | 
4 | Minimal full-stack MERN app with authentication using passport and JWTs.
5 |
6 | This project uses the following technologies:
7 |
8 | - [React](https://reactjs.org) and [React Router](https://reacttraining.com/react-router/) for frontend
9 | - [Express](http://expressjs.com/) and [Node](https://nodejs.org/en/) for the backend
10 | - [MongoDB](https://www.mongodb.com/) for the database
11 | - [Redux](https://redux.js.org/basics/usagewithreact) for state management between React components
12 |
13 | ## Medium Series
14 |
15 | - [Build a Login/Auth App with the MERN Stack — Part 1 (Backend)](https://blog.bitsrc.io/build-a-login-auth-app-with-mern-stack-part-1-c405048e3669)
16 | - [Build a Login/Auth App with the MERN Stack — Part 2 (Frontend & Redux Setup)](https://blog.bitsrc.io/build-a-login-auth-app-with-mern-stack-part-2-frontend-6eac4e38ee82)
17 | - [Build a Login/Auth App with the MERN Stack — Part 3 (Linking Redux with React Components)](https://blog.bitsrc.io/build-a-login-auth-app-with-the-mern-stack-part-3-react-components-88190f8db718)
18 |
19 | ## Configuration
20 |
21 | Make sure to add your own `MONGOURI` from your [mLab](http://mlab.com) database in `config/keys.js`.
22 |
23 | ```javascript
24 | module.exports = {
25 | mongoURI: "YOUR_MONGO_URI_HERE",
26 | secretOrKey: "secret"
27 | };
28 | ```
29 |
30 | ## Quick Start
31 |
32 | ```javascript
33 | // Install dependencies for server & client
34 | npm install && npm run client-install
35 |
36 | // Run client & server with concurrently
37 | npm run dev
38 |
39 | // Server runs on http://localhost:5000 and client on http://localhost:3000
40 | ```
41 |
42 | For deploying to Heroku, please refer to [this](https://www.youtube.com/watch?v=71wSzpLyW9k) helpful video by TraversyMedia.
43 |
--------------------------------------------------------------------------------
/client/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/client/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `npm run eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35 |
36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37 |
38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
--------------------------------------------------------------------------------
/client/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "client",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "axios": "^0.18.0",
7 | "classnames": "^2.2.6",
8 | "jwt-decode": "^2.2.0",
9 | "react": "^16.6.3",
10 | "react-dom": "^16.6.3",
11 | "react-redux": "^5.1.1",
12 | "react-router-dom": "^4.3.1",
13 | "react-scripts": "2.1.1",
14 | "redux": "^4.0.1",
15 | "redux-thunk": "^2.3.0"
16 | },
17 | "scripts": {
18 | "start": "react-scripts start",
19 | "build": "react-scripts build",
20 | "test": "react-scripts test",
21 | "eject": "react-scripts eject"
22 | },
23 | "proxy": "http://localhost:5000",
24 | "eslintConfig": {
25 | "extends": "react-app"
26 | },
27 | "browserslist": [
28 | ">0.2%",
29 | "not dead",
30 | "not ie <= 11",
31 | "not op_mini all"
32 | ]
33 | }
34 |
--------------------------------------------------------------------------------
/client/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rishipr/mern-auth/4a339402faeac8685609c32362aa4ea09a07e118/client/public/favicon.ico
--------------------------------------------------------------------------------
/client/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | MERN Auth App
13 |
14 |
15 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/client/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | }
10 | ],
11 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/client/src/App.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rishipr/mern-auth/4a339402faeac8685609c32362aa4ea09a07e118/client/src/App.css
--------------------------------------------------------------------------------
/client/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from "react";
2 | import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
3 | import jwt_decode from "jwt-decode";
4 | import setAuthToken from "./utils/setAuthToken";
5 |
6 | import { setCurrentUser, logoutUser } from "./actions/authActions";
7 | import { Provider } from "react-redux";
8 | import store from "./store";
9 |
10 | import Navbar from "./components/layout/Navbar";
11 | import Landing from "./components/layout/Landing";
12 | import Register from "./components/auth/Register";
13 | import Login from "./components/auth/Login";
14 | import PrivateRoute from "./components/private-route/PrivateRoute";
15 | import Dashboard from "./components/dashboard/Dashboard";
16 |
17 | import "./App.css";
18 |
19 | // Check for token to keep user logged in
20 | if (localStorage.jwtToken) {
21 | // Set auth token header auth
22 | const token = localStorage.jwtToken;
23 | setAuthToken(token);
24 | // Decode token and get user info and exp
25 | const decoded = jwt_decode(token);
26 | // Set user and isAuthenticated
27 | store.dispatch(setCurrentUser(decoded));
28 | // Check for expired token
29 | const currentTime = Date.now() / 1000; // to get in milliseconds
30 | if (decoded.exp < currentTime) {
31 | // Logout user
32 | store.dispatch(logoutUser());
33 |
34 | // Redirect to login
35 | window.location.href = "./login";
36 | }
37 | }
38 | class App extends Component {
39 | render() {
40 | return (
41 |
42 |
43 |