Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect to ticket url path after login (#140) #173

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion frontend/src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const Navbar = () => {
setLoading(true)
try {
await login()
navigate('/')
} catch (err) {
setError('Failed to log in')
console.log(err)
Expand Down
23 changes: 20 additions & 3 deletions frontend/src/contexts/CustomRoutes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import React from 'react'
import { Navigate, Outlet } from 'react-router-dom'
import { Navigate, Outlet, useLocation } from 'react-router-dom'
import { useAuth } from './AuthContext'

export const PrivateRoute = () => {
const { currentUser } = useAuth()

const location = useLocation()

if (typeof currentUser === 'undefined') return <h1>Loading.....</h1>
if (!currentUser) return <Navigate replace to="/login" />
if (!currentUser)
return (
<Navigate
replace
to="/login"
state={{
from:
location.pathname != '/login' ? location.pathname : '/',
}}
/>
)
return <Outlet />
}

Expand All @@ -16,6 +28,11 @@ export const PublicRoute = () => {

export const LoggedInRedirect = () => {
const { currentUser } = useAuth()
if (currentUser) return <Navigate replace to="/" />

const location = useLocation()

const prevLocation = location.state ? location.state['from'] : '/'

if (currentUser) return <Navigate replace to={prevLocation} />
return <Outlet />
}
Loading