Skip to content

Commit

Permalink
[hotfix] auth router (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
sijav authored Jul 12, 2024
1 parent e38a7ce commit d29b518
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
9 changes: 5 additions & 4 deletions src/core/auth/AuthGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ export function AuthGuard({ children }: PropsWithChildren) {
? window.location.search
: `?returnUrl=${window.encodeURIComponent(window.location.pathname + window.location.search + (noWorkspace ? '' : window.location.hash))}`,
})
postHog.reset()
clearAllCookies()
handleInternalSetAuth(defaultAuth)
try {
await logoutMutation()
} finally {
postHog.reset()
clearAllCookies()
handleInternalSetAuth(defaultAuth)
} catch {
/* empty */
}
},
[handleInternalSetAuth, navigate, postHog],
Expand Down
41 changes: 22 additions & 19 deletions src/core/auth/RequireAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Outlet, useLocation } from 'react-router-dom'
import { useAbsoluteNavigate } from 'src/shared/absolute-navigate'
import { panelUI } from 'src/shared/constants'
import { useUserProfile } from './useUserProfile'

interface RequireAuthProps {
Expand All @@ -9,27 +10,29 @@ interface RequireAuthProps {
export function RequireAuth({ reverse }: RequireAuthProps) {
const navigate = useAbsoluteNavigate()
const location = useLocation()
const user = useUserProfile()
const { isAuthenticated } = useUserProfile()

const isAuthenticated = !user.isAuthenticated && location.pathname !== '/auth/login'

if (isAuthenticated && !reverse) {
window.setTimeout(() => {
navigate(
{
pathname: '/auth/login',
search: location.search.includes('returnUrl')
? location.search
: `returnUrl=${window.encodeURIComponent(`${location.pathname}${location.search}${location.hash}`)}`,
},
{ replace: true },
)
})
if (!isAuthenticated && !reverse) {
if (!location.pathname.includes('/auth')) {
window.setTimeout(() => {
navigate(
{
pathname: '/auth/login',
search: location.search.includes('returnUrl')
? location.search
: `returnUrl=${window.encodeURIComponent(`${location.pathname}${location.search}${location.hash}`)}`,
},
{ replace: true },
)
})
}
return null
} else if (reverse && !isAuthenticated) {
window.setTimeout(() => {
navigate(window.decodeURIComponent(location.search?.split('returnUrl=')[1]?.split('&')[0] ?? '/'), { replace: true })
})
} else if (reverse && isAuthenticated) {
if (location.pathname.includes('/auth')) {
window.setTimeout(() => {
navigate(window.decodeURIComponent(location.search?.split('returnUrl=')[1]?.split('&')[0] ?? panelUI.homePage), { replace: true })
})
}
return null
}

Expand Down

0 comments on commit d29b518

Please sign in to comment.