memos/web/src/utils/auth-redirect.ts
Johnny 85f4fc7a75 refactor: remove MemoContentContext and integrate MemoViewContext
- Deleted MemoContentContext and its associated types.
- Updated Tag and TaskListItem components to use MemoViewContext instead.
- Refactored MemoContent component to eliminate context provider and directly use derived values.
- Simplified MemoViewContext to only include essential data.
- Enhanced error handling in various components by introducing a centralized error handling utility.
- Improved type safety across components and hooks by refining TypeScript definitions.
- Updated remark plugins to enhance tag parsing and preserve node types.
2025-12-28 12:46:12 +08:00

36 lines
1.2 KiB
TypeScript

import { getInstanceConfig } from "@/instance-config";
import { ROUTES } from "@/router/routes";
const PUBLIC_ROUTES = [
ROUTES.AUTH, // Authentication pages
ROUTES.EXPLORE, // Explore page
"/u/", // User profile pages (dynamic)
"/memos/", // Individual memo detail pages (dynamic)
] as const;
const PRIVATE_ROUTES = [ROUTES.ROOT, ROUTES.ATTACHMENTS, ROUTES.INBOX, ROUTES.ARCHIVED, ROUTES.SETTING] as const;
function isPublicRoute(path: string): boolean {
return PUBLIC_ROUTES.some((route) => path.startsWith(route));
}
function isPrivateRoute(path: string): boolean {
return PRIVATE_ROUTES.includes(path as (typeof PRIVATE_ROUTES)[number]);
}
export function redirectOnAuthFailure(): void {
const currentPath = window.location.pathname;
// Don't redirect if it's a public route
if (isPublicRoute(currentPath)) {
return;
}
const disallowPublicVisibility = getInstanceConfig().memoRelatedSetting.disallowPublicVisibility;
const target = disallowPublicVisibility ? ROUTES.AUTH : ROUTES.EXPLORE;
// Only redirect if it's a private route or disallowPublicVisibility is enabled
if (disallowPublicVisibility || isPrivateRoute(currentPath)) {
window.location.replace(target);
}
}