Skip to content
Open
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
13 changes: 12 additions & 1 deletion src/context/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ export const UserContext = ({ initialUser, children }: UserContextProps) => {
}, [router.pathname, revalidate]);

useEffect(() => {
// Only treat the session as invalid when the server actually rejected
// it. A check that never completed (e.g. the fetch was aborted because
// the user navigated away, or the server was briefly unreachable)
// rejects without a response; redirecting on those kicks logged-in
// users to /login and can even cancel an in-flight navigation to
// another site, dragging the browser back to the app. A transiently
// empty user (first fetch still in flight) must not navigate either —
// unauthenticated page loads are already redirected server-side.
const sessionInvalid =
error?.response?.status === 401 || error?.response?.status === 403;

if (
!router.pathname.match(/(setup|login|resetpassword)/) &&
(!user || error) &&
sessionInvalid &&
!routing.current
) {
routing.current = true;
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UserType } from '@server/constants/user';
import type { PermissionCheckOptions } from '@server/lib/permissions';
import { hasPermission, Permission } from '@server/lib/permissions';
import type { NotificationAgentKey } from '@server/lib/settings';
import type { AxiosError } from 'axios';
import { useRouter } from 'next/router';
import type { MutatorCallback } from 'swr';
import useSWR from 'swr';
Expand Down Expand Up @@ -41,7 +42,7 @@ export interface UserSettings {
interface UserHookResponse {
user?: User;
loading: boolean;
error: string;
error?: AxiosError;
revalidate: (
data?: User | Promise<User> | MutatorCallback<User> | undefined,
shouldRevalidate?: boolean | undefined
Expand Down
Loading