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
a012d96b
Verified
Commit
a012d96b
authored
3 years ago
by
Defendi Alberto
Browse files
Options
Downloads
Patches
Plain Diff
Implement is_authenticated call and attach to PrivateRoute
parent
dd812c51
No related branches found
No related tags found
1 merge request
!33
Connect api calls and include test/docs cases
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/components/api/PrivateRoute/PrivateRoute.tsx
+21
-4
21 additions, 4 deletions
src/components/api/PrivateRoute/PrivateRoute.tsx
with
21 additions
and
4 deletions
src/components/api/PrivateRoute/PrivateRoute.tsx
+
21
−
4
View file @
a012d96b
import
React
from
'
react
'
;
import
React
,
{
useState
,
useEffect
}
from
'
react
'
;
import
axios
from
'
axios
'
;
import
{
Route
,
Redirect
,
RouteProps
}
from
'
react-router-dom
'
;
import
{
NonAuthRoutes
}
from
'
components/api/routes
'
;
...
...
@@ -18,18 +19,34 @@ export const PrivateRoute = ({
path
,
requiredRoles
,
}:
Props
):
JSX
.
Element
=>
{
const
isAuthed
=
!!
sessionStorage
.
getItem
(
'
X-CSRFTOKEN
'
);
const
[
auth
,
setAuth
]
=
useState
<
boolean
>
(
false
);
const
[
loading
,
setLoading
]
=
useState
<
boolean
>
(
false
);
useEffect
(()
=>
{
const
fetch
=
async
():
Promise
<
any
>
=>
{
const
result
=
await
axios
(
'
/api/web/login/is_authenticated
'
);
// FIX: Remove negation and use true server data
setAuth
(
!
result
.
data
.
is_authenticated
);
setLoading
(
true
);
};
fetch
();
},
[]);
const
currentRole
=
String
(
sessionStorage
.
getItem
(
'
ROLE
'
));
const
userHasRequiredRole
=
requiredRoles
.
includes
(
currentRole
);
const
message
=
userHasRequiredRole
?
'
Please log in to view this page
'
:
'
Your role is not allowed
'
;
return
(
return
!
loading
?
(
<
p
>
loading
</
p
>
)
:
(
<
Route
exact
=
{
false
}
path
=
{
path
}
render
=
{
(
props
:
RouteProps
)
=>
isA
uth
ed
&&
userHasRequiredRole
?
(
a
uth
&&
userHasRequiredRole
?
(
<
Component
{
...
props
}
/>
)
:
(
<
Redirect
...
...
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