├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.js ├── components ├── Dashboard.js ├── Login.js ├── Navbar.js └── Register.js └── index.js /.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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | 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. 37 | 38 | 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. 39 | 40 | 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. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "frontend", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.14.1", 7 | "@testing-library/react": "^11.2.7", 8 | "@testing-library/user-event": "^12.8.3", 9 | "axios": "^0.24.0", 10 | "bulma": "^0.9.3", 11 | "jwt-decode": "^3.1.2", 12 | "react": "^17.0.2", 13 | "react-dom": "^17.0.2", 14 | "react-router-dom": "^5.3.0", 15 | "react-scripts": "4.0.3", 16 | "web-vitals": "^1.1.2" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": [ 26 | "react-app", 27 | "react-app/jest" 28 | ] 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mfikricom/FrontEnd-JWT-React-JS/9fc7a879d4896274521c4726684970ecf7d75b42/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mfikricom/FrontEnd-JWT-React-JS/9fc7a879d4896274521c4726684970ecf7d75b42/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mfikricom/FrontEnd-JWT-React-JS/9fc7a879d4896274521c4726684970ecf7d75b42/public/logo512.png -------------------------------------------------------------------------------- /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 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import { BrowserRouter, Route, Switch } from "react-router-dom"; 2 | import Dashboard from "./components/Dashboard"; 3 | import Login from "./components/Login"; 4 | import Navbar from "./components/Navbar"; 5 | import Register from "./components/Register"; 6 | 7 | function App() { 8 | return ( 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | ); 24 | } 25 | 26 | export default App; 27 | -------------------------------------------------------------------------------- /src/components/Dashboard.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable react-hooks/exhaustive-deps */ 2 | import React, { useState, useEffect } from 'react' 3 | import axios from 'axios'; 4 | import jwt_decode from "jwt-decode"; 5 | import { useHistory } from 'react-router-dom'; 6 | 7 | const Dashboard = () => { 8 | const [name, setName] = useState(''); 9 | const [token, setToken] = useState(''); 10 | const [expire, setExpire] = useState(''); 11 | const [users, setUsers] = useState([]); 12 | const history = useHistory(); 13 | 14 | useEffect(() => { 15 | refreshToken(); 16 | getUsers(); 17 | }, []); 18 | 19 | const refreshToken = async () => { 20 | try { 21 | const response = await axios.get('http://localhost:5000/token'); 22 | setToken(response.data.accessToken); 23 | const decoded = jwt_decode(response.data.accessToken); 24 | setName(decoded.name); 25 | setExpire(decoded.exp); 26 | } catch (error) { 27 | if (error.response) { 28 | history.push("/"); 29 | } 30 | } 31 | } 32 | 33 | const axiosJWT = axios.create(); 34 | 35 | axiosJWT.interceptors.request.use(async (config) => { 36 | const currentDate = new Date(); 37 | if (expire * 1000 < currentDate.getTime()) { 38 | const response = await axios.get('http://localhost:5000/token'); 39 | config.headers.Authorization = `Bearer ${response.data.accessToken}`; 40 | setToken(response.data.accessToken); 41 | const decoded = jwt_decode(response.data.accessToken); 42 | setName(decoded.name); 43 | setExpire(decoded.exp); 44 | } 45 | return config; 46 | }, (error) => { 47 | return Promise.reject(error); 48 | }); 49 | 50 | const getUsers = async () => { 51 | const response = await axiosJWT.get('http://localhost:5000/users', { 52 | headers: { 53 | Authorization: `Bearer ${token}` 54 | } 55 | }); 56 | setUsers(response.data); 57 | } 58 | 59 | return ( 60 |
61 |

Welcome Back: {name}

62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | {users.map((user, index) => ( 73 | 74 | 75 | 76 | 77 | 78 | ))} 79 | 80 | 81 |
NoNameEmail
{index + 1}{user.name}{user.email}
82 |
83 | ) 84 | } 85 | 86 | export default Dashboard 87 | -------------------------------------------------------------------------------- /src/components/Login.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react' 2 | import axios from 'axios'; 3 | import { useHistory } from 'react-router-dom'; 4 | 5 | const Login = () => { 6 | const [email, setEmail] = useState(''); 7 | const [password, setPassword] = useState(''); 8 | const [msg, setMsg] = useState(''); 9 | const history = useHistory(); 10 | 11 | const Auth = async (e) => { 12 | e.preventDefault(); 13 | try { 14 | await axios.post('http://localhost:5000/login', { 15 | email: email, 16 | password: password 17 | }); 18 | history.push("/dashboard"); 19 | } catch (error) { 20 | if (error.response) { 21 | setMsg(error.response.data.msg); 22 | } 23 | } 24 | } 25 | 26 | return ( 27 |
28 |
29 |
30 |
31 |
32 |
33 |

{msg}

34 |
35 | 36 |
37 | setEmail(e.target.value)} /> 38 |
39 |
40 |
41 | 42 |
43 | setPassword(e.target.value)} /> 44 |
45 |
46 |
47 | 48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | ) 56 | } 57 | 58 | export default Login 59 | -------------------------------------------------------------------------------- /src/components/Navbar.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import axios from 'axios'; 3 | import { useHistory } from 'react-router-dom'; 4 | 5 | const Navbar = () => { 6 | const history = useHistory(); 7 | 8 | const Logout = async () => { 9 | try { 10 | await axios.delete('http://localhost:5000/logout'); 11 | history.push("/"); 12 | } catch (error) { 13 | console.log(error); 14 | } 15 | } 16 | 17 | return ( 18 | 51 | ) 52 | } 53 | 54 | export default Navbar 55 | -------------------------------------------------------------------------------- /src/components/Register.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react' 2 | import axios from "axios"; 3 | import { useHistory } from "react-router-dom"; 4 | 5 | const Register = () => { 6 | const [name, setName] = useState(''); 7 | const [email, setEmail] = useState(''); 8 | const [password, setPassword] = useState(''); 9 | const [confPassword, setConfPassword] = useState(''); 10 | const [msg, setMsg] = useState(''); 11 | const history = useHistory(); 12 | 13 | const Register = async (e) => { 14 | e.preventDefault(); 15 | try { 16 | await axios.post('http://localhost:5000/users', { 17 | name: name, 18 | email: email, 19 | password: password, 20 | confPassword: confPassword 21 | }); 22 | history.push("/"); 23 | } catch (error) { 24 | if (error.response) { 25 | setMsg(error.response.data.msg); 26 | } 27 | } 28 | } 29 | 30 | return ( 31 |
32 |
33 |
34 |
35 |
36 |
37 |

{msg}

38 |
39 | 40 |
41 | setName(e.target.value)} /> 43 |
44 |
45 |
46 | 47 |
48 | setEmail(e.target.value)} /> 49 |
50 |
51 |
52 | 53 |
54 | setPassword(e.target.value)} /> 55 |
56 |
57 |
58 | 59 |
60 | setConfPassword(e.target.value)} /> 61 |
62 |
63 |
64 | 65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | ) 73 | } 74 | 75 | export default Register 76 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import "bulma/css/bulma.css"; 5 | import axios from "axios"; 6 | axios.defaults.withCredentials = true; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | , 12 | document.getElementById('root') 13 | ); 14 | 15 | --------------------------------------------------------------------------------