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
728d8586
Verified
Commit
728d8586
authored
3 years ago
by
Defendi Alberto
Browse files
Options
Downloads
Patches
Plain Diff
Fetch role from server and clean axios requests.
parent
4b0ff168
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
+15
-10
15 additions, 10 deletions
src/api/PrivateRoute/PrivateRoute.tsx
with
15 additions
and
10 deletions
src/api/PrivateRoute/PrivateRoute.tsx
+
15
−
10
View file @
728d8586
...
@@ -3,7 +3,6 @@ import axios from 'axios';
...
@@ -3,7 +3,6 @@ import axios from 'axios';
import
{
Route
,
Redirect
,
RouteProps
}
from
'
react-router-dom
'
;
import
{
Route
,
Redirect
,
RouteProps
}
from
'
react-router-dom
'
;
import
{
NonAuthRoutes
}
from
'
api/routes
'
;
import
{
NonAuthRoutes
}
from
'
api/routes
'
;
import
{
AuthContext
}
from
'
components/AuthUser/AuthContext
'
;
import
{
AuthContext
}
from
'
components/AuthUser/AuthContext
'
;
import
{
Roles
}
from
'
api/userRoles
'
;
/**
/**
* A wrapper for <Route> that redirects to the login screen if you're not yet authenticated.
* A wrapper for <Route> that redirects to the login screen if you're not yet authenticated.
...
@@ -23,23 +22,28 @@ export const PrivateRoute = ({
...
@@ -23,23 +22,28 @@ export const PrivateRoute = ({
requiredRoles
,
requiredRoles
,
}:
Props
):
JSX
.
Element
=>
{
}:
Props
):
JSX
.
Element
=>
{
const
[
auth
,
setAuth
]
=
useState
<
boolean
>
(
false
);
const
[
auth
,
setAuth
]
=
useState
<
boolean
>
(
false
);
const
[
serverRole
,
setServerRole
]
=
useState
<
string
>
(
''
);
const
{
role
}
=
useContext
(
AuthContext
);
const
{
role
}
=
useContext
(
AuthContext
);
useEffect
(()
=>
{
useEffect
(()
=>
{
const
fetch
=
async
():
Promise
<
unknown
>
=>
{
const
fetch
=
async
():
Promise
<
void
>
=>
{
const
result
=
await
axios
(
'
/api/web/login/is_authenticated
'
)
;
await
axios
(
'
/api/web/login/is_authenticated
'
)
.
then
((
res
)
=>
setAuth
(
res
ult
.
data
.
is_authenticated
)
;
setAuth
(
res
.
data
.
is_authenticated
)
,
return
null
;
)
;
};
};
/*
/*
Check if user is logged in.
Check if user is logged in.
Avoiding this condition would call is\_authenticated every time
Avoiding this condition would call is\_authenticated every time
this component state is triggered, falling in unnecessary calls to the
this component state is triggered, falling in unnecessary calls to the
server.
server.
*/
*/
if
(
role
!==
Roles
.
visitor
)
fetch
();
fetch
();
axios
(
'
/api/web/login/get_role
'
).
then
((
response
)
=>
setServerRole
(
response
.
data
.
role
),
);
},
[
auth
]);
},
[
auth
]);
const
userHasRequiredRole
=
requiredRoles
.
includes
(
r
ole
);
const
userHasRequiredRole
=
requiredRoles
.
includes
(
serverR
ole
);
const
message
=
userHasRequiredRole
const
message
=
userHasRequiredRole
?
'
Please log in to view this page
'
?
'
Please log in to view this page
'
:
'
Your role is not allowed
'
;
:
'
Your role is not allowed
'
;
...
@@ -54,9 +58,10 @@ export const PrivateRoute = ({
...
@@ -54,9 +58,10 @@ export const PrivateRoute = ({
)
:
(
)
:
(
<
Redirect
<
Redirect
to
=
{
{
to
=
{
{
pathname
:
userHasRequiredRole
pathname
:
?
`
${
NonAuthRoutes
.
auth
}${
NonAuthRoutes
.
signIn
}
`
userHasRequiredRole
&&
auth
:
NonAuthRoutes
.
unauthorized
,
?
`
${
NonAuthRoutes
.
auth
}${
NonAuthRoutes
.
signIn
}
`
:
NonAuthRoutes
.
unauthorized
,
state
:
{
state
:
{
message
,
message
,
requestedPath
:
path
,
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