Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dashboard
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
red-giant
dashboard
Commits
be557891
Verified
Commit
be557891
authored
3 years ago
by
Defendi Alberto
Browse files
Options
Downloads
Patches
Plain Diff
Remove superflous api call and use AuthContext to pass isAuthenticated
and role.
parent
dbb38d5d
No related branches found
No related tags found
2 merge requests
!56
Refined auth flow and new website pages.
,
!44
New route type (RestrictedRoute) and better api calls.
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/api/PrivateRoute/PrivateRoute.tsx
+7
-28
7 additions, 28 deletions
src/api/PrivateRoute/PrivateRoute.tsx
with
7 additions
and
28 deletions
src/api/PrivateRoute/PrivateRoute.tsx
+
7
−
28
View file @
be557891
import
React
,
{
useState
,
useEffect
,
useContext
}
from
'
react
'
;
import
axios
from
'
axios
'
;
import
{
Route
,
Redirect
,
RouteProps
}
from
'
react-router-dom
'
;
import
{
NonAuthRoutes
}
from
'
api/routes
'
;
import
{
AuthContext
}
from
'
components/AuthUser/AuthContext
'
;
...
...
@@ -8,7 +7,6 @@ import { AuthContext } from 'components/AuthUser/AuthContext';
* A wrapper for <Route> that redirects to the login screen if you're not yet authenticated.
* Every non-public route must be wrapped with this component.
* */
type
Props
=
{
Component
:
React
.
FC
<
RouteProps
>
;
path
:
string
;
...
...
@@ -21,29 +19,11 @@ export const PrivateRoute = ({
path
,
requiredRoles
,
}:
Props
):
JSX
.
Element
=>
{
const
[
auth
,
setAuth
]
=
useState
<
boolean
>
(
false
);
const
[
serverRole
,
setServerRole
]
=
useState
<
string
>
(
''
);
const
{
role
}
=
useContext
(
AuthContext
);
const
{
role
,
isAuth
}
=
useContext
(
AuthContext
);
useEffect
(()
=>
{
const
fetch
=
async
():
Promise
<
void
>
=>
{
await
axios
(
'
/api/web/login/is_authenticated
'
).
then
((
res
)
=>
setAuth
(
res
.
data
.
is_authenticated
),
);
};
// Check if the role is contained in the roles array (passed as props).
const
userHasRequiredRole
=
requiredRoles
.
includes
(
role
);
/*
Check if user is logged in.
Avoiding this condition would call is\_authenticated every time
this component state is triggered, falling in unnecessary calls to the
server.
*/
fetch
();
axios
(
'
/api/web/login/get_role
'
).
then
((
response
)
=>
setServerRole
(
response
.
data
.
role
),
);
},
[
auth
]);
const
userHasRequiredRole
=
requiredRoles
.
includes
(
serverRole
);
const
message
=
userHasRequiredRole
?
'
Please log in to view this page
'
:
'
Your role is not allowed
'
;
...
...
@@ -53,15 +33,14 @@ export const PrivateRoute = ({
exact
=
{
false
}
path
=
{
path
}
render
=
{
(
props
:
RouteProps
)
=>
a
uth
&&
userHasRequiredRole
?
(
isA
uth
&&
userHasRequiredRole
?
(
<
Component
{
...
props
}
/>
)
:
(
<
Redirect
to
=
{
{
pathname
:
userHasRequiredRole
&&
auth
?
`
${
NonAuthRoutes
.
auth
}${
NonAuthRoutes
.
signIn
}
`
:
NonAuthRoutes
.
unauthorized
,
pathname
:
!
userHasRequiredRole
?
`
${
NonAuthRoutes
.
auth
}${
NonAuthRoutes
.
signIn
}
`
:
NonAuthRoutes
.
unauthorized
,
state
:
{
message
,
requestedPath
:
path
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment