Skip to content

Commit

Permalink
[fix] do not show login screen when already authenticated (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
sijav authored Jul 11, 2024
1 parent 0a7dfc4 commit 75ecf99
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export const AppRouter = memo(
() => (
<Suspense fallback={<FullPageLoadingSuspenseFallback forceFullPage />}>
<Routes>
<Route path="/auth/*" element={<AuthContainer />} />
<Route element={<RequireAuth reverse />}>
<Route path="/auth/*" element={<AuthContainer />} />
</Route>
<Route element={<RequireAuth />}>
<Route path="/subscription/*" element={<SubscriptionContainer />} />
<Route path="/*" element={<PanelContainer />} />
Expand Down
15 changes: 13 additions & 2 deletions src/core/auth/RequireAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { Outlet, useLocation } from 'react-router-dom'
import { useAbsoluteNavigate } from 'src/shared/absolute-navigate'
import { useUserProfile } from './useUserProfile'

export function RequireAuth() {
interface RequireAuthProps {
reverse?: boolean
}

export function RequireAuth({ reverse }: RequireAuthProps) {
const navigate = useAbsoluteNavigate()
const location = useLocation()
const user = useUserProfile()

if (!user.isAuthenticated && location.pathname !== '/auth/login') {
const isAuthenticated = !user.isAuthenticated && location.pathname !== '/auth/login'

if (isAuthenticated && !reverse) {
window.setTimeout(() => {
navigate(
{
Expand All @@ -20,6 +26,11 @@ export function RequireAuth() {
)
})
return null
} else if (reverse && !isAuthenticated) {
window.setTimeout(() => {
navigate(window.decodeURIComponent(location.search?.split('returnUrl=')[1]?.split('&')[0] ?? '/'), { replace: true })
})
return null
}

return <Outlet />
Expand Down

0 comments on commit 75ecf99

Please sign in to comment.