main
Last change
on this file since b248810 was b248810, checked in by 223021 <daniel.ilievski.2@…>, 3 weeks ago |
Added no access page for new recruiters and admin panel for granting access
|
-
Property mode
set to
100644
|
File size:
2.0 KB
|
Rev | Line | |
---|
[d8b6c91] | 1 | import logo from './logo.svg';
|
---|
| 2 | import './App.css';
|
---|
[b248810] | 3 | import {useDispatch, useSelector} from "react-redux";
|
---|
[d8b6c91] | 4 | import {BrowserRouter} from "react-router-dom";
|
---|
| 5 | import {Header} from "./views/static/Header";
|
---|
| 6 | import {RoutesConfig} from "./auth/RoutesConfig";
|
---|
[b248810] | 7 | import {useEffect, useState} from "react";
|
---|
[d8b6c91] | 8 | import {AuthActions} from "./redux/actions/authActions";
|
---|
| 9 | import {AUTH_TOKEN} from "./axios/axiosInstance";
|
---|
[b248810] | 10 | import {jwtDecode} from "jwt-decode";
|
---|
| 11 | import {NoAccess} from "./views/static/NoAccess";
|
---|
[d8b6c91] | 12 |
|
---|
| 13 | function App() {
|
---|
| 14 | const dispatch = useDispatch();
|
---|
| 15 |
|
---|
| 16 | useEffect(() => {
|
---|
| 17 | dispatch(AuthActions.updateToken(localStorage.getItem(AUTH_TOKEN)))
|
---|
| 18 | }, [dispatch])
|
---|
| 19 |
|
---|
[b248810] | 20 | const [user, setUser] = useState(null);
|
---|
| 21 | const [loading, setLoading] = useState(true);
|
---|
| 22 | const auth = useSelector(state => state.auth);
|
---|
| 23 |
|
---|
| 24 | useEffect(() => {
|
---|
| 25 | const token = localStorage.getItem(AUTH_TOKEN);
|
---|
| 26 | if (token !== null) {
|
---|
| 27 | try {
|
---|
| 28 | const decodedToken = jwtDecode(token);
|
---|
| 29 | setUser({
|
---|
| 30 | name: decodedToken.name,
|
---|
| 31 | role: decodedToken.role,
|
---|
| 32 | hasAccess: auth.currentUser.access,
|
---|
| 33 | });
|
---|
| 34 | setLoading(false);
|
---|
| 35 | } catch (error) {
|
---|
| 36 | console.error('Failed to decode token', error);
|
---|
| 37 | setLoading(false);
|
---|
| 38 | }
|
---|
| 39 | } else {
|
---|
| 40 | setLoading(false);
|
---|
| 41 | }
|
---|
| 42 | }, [auth]);
|
---|
| 43 |
|
---|
| 44 | if (loading) {
|
---|
| 45 | return <NoAccess />; // Replace LoadingSpinner with your loading indicator component
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[d8b6c91] | 48 | return (
|
---|
| 49 | <div className="App">
|
---|
| 50 | <BrowserRouter>
|
---|
[b248810] | 51 | {user === null ? (
|
---|
| 52 | <>
|
---|
| 53 | <Header />
|
---|
| 54 | <RoutesConfig />
|
---|
| 55 | </>
|
---|
| 56 | ) : user.hasAccess ? (
|
---|
| 57 | <>
|
---|
| 58 | <Header />
|
---|
| 59 | <RoutesConfig />
|
---|
| 60 | </>
|
---|
| 61 | ) : (
|
---|
| 62 | <NoAccess user={user}/>
|
---|
| 63 | )}
|
---|
| 64 |
|
---|
| 65 |
|
---|
[d8b6c91] | 66 | </BrowserRouter>
|
---|
| 67 | </div>
|
---|
| 68 | );
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | export default App;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.