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
ae517281
Verified
Commit
ae517281
authored
3 years ago
by
Defendi Alberto
Browse files
Options
Downloads
Patches
Plain Diff
Move cookie fetch to App
parent
37c55739
No related branches found
No related tags found
2 merge requests
!56
Refined auth flow and new website pages.
,
!41
Sign up form setup
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/App.tsx
+46
-23
46 additions, 23 deletions
src/App.tsx
with
46 additions
and
23 deletions
src/App.tsx
+
46
−
23
View file @
ae517281
import
React
,
{
FC
}
from
'
react
'
;
import
React
,
{
FC
,
useEffect
,
useState
}
from
'
react
'
;
import
{
BrowserRouter
as
Router
,
Switch
,
Route
}
from
'
react-router-dom
'
;
import
{
BrowserRouter
as
Router
,
Switch
,
Route
}
from
'
react-router-dom
'
;
import
{
HomePage
}
from
'
components/HomePage/HomePage
'
;
import
{
HomePage
}
from
'
components/HomePage/HomePage
'
;
import
{
AuthUser
}
from
'
components/AuthUser/AuthUser
'
;
import
{
AuthUser
}
from
'
components/AuthUser/AuthUser
'
;
...
@@ -9,26 +9,49 @@ import { NotFound } from 'components/NotFound/NotFound';
...
@@ -9,26 +9,49 @@ import { NotFound } from 'components/NotFound/NotFound';
import
{
ProfilePage
}
from
'
components/ProfilePage/ProfilePage
'
;
import
{
ProfilePage
}
from
'
components/ProfilePage/ProfilePage
'
;
import
{
Roles
}
from
'
components/api/userRoles
'
;
import
{
Roles
}
from
'
components/api/userRoles
'
;
import
{
Unauthorized
}
from
'
components/Unauthorized/Unauthorized
'
;
import
{
Unauthorized
}
from
'
components/Unauthorized/Unauthorized
'
;
import
axios
from
'
axios
'
;
export
const
App
:
FC
=
()
=>
(
const
configDjangoCookieName
=
():
void
=>
{
<
Router
>
axios
.
defaults
.
xsrfHeaderName
=
'
X-CSRFTOKEN
'
;
<
div
data-testid
=
"App"
>
axios
.
defaults
.
xsrfCookieName
=
'
csrftoken
'
;
<
Switch
>
axios
.
defaults
.
withCredentials
=
true
;
<
Route
path
=
{
NonAuthRoutes
.
auth
}
component
=
{
AuthUser
}
/>
};
<
Route
exact
path
=
{
NonAuthRoutes
.
home
}
component
=
{
LandingPage
}
/>
<
PrivateRoute
export
const
App
:
FC
=
()
=>
{
path
=
{
AuthRoutes
.
dashboard
}
const
[
isCookieFetched
,
setisCookieFetched
]
=
useState
<
string
>
(
''
);
Component
=
{
HomePage
}
configDjangoCookieName
();
requiredRoles
=
{
[
Roles
.
admin
,
Roles
.
operator
,
Roles
.
senior
]
}
useEffect
(()
=>
{
/>
const
fetchCookie
=
async
():
Promise
<
unknown
>
=>
{
<
PrivateRoute
const
response
=
await
axios
(
'
api/web/csrf
'
);
path
=
{
AuthRoutes
.
profile
}
axios
.
defaults
.
headers
.
common
[
'
X-CSRFTOKEN
'
]
=
response
.
data
.
token
;
Component
=
{
ProfilePage
}
sessionStorage
.
setItem
(
'
X-CSRFTOKEN
'
,
response
.
data
.
token
);
requiredRoles
=
{
[
Roles
.
admin
,
Roles
.
operator
,
Roles
.
senior
]
}
sessionStorage
.
setItem
(
'
ROLE
'
,
'
admin
'
);
/>
setisCookieFetched
(
response
.
data
.
token
);
<
Route
path
=
{
NonAuthRoutes
.
unauthorized
}
component
=
{
Unauthorized
}
/>
return
null
;
<
Route
component
=
{
NotFound
}
/>
};
</
Switch
>
if
(
!
isCookieFetched
)
fetchCookie
();
</
div
>
},
[]);
</
Router
>
);
return
(
<
Router
>
<
div
data-testid
=
"App"
>
<
Switch
>
<
Route
path
=
{
NonAuthRoutes
.
auth
}
component
=
{
AuthUser
}
/>
<
Route
exact
path
=
{
NonAuthRoutes
.
home
}
component
=
{
LandingPage
}
/>
<
PrivateRoute
path
=
{
AuthRoutes
.
dashboard
}
Component
=
{
HomePage
}
requiredRoles
=
{
[
Roles
.
admin
,
Roles
.
operator
,
Roles
.
senior
]
}
/>
<
PrivateRoute
path
=
{
AuthRoutes
.
profile
}
Component
=
{
ProfilePage
}
requiredRoles
=
{
[
Roles
.
admin
,
Roles
.
operator
,
Roles
.
senior
]
}
/>
<
Route
path
=
{
NonAuthRoutes
.
unauthorized
}
component
=
{
Unauthorized
}
/>
<
Route
component
=
{
NotFound
}
/>
</
Switch
>
</
div
>
</
Router
>
);
};
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