Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
greg
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
it-bott-integrasjoner
greg
Merge requests
!140
Greg 89 login redirect
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Greg 89 login redirect
GREG-89-login-redirect
into
master
Overview
0
Commits
3
Pipelines
2
Changes
1
Merged
elg
requested to merge
GREG-89-login-redirect
into
master
3 years ago
Overview
0
Commits
3
Pipelines
2
Changes
1
Expand
0
0
Merge request reports
Viewing commit
097d6903
Prev
Next
Show latest version
1 file
+
13
−
10
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
097d6903
Redirect back to requested page from protectedRoutes
· 097d6903
Stein Elgethun
authored
3 years ago
Issue: GREG-89
frontend/src/components/protectedRoute/index.tsx
+
13
−
10
Options
import
React
from
'
react
'
import
{
Route
,
R
edirect
,
RouteProps
}
from
'
react-router-dom
'
import
{
Route
,
R
outeProps
,
useLocation
,
useHistory
}
from
'
react-router-dom
'
import
{
setCookie
}
from
'
utils
'
import
{
useUserContext
}
from
'
contexts
'
interface
IProtectedRoute
extends
RouteProps
{
@@ -9,17 +10,19 @@ interface IProtectedRoute extends RouteProps {
function
ProtectedRoute
({
children
,
...
rest
}:
IProtectedRoute
)
{
// Simple protected route component
// TODO: redirect back to the requested site after login
const
{
user
}
=
useUserContext
()
if
(
!
user
.
auth
)
{
const
location
=
useLocation
()
const
history
=
useHistory
()
setCookie
(
'
redirect
'
,
location
.
pathname
)
history
.
push
(
'
/
'
)
}
return
(
<
Route
{
...
rest
}
/* eslint-disable arrow-body-style */
render
=
{
()
=>
{
return
user
.
auth
?
children
:
<
Redirect
to
=
"/"
/>
}
}
/* eslint-enable arrow-body-style */
/>
<
Route
{
...
rest
}
>
{
children
}
</
Route
>
)
}
Loading