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
95f44dae
Verified
Commit
95f44dae
authored
3 years ago
by
Defendi Alberto
Browse files
Options
Downloads
Patches
Plain Diff
Call is_authenticated after role is fetched.
parent
6e1419c7
No related branches found
No related tags found
2 merge requests
!56
Refined auth flow and new website pages.
,
!43
Move back cookie fetch to SignInForm. Role fetched and saved into a Context. Small refactorings.
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/api/PrivateRoute/PrivateRoute.tsx
+11
-6
11 additions, 6 deletions
src/api/PrivateRoute/PrivateRoute.tsx
with
11 additions
and
6 deletions
src/api/PrivateRoute/PrivateRoute.tsx
+
11
−
6
View file @
95f44dae
...
@@ -6,6 +6,7 @@ import { AuthContext } from 'components/AuthUser/AuthContext';
...
@@ -6,6 +6,7 @@ import { AuthContext } from 'components/AuthUser/AuthContext';
/**
/**
* 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.
* Every non-public route must be wrapped with this component.
* */
* */
type
Props
=
{
type
Props
=
{
...
@@ -31,17 +32,21 @@ export const PrivateRoute = ({
...
@@ -31,17 +32,21 @@ export const PrivateRoute = ({
setLoading
(
true
);
setLoading
(
true
);
return
null
;
return
null
;
};
};
fetch
();
/*
},
[]);
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.
*/
if
(
role
)
fetch
();
},
[
auth
]);
const
userHasRequiredRole
=
requiredRoles
.
includes
(
role
);
const
userHasRequiredRole
=
requiredRoles
.
includes
(
role
);
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
'
;
return
!
loading
?
(
return
(
<
p
>
loading
</
p
>
)
:
(
<
Route
<
Route
exact
=
{
false
}
exact
=
{
false
}
path
=
{
path
}
path
=
{
path
}
...
@@ -52,7 +57,7 @@ export const PrivateRoute = ({
...
@@ -52,7 +57,7 @@ export const PrivateRoute = ({
<
Redirect
<
Redirect
to
=
{
{
to
=
{
{
pathname
:
userHasRequiredRole
pathname
:
userHasRequiredRole
?
NonAuthRoutes
.
signIn
?
`auth/
${
NonAuthRoutes
.
signIn
}
`
:
NonAuthRoutes
.
unauthorized
,
:
NonAuthRoutes
.
unauthorized
,
state
:
{
state
:
{
message
,
message
,
...
...
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