mirror of
https://github.com/hazemKrimi/crimson-quirks-ui.git
synced 2026-05-02 02:30:29 +00:00
Update routing and token retrieval
This commit is contained in:
+54
-10
@@ -1,8 +1,15 @@
|
|||||||
|
import jwtDecode from 'jwt-decode';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { Switch } from 'react-router-dom';
|
import { Switch } from 'react-router-dom';
|
||||||
import { useReactiveVar } from '@apollo/client';
|
import { useLazyQuery, useReactiveVar } from '@apollo/client';
|
||||||
import { ProtectedRoute, AuthRoute, Navbar, Sidebar } from './components';
|
import {
|
||||||
import { roleVar, tokenVar } from './graphql/state';
|
ProtectedRoute,
|
||||||
|
AuthRoute,
|
||||||
|
Navbar,
|
||||||
|
Sidebar,
|
||||||
|
Spinner,
|
||||||
|
} from './components';
|
||||||
|
import { roleVar, tokenVar, userVar } from './graphql/state';
|
||||||
import {
|
import {
|
||||||
AdditionalInfo,
|
AdditionalInfo,
|
||||||
ForgotPassword,
|
ForgotPassword,
|
||||||
@@ -12,23 +19,58 @@ import {
|
|||||||
Project,
|
Project,
|
||||||
Settings,
|
Settings,
|
||||||
} from './pages';
|
} from './pages';
|
||||||
|
import { GetUserByIdQuery, GetUserByIdQueryVariables } from './graphql/types';
|
||||||
|
import { GET_USER_BY_ID } from './graphql/auth.api';
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const token = useReactiveVar(tokenVar);
|
const token = useReactiveVar(tokenVar);
|
||||||
const role = useReactiveVar(roleVar);
|
const role = useReactiveVar(roleVar);
|
||||||
|
const currentUser = useReactiveVar(userVar);
|
||||||
|
|
||||||
|
const [getUserById, { loading }] = useLazyQuery<
|
||||||
|
GetUserByIdQuery,
|
||||||
|
GetUserByIdQueryVariables
|
||||||
|
>(GET_USER_BY_ID, {
|
||||||
|
onCompleted({ getUserById: user }) {
|
||||||
|
userVar(user);
|
||||||
|
switch (user.role) {
|
||||||
|
case 'Client':
|
||||||
|
roleVar('client');
|
||||||
|
break;
|
||||||
|
case 'ProductOwner':
|
||||||
|
roleVar('productOwner');
|
||||||
|
break;
|
||||||
|
case 'Developer':
|
||||||
|
roleVar('developer');
|
||||||
|
break;
|
||||||
|
case 'Admin':
|
||||||
|
roleVar('admin');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const localStorageToken = localStorage.getItem('token');
|
const localStorageToken = localStorage.getItem('token');
|
||||||
|
|
||||||
if (localStorageToken) tokenVar(localStorageToken);
|
if (localStorageToken) {
|
||||||
|
const { id } = jwtDecode<{ id: string; role: string }>(localStorageToken);
|
||||||
|
|
||||||
|
getUserById({ variables: { id } });
|
||||||
|
tokenVar(localStorageToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return !loading ? (
|
||||||
<>
|
<>
|
||||||
{token && (
|
{token && currentUser?.firstName && (
|
||||||
<>
|
<>
|
||||||
<Navbar withSidebar={role !== 'admin'} />
|
<Navbar />
|
||||||
{role !== 'admin' && <Sidebar />}
|
<Sidebar />
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<Switch>
|
<Switch>
|
||||||
@@ -47,9 +89,9 @@ const App = () => {
|
|||||||
<AuthRoute path='/signup' exact>
|
<AuthRoute path='/signup' exact>
|
||||||
<Signup />
|
<Signup />
|
||||||
</AuthRoute>
|
</AuthRoute>
|
||||||
<AuthRoute path='/additional-info' exact>
|
<ProtectedRoute path='/additional-info' exact>
|
||||||
<AdditionalInfo />
|
<AdditionalInfo />
|
||||||
</AuthRoute>
|
</ProtectedRoute>
|
||||||
<AuthRoute path='/forgot-password' exact>
|
<AuthRoute path='/forgot-password' exact>
|
||||||
<ForgotPassword />
|
<ForgotPassword />
|
||||||
</AuthRoute>
|
</AuthRoute>
|
||||||
@@ -58,6 +100,8 @@ const App = () => {
|
|||||||
</AuthRoute>
|
</AuthRoute>
|
||||||
</Switch>
|
</Switch>
|
||||||
</>
|
</>
|
||||||
|
) : (
|
||||||
|
<Spinner fullScreen color={role || 'client'} />
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user