├── src ├── App.css ├── ClosedPage.js ├── OpenedPage.js ├── App.test.js ├── reportWebVitals.js ├── Account.js ├── index.js ├── index.css ├── App.js ├── FreeComponent.js ├── ProtectedRoutes.js ├── PrintComponent.js ├── AuthComponent.js ├── Register.js └── Login.js ├── public ├── _redirects ├── favicon.ico ├── logo192.png ├── logo512.png ├── robots.txt ├── manifest.json └── index.html ├── debug.log ├── .gitignore ├── package.json └── README.md /src/App.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/ClosedPage.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/OpenedPage.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/_redirects: -------------------------------------------------------------------------------- 1 | /* /index.html 200 -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EBEREGIT/react-auth/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EBEREGIT/react-auth/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EBEREGIT/react-auth/HEAD/public/logo512.png -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /debug.log: -------------------------------------------------------------------------------- 1 | [0117/112558.350:ERROR:directory_reader_win.cc(43)] FindFirstFile: The system cannot find the path specified. (0x3) 2 | [0124/114515.970:ERROR:directory_reader_win.cc(43)] FindFirstFile: The system cannot find the path specified. (0x3) 3 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/Account.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Col, Row } from "react-bootstrap"; 3 | import Login from "./Login"; 4 | import Register from "./Register"; 5 | 6 | export default function Account() { 7 | return ( 8 | 9 | {/* Register */} 10 | 11 | 12 | 13 | 14 | {/* Login */} 15 | 16 | 17 | 18 | 19 | ); 20 | } 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | import reportWebVitals from "./reportWebVitals"; 6 | import "bootstrap/dist/css/bootstrap.min.css"; 7 | import { BrowserRouter } from "react-router-dom"; 8 | 9 | ReactDOM.render( 10 | 11 | 12 | 13 | 14 | , 15 | document.getElementById("root") 16 | ); 17 | 18 | // If you want to start measuring performance in your app, pass a function 19 | // to log results (for example: reportWebVitals(console.log)) 20 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 21 | reportWebVitals(); 22 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | 15 | #navigation { 16 | margin-top: 5%; 17 | margin-bottom: 5%; 18 | } 19 | 20 | #navigation a { 21 | margin-right: 10%; 22 | } 23 | 24 | #navigation a:last-child { 25 | margin-right: 0; 26 | } 27 | 28 | thead th { 29 | padding-right: 50px; 30 | } 31 | 32 | #print_component{ 33 | text-align: center; 34 | } -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import { Switch, Route } from "react-router-dom"; 2 | import { Container, Col, Row } from "react-bootstrap"; 3 | import Account from "./Account"; 4 | import FreeComponent from "./FreeComponent"; 5 | import AuthComponent from "./AuthComponent"; 6 | import ProtectedRoutes from "./ProtectedRoutes"; 7 | 8 | function App() { 9 | return ( 10 | 11 | 12 | 13 |

React Authentication Tutorial

14 | 15 | 20 | 21 |
22 | 23 | {/* create routes here */} 24 | 25 | 26 | 27 | 28 | 29 |
30 | ); 31 | } 32 | 33 | export default App; 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-auth", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.6", 7 | "@testing-library/react": "^11.2.2", 8 | "@testing-library/user-event": "^12.2.2", 9 | "axios": "^0.21.1", 10 | "bootstrap": "^4.5.3", 11 | "react": "^17.0.1", 12 | "react-bootstrap": "^1.4.0", 13 | "react-dom": "^17.0.1", 14 | "react-router-dom": "^5.2.0", 15 | "react-scripts": "4.0.0", 16 | "react-to-print": "^2.13.0", 17 | "universal-cookie": "^4.0.4", 18 | "web-vitals": "^0.2.4" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test", 24 | "eject": "react-scripts eject" 25 | }, 26 | "eslintConfig": { 27 | "extends": [ 28 | "react-app", 29 | "react-app/jest" 30 | ] 31 | }, 32 | "browserslist": { 33 | "production": [ 34 | ">0.2%", 35 | "not dead", 36 | "not op_mini all" 37 | ], 38 | "development": [ 39 | "last 1 chrome version", 40 | "last 1 firefox version", 41 | "last 1 safari version" 42 | ] 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Authentication 2 | 3 | This repository houses a project built during the course of learning how to build an authentication system with react. The following are the tutorials: 4 | 5 | * [Introduction to React-Bootstrap](https://dev.to/ebereplenty/introduction-to-react-bootstrap-20ik) 6 | * [React Authentication - Register](https://dev.to/ebereplenty/react-authentication-part-1-39aj) 7 | * [React Authentication - LOGIN](https://dev.to/ebereplenty/react-authentication-login-h3i) 8 | * [React Authentication - Protecting and Accessing Routes/Endpoints](https://dev.to/ebereplenty/react-authentication-protecting-and-accessing-routes-endpoints-96h) 9 | * [Printing in React Made Easy With React-To-Print](https://dev.to/ebereplenty/printing-in-react-made-easy-with-react-to-print-4b3k) 10 | 11 | ## Dependencies 12 | * [Axios](https://www.npmjs.com/package/axios) 13 | * [React-Bootstrap](https://react-bootstrap.github.io/) 14 | * [universal-cookie](https://www.npmjs.com/package/universal-cookie) 15 | * [react-router-dom](https://www.npmjs.com/package/react-router-dom) 16 | 17 | ## Installation 18 | * Clone the whole project or by branch name 19 | * Run ``npm install`` 20 | * Run ``npm start`` 21 | *That will open the project on your default browser* 22 | -------------------------------------------------------------------------------- /src/FreeComponent.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import axios from "axios"; 3 | import PrintComponent from "./PrintComponent"; 4 | 5 | export default function FreeComponent() { 6 | // set an initial state for the message we will receive after the API call 7 | const [message, setMessage] = useState(""); 8 | 9 | // useEffect automatically executes once the page is fully loaded 10 | useEffect(() => { 11 | // set configurations for the API call here 12 | const configuration = { 13 | method: "get", 14 | url: "https://nodejs-mongodb-auth-app.herokuapp.com/free-endpoint", 15 | }; 16 | 17 | // make the API call 18 | axios(configuration) 19 | .then((result) => { 20 | // assign the message in our result to the message we initialized above 21 | setMessage(result.data.message); 22 | }) 23 | .catch((error) => { 24 | error = new Error(); 25 | }); 26 | }, []); 27 | 28 | return ( 29 |
30 |

Free Component

31 | 32 | {/* displaying our message from our API call */} 33 |

{message}

34 | 35 | 36 |
37 | ); 38 | } 39 | -------------------------------------------------------------------------------- /src/ProtectedRoutes.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Route, Redirect } from "react-router-dom"; 3 | import Cookies from "universal-cookie"; 4 | const cookies = new Cookies(); 5 | 6 | // receives component and any other props represented by ...rest 7 | export default function ProtectedRoutes({ component: Component, ...rest }) { 8 | return ( 9 | 10 | // this route takes other route assigned to it from the App.js and return the same route if condition is met 11 | { 14 | // get cookie from browser if logged in 15 | const token = cookies.get("TOKEN"); 16 | 17 | // return route if there is a valid token set in the cookie 18 | if (token) { 19 | return ; 20 | } else { 21 | // return the user to the landing page if there is no valid token set 22 | return ( 23 | 32 | ); 33 | } 34 | }} 35 | /> 36 | ); 37 | } 38 | -------------------------------------------------------------------------------- /src/PrintComponent.js: -------------------------------------------------------------------------------- 1 | import React, { useRef } from "react"; 2 | import { Button } from "react-bootstrap"; 3 | import ReactToPrint from "react-to-print"; 4 | 5 | export default function PrintComponent() { 6 | let componentRef = useRef(); 7 | 8 | return ( 9 | <> 10 | 22 | 23 | ); 24 | } 25 | 26 | // component to be printed 27 | class ComponentToPrint extends React.Component { 28 | render() { 29 | return ( 30 |
31 |

Attendance

32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
S/NNameEmail
1Njoku Samsonsamson@yahoo.com
2Ebere Plentyebere@gmail.com
3UndefinedNo Email
56 |
57 | ); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/AuthComponent.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import { Button } from "react-bootstrap"; 3 | import axios from "axios"; 4 | import Cookies from "universal-cookie"; 5 | const cookies = new Cookies(); 6 | 7 | // get token generated on login 8 | const token = cookies.get("TOKEN"); 9 | 10 | export default function AuthComponent() { 11 | // set an initial state for the message we will receive after the API call 12 | const [message, setMessage] = useState(""); 13 | 14 | // useEffect automatically executes once the page is fully loaded 15 | useEffect(() => { 16 | // set configurations for the API call here 17 | const configuration = { 18 | method: "get", 19 | url: "https://nodejs-mongodb-auth-app.herokuapp.com/auth-endpoint", 20 | headers: { 21 | Authorization: `Bearer ${token}`, 22 | }, 23 | }; 24 | 25 | // make the API call 26 | axios(configuration) 27 | .then((result) => { 28 | // assign the message in our result to the message we initialized above 29 | setMessage(result.data.message); 30 | }) 31 | .catch((error) => { 32 | error = new Error(); 33 | }); 34 | }, []); 35 | 36 | // logout 37 | const logout = () => { 38 | // destroy the cookie 39 | cookies.remove("TOKEN", { path: "/" }); 40 | // redirect user to the landing page 41 | window.location.href = "/"; 42 | } 43 | 44 | return ( 45 |
46 |

Auth Component

47 | 48 | {/* displaying our message from our API call */} 49 |

{message}

50 | 51 | {/* logout */} 52 | 55 |
56 | ); 57 | } 58 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/Register.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { Form, Button } from "react-bootstrap"; 3 | import axios from "axios"; 4 | 5 | export default function Register() { 6 | // initial state 7 | const [email, setEmail] = useState(""); 8 | const [password, setPassword] = useState(""); 9 | const [register, setRegister] = useState(false); 10 | 11 | const handleSubmit = (e) => { 12 | // prevent the form from refreshing the whole page 13 | e.preventDefault(); 14 | 15 | // set configurations 16 | const configuration = { 17 | method: "post", 18 | url: "https://nodejs-mongodb-auth-app.herokuapp.com/register", 19 | data: { 20 | email, 21 | password, 22 | }, 23 | }; 24 | 25 | // make the API call 26 | axios(configuration) 27 | .then((result) => { 28 | setRegister(true); 29 | }) 30 | .catch((error) => { 31 | error = new Error(); 32 | }); 33 | }; 34 | 35 | return ( 36 | <> 37 |

Register

38 |
handleSubmit(e)}> 39 | {/* email */} 40 | 41 | Email address 42 | setEmail(e.target.value)} 47 | placeholder="Enter email" 48 | /> 49 | 50 | 51 | {/* password */} 52 | 53 | Password 54 | setPassword(e.target.value)} 59 | placeholder="Password" 60 | /> 61 | 62 | 63 | {/* submit button */} 64 | 71 | 72 | {/* display success message */} 73 | {register ? ( 74 |

You Are Registered Successfully

75 | ) : ( 76 |

You Are Not Registered

77 | )} 78 |
79 | 80 | ); 81 | } 82 | -------------------------------------------------------------------------------- /src/Login.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { Form, Button } from "react-bootstrap"; 3 | import axios from "axios"; 4 | import Cookies from "universal-cookie"; 5 | const cookies = new Cookies(); 6 | 7 | export default function Login() { 8 | // initial state 9 | const [email, setEmail] = useState(""); 10 | const [password, setPassword] = useState(""); 11 | const [login, setLogin] = useState(false); 12 | 13 | const handleSubmit = (e) => { 14 | // prevent the form from refreshing the whole page 15 | e.preventDefault(); 16 | 17 | // set configurations 18 | const configuration = { 19 | method: "post", 20 | url: "https://nodejs-mongodb-auth-app.herokuapp.com/login", 21 | data: { 22 | email, 23 | password, 24 | }, 25 | }; 26 | 27 | // make the API call 28 | axios(configuration) 29 | .then((result) => { 30 | // set the cookie 31 | cookies.set("TOKEN", result.data.token, { 32 | path: "/", 33 | }); 34 | // redirect user to the auth page 35 | window.location.href = "/auth"; 36 | 37 | setLogin(true); 38 | }) 39 | .catch((error) => { 40 | error = new Error(); 41 | }); 42 | }; 43 | 44 | return ( 45 | <> 46 |

Login

47 |
handleSubmit(e)}> 48 | {/* email */} 49 | 50 | Email address 51 | setEmail(e.target.value)} 56 | placeholder="Enter email" 57 | /> 58 | 59 | 60 | {/* password */} 61 | 62 | Password 63 | setPassword(e.target.value)} 68 | placeholder="Password" 69 | /> 70 | 71 | 72 | {/* submit button */} 73 | 80 | 81 | {/* display success message */} 82 | {login ? ( 83 |

You Are Logged in Successfully

84 | ) : ( 85 |

You Are Not Logged in

86 | )} 87 |
88 | 89 | ); 90 | } 91 | --------------------------------------------------------------------------------