├── .env ├── .gitignore ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── react-typescript-authentication-example-form-validation.png ├── react-typescript-authentication-example-login.png ├── react-typescript-authentication-example-profile-page.png ├── react-typescript-authentication-example-signup.png ├── src ├── App.css ├── App.tsx ├── common │ └── EventBus.ts ├── components │ ├── BoardAdmin.tsx │ ├── BoardModerator.tsx │ ├── BoardUser.tsx │ ├── Home.tsx │ ├── Login.tsx │ ├── Profile.tsx │ └── Register.tsx ├── index.css ├── index.tsx ├── logo.svg ├── react-app-env.d.ts ├── reportWebVitals.ts ├── services │ ├── auth-header.ts │ ├── auth.service.ts │ └── user.service.ts ├── setupTests.ts └── types │ └── user.type.ts ├── tsconfig.json └── yarn.lock /.env: -------------------------------------------------------------------------------- 1 | PORT=8081 -------------------------------------------------------------------------------- /.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 | # React Typescript Authentication example with Hooks, Axios and Rest API 2 | 3 | Build React Typescript Authentication and Authorization example using React Hooks, React Router, Axios and Bootstrap (without Redux): 4 | - JWT Authentication Flow for User Signup & User Login 5 | - Project Structure for React Typescript Authentication (without Redux) with React Router & Axios 6 | - Creating React Components with Form Validation using Formik and Yup 7 | - React Typescript Components for accessing protected Resources (Authorization) 8 | - Dynamic Navigation Bar in React Typescript App 9 | 10 | For instruction, please visit: 11 | > [React Typescript Authentication example with Hooks, Axios and Rest API](https://www.bezkoder.com/react-typescript-authentication-example/) 12 | 13 | Signup Page: 14 | 15 | ![react-typescript-authentication-example-signup](react-typescript-authentication-example-signup.png) 16 | 17 | Form Validation: 18 | 19 | ![react-typescript-authentication-example-form-validation](react-typescript-authentication-example-form-validation.png) 20 | 21 | Login Page: 22 | 23 | ![react-typescript-authentication-example-login](react-typescript-authentication-example-login.png) 24 | 25 | Profile Page: 26 | 27 | ![react-typescript-authentication-example-profile-page](react-typescript-authentication-example-profile-page.png) 28 | 29 | This React Client works well with following back-end Server: 30 | > [Spring Boot: JWT Authentication & Authorization with MySQL](https://www.bezkoder.com/spring-boot-jwt-authentication/) 31 | 32 | > [Spring Boot: JWT Authentication & Authorization with PostgreSQL](https://www.bezkoder.com/spring-boot-security-postgresql-jwt-authentication/) 33 | 34 | > [Spring Boot: JWT Authentication & Authorization with MongoDB](https://www.bezkoder.com/spring-boot-jwt-auth-mongodb/) 35 | 36 | > [Node Express: JWT Authentication & Authorization with MySQL](https://www.bezkoder.com/node-js-jwt-authentication-mysql/) 37 | 38 | > [Node Express: JWT Authentication & Authorization with PostgreSQL](https://www.bezkoder.com/node-js-jwt-authentication-postgresql/) 39 | 40 | > [Node Express: WT Authentication & Authorization with MongoDB](https://www.bezkoder.com/node-js-mongodb-auth-jwt/) 41 | 42 | Related Posts: 43 | > [React Hooks Typescript example Project with Axios and Web API](https://www.bezkoder.com/react-typescript-api-call/) 44 | 45 | > [React (Javascript) CRUD example to consume Web API](https://www.bezkoder.com/react-crud-web-api/) 46 | 47 | > [React Redux CRUD App example with Rest API](https://www.bezkoder.com/react-redux-crud-example/) 48 | 49 | > [React (Hooks) CRUD example to consume Web API](https://www.bezkoder.com/react-hooks-crud-axios-api/) 50 | 51 | > [React Table example: CRUD App with react-table v7](https://www.bezkoder.com/react-table-example-hooks-crud/) 52 | 53 | Using Material UI instead of Bootstrap: 54 | > [React Material UI examples with a CRUD Application](https://www.bezkoder.com/react-material-ui-examples-crud/) 55 | 56 | More Practice: 57 | > [React Pagination example](https://www.bezkoder.com/react-pagination-material-ui/) 58 | 59 | > [React File Upload example](https://www.bezkoder.com/react-file-upload-axios/) 60 | 61 | > [React JWT Authentication & Authorization example](https://www.bezkoder.com/react-jwt-auth/) 62 | 63 | > [React + Redux: JWT Authentication & Authorization example](https://www.bezkoder.com/react-redux-jwt-auth/) 64 | 65 | Fullstack with Node Express: 66 | > [React + Node Express + MySQL](https://www.bezkoder.com/react-node-express-mysql/) 67 | 68 | > [React + Node Express + PostgreSQL](https://www.bezkoder.com/react-node-express-postgresql/) 69 | 70 | > [React + Node Express + MongoDB](https://www.bezkoder.com/react-node-express-mongodb-mern-stack/) 71 | 72 | Fullstack with Spring Boot: 73 | > [React + Spring Boot + MySQL](https://www.bezkoder.com/react-spring-boot-crud/) 74 | 75 | > [React + Spring Boot + PostgreSQL](https://www.bezkoder.com/spring-boot-react-postgresql/) 76 | 77 | > [React + Spring Boot + MongoDB](https://www.bezkoder.com/react-spring-boot-mongodb/) 78 | 79 | Fullstack with Django: 80 | 81 | > [React + Django Rest Framework](https://www.bezkoder.com/django-react-axios-rest-framework/) 82 | 83 | Serverless: 84 | > [React Firebase CRUD App with Realtime Database](https://www.bezkoder.com/react-firebase-crud/) 85 | 86 | > [React Firestore CRUD App example | Firebase Cloud Firestore](https://www.bezkoder.com/react-firestore-crud/) 87 | 88 | Integration (run back-end & front-end on same server/port) 89 | > [Integrate React with Spring Boot](https://www.bezkoder.com/integrate-reactjs-spring-boot/) 90 | 91 | > [Integrate React with Node Express](https://www.bezkoder.com/integrate-react-express-same-server-port/) 92 | 93 | 94 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 95 | 96 | ### Set port 97 | .env 98 | ``` 99 | PORT=8081 100 | ``` 101 | 102 | ## Project setup 103 | 104 | In the project directory, you can run: 105 | 106 | ``` 107 | npm install 108 | # or 109 | yarn install 110 | ``` 111 | 112 | or 113 | 114 | ### Compiles and hot-reloads for development 115 | 116 | ``` 117 | npm start 118 | # or 119 | yarn start 120 | ``` 121 | 122 | Open [http://localhost:8081](http://localhost:8081) to view it in the browser. 123 | 124 | The page will reload if you make edits. 125 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-typescript-authentication-example", 3 | "version": "1.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.14.1", 7 | "@testing-library/react": "^13.0.0", 8 | "@testing-library/user-event": "^13.2.1", 9 | "@types/jest": "^27.0.1", 10 | "@types/node": "^16.7.13", 11 | "@types/react": "^18.0.0", 12 | "@types/react-dom": "^18.0.0", 13 | "axios": "^0.27.2", 14 | "bootstrap": "^4.6.2", 15 | "formik": "^2.2.9", 16 | "react": "^18.2.0", 17 | "react-dom": "^18.2.0", 18 | "react-router-dom": "^6.4.1", 19 | "react-scripts": "5.0.1", 20 | "typescript": "^4.4.2", 21 | "web-vitals": "^2.1.0", 22 | "yup": "^0.32.11" 23 | }, 24 | "scripts": { 25 | "start": "react-scripts start", 26 | "build": "react-scripts build", 27 | "test": "react-scripts test", 28 | "eject": "react-scripts eject" 29 | }, 30 | "eslintConfig": { 31 | "extends": [ 32 | "react-app", 33 | "react-app/jest" 34 | ] 35 | }, 36 | "browserslist": { 37 | "production": [ 38 | ">0.2%", 39 | "not dead", 40 | "not op_mini all" 41 | ], 42 | "development": [ 43 | "last 1 chrome version", 44 | "last 1 firefox version", 45 | "last 1 safari version" 46 | ] 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-typescript-authentication-example/23807737b9a8b55852757766f88998df6faee347/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/bezkoder/react-typescript-authentication-example/23807737b9a8b55852757766f88998df6faee347/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-typescript-authentication-example/23807737b9a8b55852757766f88998df6faee347/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 | -------------------------------------------------------------------------------- /react-typescript-authentication-example-form-validation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-typescript-authentication-example/23807737b9a8b55852757766f88998df6faee347/react-typescript-authentication-example-form-validation.png -------------------------------------------------------------------------------- /react-typescript-authentication-example-login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-typescript-authentication-example/23807737b9a8b55852757766f88998df6faee347/react-typescript-authentication-example-login.png -------------------------------------------------------------------------------- /react-typescript-authentication-example-profile-page.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-typescript-authentication-example/23807737b9a8b55852757766f88998df6faee347/react-typescript-authentication-example-profile-page.png -------------------------------------------------------------------------------- /react-typescript-authentication-example-signup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-typescript-authentication-example/23807737b9a8b55852757766f88998df6faee347/react-typescript-authentication-example-signup.png -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | label { 2 | display: block; 3 | margin-top: 10px; 4 | } 5 | 6 | .card-container.card { 7 | max-width: 350px !important; 8 | padding: 40px 40px; 9 | } 10 | 11 | .card { 12 | background-color: #f7f7f7; 13 | padding: 20px 25px 30px; 14 | margin: 0 auto 25px; 15 | margin-top: 50px; 16 | -moz-border-radius: 2px; 17 | -webkit-border-radius: 2px; 18 | border-radius: 2px; 19 | -moz-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3); 20 | -webkit-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3); 21 | box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3); 22 | } 23 | 24 | .profile-img-card { 25 | width: 96px; 26 | height: 96px; 27 | margin: 0 auto 10px; 28 | display: block; 29 | -moz-border-radius: 50%; 30 | -webkit-border-radius: 50%; 31 | border-radius: 50%; 32 | } -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { useState, useEffect } from "react"; 3 | import { Routes, Route, Link } from "react-router-dom"; 4 | import "bootstrap/dist/css/bootstrap.min.css"; 5 | import "./App.css"; 6 | 7 | import * as AuthService from "./services/auth.service"; 8 | import IUser from './types/user.type'; 9 | 10 | import Login from "./components/Login"; 11 | import Register from "./components/Register"; 12 | import Home from "./components/Home"; 13 | import Profile from "./components/Profile"; 14 | import BoardUser from "./components/BoardUser"; 15 | import BoardModerator from "./components/BoardModerator"; 16 | import BoardAdmin from "./components/BoardAdmin"; 17 | 18 | import EventBus from "./common/EventBus"; 19 | 20 | const App: React.FC = () => { 21 | const [showModeratorBoard, setShowModeratorBoard] = useState(false); 22 | const [showAdminBoard, setShowAdminBoard] = useState(false); 23 | const [currentUser, setCurrentUser] = useState(undefined); 24 | 25 | useEffect(() => { 26 | const user = AuthService.getCurrentUser(); 27 | 28 | if (user) { 29 | setCurrentUser(user); 30 | setShowModeratorBoard(user.roles.includes("ROLE_MODERATOR")); 31 | setShowAdminBoard(user.roles.includes("ROLE_ADMIN")); 32 | } 33 | 34 | EventBus.on("logout", logOut); 35 | 36 | return () => { 37 | EventBus.remove("logout", logOut); 38 | }; 39 | }, []); 40 | 41 | const logOut = () => { 42 | AuthService.logout(); 43 | setShowModeratorBoard(false); 44 | setShowAdminBoard(false); 45 | setCurrentUser(undefined); 46 | }; 47 | 48 | return ( 49 |
50 | 115 | 116 |
117 | 118 | } /> 119 | } /> 120 | } /> 121 | } /> 122 | } /> 123 | } /> 124 | } /> 125 | } /> 126 | 127 |
128 |
129 | ); 130 | }; 131 | 132 | export default App; 133 | -------------------------------------------------------------------------------- /src/common/EventBus.ts: -------------------------------------------------------------------------------- 1 | const eventBus = { 2 | on(event: string, callback: EventListener) { 3 | document.addEventListener(event, (e) => callback(e)); 4 | }, 5 | dispatch(event: string, data?: any) { 6 | document.dispatchEvent(new CustomEvent(event, { detail: data })); 7 | }, 8 | remove(event: string, callback: EventListener) { 9 | document.removeEventListener(event, callback); 10 | }, 11 | }; 12 | 13 | export default eventBus; 14 | -------------------------------------------------------------------------------- /src/components/BoardAdmin.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | 3 | import { getAdminBoard } from "../services/user.service"; 4 | import EventBus from "../common/EventBus"; 5 | 6 | const BoardAdmin: React.FC = () => { 7 | const [content, setContent] = useState(""); 8 | 9 | useEffect(() => { 10 | getAdminBoard().then( 11 | (response) => { 12 | setContent(response.data); 13 | }, 14 | (error) => { 15 | const _content = 16 | (error.response && 17 | error.response.data && 18 | error.response.data.message) || 19 | error.message || 20 | error.toString(); 21 | 22 | setContent(_content); 23 | 24 | if (error.response && error.response.status === 401) { 25 | EventBus.dispatch("logout"); 26 | } 27 | } 28 | ); 29 | }, []); 30 | 31 | return ( 32 |
33 |
34 |

{content}

35 |
36 |
37 | ); 38 | }; 39 | 40 | export default BoardAdmin; 41 | -------------------------------------------------------------------------------- /src/components/BoardModerator.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | 3 | import { getModeratorBoard } from "../services/user.service"; 4 | import EventBus from "../common/EventBus"; 5 | 6 | const BoardModerator: React.FC = () => { 7 | const [content, setContent] = useState(""); 8 | 9 | useEffect(() => { 10 | getModeratorBoard().then( 11 | (response) => { 12 | setContent(response.data); 13 | }, 14 | (error) => { 15 | const _content = 16 | (error.response && 17 | error.response.data && 18 | error.response.data.message) || 19 | error.message || 20 | error.toString(); 21 | 22 | setContent(_content); 23 | 24 | if (error.response && error.response.status === 401) { 25 | EventBus.dispatch("logout"); 26 | } 27 | } 28 | ); 29 | }, []); 30 | 31 | return ( 32 |
33 |
34 |

{content}

35 |
36 |
37 | ); 38 | }; 39 | 40 | export default BoardModerator; 41 | -------------------------------------------------------------------------------- /src/components/BoardUser.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | 3 | import { getUserBoard } from "../services/user.service"; 4 | import EventBus from "../common/EventBus"; 5 | 6 | const BoardUser: React.FC = () => { 7 | const [content, setContent] = useState(""); 8 | 9 | useEffect(() => { 10 | getUserBoard().then( 11 | (response) => { 12 | setContent(response.data); 13 | }, 14 | (error) => { 15 | const _content = 16 | (error.response && 17 | error.response.data && 18 | error.response.data.message) || 19 | error.message || 20 | error.toString(); 21 | 22 | setContent(_content); 23 | 24 | if (error.response && error.response.status === 401) { 25 | EventBus.dispatch("logout"); 26 | } 27 | } 28 | ); 29 | }, []); 30 | 31 | return ( 32 |
33 |
34 |

{content}

35 |
36 |
37 | ); 38 | }; 39 | 40 | export default BoardUser; 41 | -------------------------------------------------------------------------------- /src/components/Home.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | 3 | import { getPublicContent } from "../services/user.service"; 4 | 5 | const Home: React.FC = () => { 6 | const [content, setContent] = useState(""); 7 | 8 | useEffect(() => { 9 | getPublicContent().then( 10 | (response) => { 11 | setContent(response.data); 12 | }, 13 | (error) => { 14 | const _content = 15 | (error.response && error.response.data) || 16 | error.message || 17 | error.toString(); 18 | 19 | setContent(_content); 20 | } 21 | ); 22 | }, []); 23 | 24 | return ( 25 |
26 |
27 |

{content}

28 |
29 |
30 | ); 31 | }; 32 | 33 | export default Home; 34 | -------------------------------------------------------------------------------- /src/components/Login.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { NavigateFunction, useNavigate } from 'react-router-dom'; 3 | import { Formik, Field, Form, ErrorMessage } from "formik"; 4 | import * as Yup from "yup"; 5 | 6 | import { login } from "../services/auth.service"; 7 | 8 | type Props = {} 9 | 10 | const Login: React.FC = () => { 11 | let navigate: NavigateFunction = useNavigate(); 12 | 13 | const [loading, setLoading] = useState(false); 14 | const [message, setMessage] = useState(""); 15 | 16 | const initialValues: { 17 | username: string; 18 | password: string; 19 | } = { 20 | username: "", 21 | password: "", 22 | }; 23 | 24 | const validationSchema = Yup.object().shape({ 25 | username: Yup.string().required("This field is required!"), 26 | password: Yup.string().required("This field is required!"), 27 | }); 28 | 29 | const handleLogin = (formValue: { username: string; password: string }) => { 30 | const { username, password } = formValue; 31 | 32 | setMessage(""); 33 | setLoading(true); 34 | 35 | login(username, password).then( 36 | () => { 37 | navigate("/profile"); 38 | window.location.reload(); 39 | }, 40 | (error) => { 41 | const resMessage = 42 | (error.response && 43 | error.response.data && 44 | error.response.data.message) || 45 | error.message || 46 | error.toString(); 47 | 48 | setLoading(false); 49 | setMessage(resMessage); 50 | } 51 | ); 52 | }; 53 | 54 | return ( 55 |
56 |
57 | profile-img 62 | 67 |
68 |
69 | 70 | 71 | 76 |
77 | 78 |
79 | 80 | 81 | 86 |
87 | 88 |
89 | 95 |
96 | 97 | {message && ( 98 |
99 |
100 | {message} 101 |
102 |
103 | )} 104 |
105 |
106 |
107 |
108 | ); 109 | }; 110 | 111 | export default Login; 112 | -------------------------------------------------------------------------------- /src/components/Profile.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { getCurrentUser } from "../services/auth.service"; 3 | 4 | const Profile: React.FC = () => { 5 | const currentUser = getCurrentUser(); 6 | 7 | return ( 8 |
9 |
10 |

11 | {currentUser.username} Profile 12 |

13 |
14 |

15 | Token: {currentUser.accessToken.substring(0, 20)} ...{" "} 16 | {currentUser.accessToken.substr(currentUser.accessToken.length - 20)} 17 |

18 |

19 | Id: {currentUser.id} 20 |

21 |

22 | Email: {currentUser.email} 23 |

24 | Authorities: 25 |
    26 | {currentUser.roles && 27 | currentUser.roles.map((role: string, index: number) =>
  • {role}
  • )} 28 |
29 |
30 | ); 31 | }; 32 | 33 | export default Profile; 34 | -------------------------------------------------------------------------------- /src/components/Register.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { Formik, Field, Form, ErrorMessage } from "formik"; 3 | import * as Yup from "yup"; 4 | 5 | import IUser from "../types/user.type"; 6 | import { register } from "../services/auth.service"; 7 | 8 | const Register: React.FC = () => { 9 | const [successful, setSuccessful] = useState(false); 10 | const [message, setMessage] = useState(""); 11 | 12 | const initialValues: IUser = { 13 | username: "", 14 | email: "", 15 | password: "", 16 | }; 17 | 18 | const validationSchema = Yup.object().shape({ 19 | username: Yup.string() 20 | .test( 21 | "len", 22 | "The username must be between 3 and 20 characters.", 23 | (val: any) => 24 | val && 25 | val.toString().length >= 3 && 26 | val.toString().length <= 20 27 | ) 28 | .required("This field is required!"), 29 | email: Yup.string() 30 | .email("This is not a valid email.") 31 | .required("This field is required!"), 32 | password: Yup.string() 33 | .test( 34 | "len", 35 | "The password must be between 6 and 40 characters.", 36 | (val: any) => 37 | val && 38 | val.toString().length >= 6 && 39 | val.toString().length <= 40 40 | ) 41 | .required("This field is required!"), 42 | }); 43 | 44 | const handleRegister = (formValue: IUser) => { 45 | const { username, email, password } = formValue; 46 | 47 | register(username, email, password).then( 48 | (response) => { 49 | setMessage(response.data.message); 50 | setSuccessful(true); 51 | }, 52 | (error) => { 53 | const resMessage = 54 | (error.response && 55 | error.response.data && 56 | error.response.data.message) || 57 | error.message || 58 | error.toString(); 59 | 60 | setMessage(resMessage); 61 | setSuccessful(false); 62 | } 63 | ); 64 | }; 65 | 66 | return ( 67 |
68 |
69 | profile-img 74 | 79 |
80 | {!successful && ( 81 |
82 |
83 | 84 | 85 | 90 |
91 | 92 |
93 | 94 | 95 | 100 |
101 | 102 |
103 | 104 | 109 | 114 |
115 | 116 |
117 | 118 |
119 |
120 | )} 121 | 122 | {message && ( 123 |
124 |
130 | {message} 131 |
132 |
133 | )} 134 |
135 |
136 |
137 |
138 | ); 139 | }; 140 | 141 | export default Register; 142 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import ReactDOM from 'react-dom/client'; 2 | import { BrowserRouter } from "react-router-dom"; 3 | 4 | import './index.css'; 5 | import App from './App'; 6 | import reportWebVitals from './reportWebVitals'; 7 | 8 | const root = ReactDOM.createRoot( 9 | document.getElementById('root') as HTMLElement 10 | ); 11 | 12 | root.render( 13 | 14 | 15 | 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/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/reportWebVitals.ts: -------------------------------------------------------------------------------- 1 | import { ReportHandler } from 'web-vitals'; 2 | 3 | const reportWebVitals = (onPerfEntry?: ReportHandler) => { 4 | if (onPerfEntry && onPerfEntry instanceof Function) { 5 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 6 | getCLS(onPerfEntry); 7 | getFID(onPerfEntry); 8 | getFCP(onPerfEntry); 9 | getLCP(onPerfEntry); 10 | getTTFB(onPerfEntry); 11 | }); 12 | } 13 | }; 14 | 15 | export default reportWebVitals; 16 | -------------------------------------------------------------------------------- /src/services/auth-header.ts: -------------------------------------------------------------------------------- 1 | export default function authHeader() { 2 | const userStr = localStorage.getItem("user"); 3 | let user = null; 4 | if (userStr) 5 | user = JSON.parse(userStr); 6 | 7 | if (user && user.accessToken) { 8 | return { Authorization: 'Bearer ' + user.accessToken }; // for Spring Boot back-end 9 | // return { 'x-access-token': user.accessToken }; // for Node.js Express back-end 10 | } else { 11 | return { Authorization: '' }; // for Spring Boot back-end 12 | // return { 'x-access-token': null }; // for Node Express back-end 13 | } 14 | } -------------------------------------------------------------------------------- /src/services/auth.service.ts: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | 3 | const API_URL = "http://localhost:8080/api/auth/"; 4 | 5 | export const register = (username: string, email: string, password: string) => { 6 | return axios.post(API_URL + "signup", { 7 | username, 8 | email, 9 | password, 10 | }); 11 | }; 12 | 13 | export const login = (username: string, password: string) => { 14 | return axios 15 | .post(API_URL + "signin", { 16 | username, 17 | password, 18 | }) 19 | .then((response) => { 20 | if (response.data.accessToken) { 21 | localStorage.setItem("user", JSON.stringify(response.data)); 22 | } 23 | 24 | return response.data; 25 | }); 26 | }; 27 | 28 | export const logout = () => { 29 | localStorage.removeItem("user"); 30 | }; 31 | 32 | export const getCurrentUser = () => { 33 | const userStr = localStorage.getItem("user"); 34 | if (userStr) return JSON.parse(userStr); 35 | 36 | return null; 37 | }; 38 | -------------------------------------------------------------------------------- /src/services/user.service.ts: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | import authHeader from "./auth-header"; 3 | 4 | const API_URL = "http://localhost:8080/api/test/"; 5 | 6 | export const getPublicContent = () => { 7 | return axios.get(API_URL + "all"); 8 | }; 9 | 10 | export const getUserBoard = () => { 11 | return axios.get(API_URL + "user", { headers: authHeader() }); 12 | }; 13 | 14 | export const getModeratorBoard = () => { 15 | return axios.get(API_URL + "mod", { headers: authHeader() }); 16 | }; 17 | 18 | export const getAdminBoard = () => { 19 | return axios.get(API_URL + "admin", { headers: authHeader() }); 20 | }; 21 | -------------------------------------------------------------------------------- /src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /src/types/user.type.ts: -------------------------------------------------------------------------------- 1 | export default interface IUser { 2 | id?: any | null, 3 | username: string, 4 | email: string, 5 | password: string, 6 | roles?: Array 7 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "noFallthroughCasesInSwitch": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "jsx": "react-jsx" 22 | }, 23 | "include": [ 24 | "src" 25 | ] 26 | } 27 | --------------------------------------------------------------------------------