Skip to content

Commit

Permalink
redirect to ticket url path after login (#140) (#173)
Browse files Browse the repository at this point in the history
Redirects user to ticket url if they clicked on it without being logged in, after logging in

---------

Co-authored-by: William Li <[email protected]>
  • Loading branch information
willi-li-am and willi-li-am authored Dec 21, 2023
1 parent 411819a commit 904563a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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 />
}

0 comments on commit 904563a

Please sign in to comment.