@@ -11,14 +11,11 @@ import {
1111 SetPasswordPage ,
1212 SignUpPage ,
1313} from "pages/auth" ;
14- import {
15- Maybe ,
16- UserRole ,
17- useUserRolesQuery ,
18- } from "api/graphql/generated/graphql" ;
14+ import { Maybe , UserRole } from "api/graphql/generated/graphql" ;
1915import { AppSpinner } from "shared/components/spinners" ;
2016import Layout from "shared/components/layout" ;
2117import ScrollPageSectionPage from "pages/scroll-page-section" ;
18+ import { useAuth } from "features/authorization/context/auth-context" ;
2219
2320import StudentRoutes from "./student" ;
2421import MentorRoutes from "./mentor" ;
@@ -34,13 +31,17 @@ interface IRoutnig {
3431}
3532
3633const ProtectedRoute : FC < IProtectedRoute > = ( { children } ) => {
37- const isAuth = localStorage . getItem ( "isAuth" ) ;
34+ const { isLoading, isAuth } = useAuth ( ) ;
35+
36+ if ( isLoading ) {
37+ return < AppSpinner /> ;
38+ }
3839
39- if ( isAuth ) {
40- return < Navigate to = "/" replace /> ;
40+ if ( ! isAuth ) {
41+ return < Navigate to = "/authorization " replace /> ;
4142 }
4243
43- return < Layout isLogging > { children } </ Layout > ;
44+ return < > { children } </ > ;
4445} ;
4546
4647export const roleRoutes : { [ key in UserRole ] ?: ReactElement [ ] } = {
@@ -63,28 +64,6 @@ export const getUserRoutes = (userRoles: Maybe<Array<Maybe<UserRole>>>) => {
6364 return Array . from ( routesSet ) ;
6465} ;
6566
66- export const useUserRoutes = ( ) => {
67- const location = useLocation ( ) ;
68- const isAuthPage =
69- location . pathname === "/authorization" ||
70- location . pathname === "/signup" ||
71- location . pathname === "/reset" ||
72- location . pathname === "/reset/token" ||
73- location . pathname === "/reset/password" ;
74-
75- const { data, loading } = useUserRolesQuery ( {
76- skip : isAuthPage ,
77- onCompleted : ( data ) => {
78- userRolesVar ( data ?. user ?. roles ) ;
79- } ,
80- } ) ;
81-
82- const roles = data ?. user ?. roles ?? [ ] ;
83- const usersRoutes = getUserRoutes ( roles ) ;
84-
85- return { usersRoutes, loading } ;
86- } ;
87-
8867const Routing : FC < IRoutnig > = ( ) => {
8968 const location = useLocation ( ) ;
9069 const [ errorBoundaryKey , setErrorBoundaryKey ] = useState < string > (
@@ -95,9 +74,11 @@ const Routing: FC<IRoutnig> = () => {
9574 setErrorBoundaryKey ( location . pathname ) ;
9675 } , [ location ] ) ;
9776
98- const { usersRoutes, loading } = useUserRoutes ( ) ;
77+ const userRoles = userRolesVar ( ) ;
78+ const usersRoutes = getUserRoutes ( userRoles ) ;
79+ const { isLoading } = useAuth ( ) ;
9980
100- if ( loading ) {
81+ if ( isLoading ) {
10182 return < AppSpinner /> ;
10283 }
10384
@@ -111,7 +92,14 @@ const Routing: FC<IRoutnig> = () => {
11192 }
11293 >
11394 < Routes >
114- < Route path = "/" element = { < Layout /> } >
95+ < Route
96+ path = "/"
97+ element = {
98+ < ProtectedRoute >
99+ < Layout />
100+ </ ProtectedRoute >
101+ }
102+ >
115103 { usersRoutes ?. map ( ( route ) => (
116104 < Route
117105 key = { route . key }
@@ -120,46 +108,11 @@ const Routing: FC<IRoutnig> = () => {
120108 />
121109 ) ) }
122110 </ Route >
123- < Route
124- path = "/authorization"
125- element = {
126- < ProtectedRoute >
127- < LoginPage />
128- </ ProtectedRoute >
129- }
130- />
131- < Route
132- path = "/signup"
133- element = {
134- < ProtectedRoute >
135- < SignUpPage />
136- </ ProtectedRoute >
137- }
138- />
139- < Route
140- path = "/reset"
141- element = {
142- < ProtectedRoute >
143- < ResetPage />
144- </ ProtectedRoute >
145- }
146- />
147- < Route
148- path = "/reset/token"
149- element = {
150- < ProtectedRoute >
151- < ConfirmTokenPage />
152- </ ProtectedRoute >
153- }
154- />
155- < Route
156- path = "/reset/password"
157- element = {
158- < ProtectedRoute >
159- < SetPasswordPage />
160- </ ProtectedRoute >
161- }
162- />
111+ < Route path = "/authorization" element = { < LoginPage /> } />
112+ < Route path = "/signup" element = { < SignUpPage /> } />
113+ < Route path = "/reset" element = { < ResetPage /> } />
114+ < Route path = "/reset/token" element = { < ConfirmTokenPage /> } />
115+ < Route path = "/reset/password" element = { < SetPasswordPage /> } />
163116 < Route
164117 path = "*"
165118 element = {
0 commit comments