Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion src/context/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ 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.
const sessionInvalid =
error?.response?.status === 401 || error?.response?.status === 403;

if (
!router.pathname.match(/(setup|login|resetpassword)/) &&
(!user || error) &&
(!user || sessionInvalid) &&
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
!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