feat(platform): 开源发布 - 初始化 Open AI Canvas
This commit is contained in:
27
web/src/components/auth/auth-session-hydrator.tsx
Normal file
27
web/src/components/auth/auth-session-hydrator.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { applyUserSession } from "@/lib/user-session";
|
||||
import { getAuthSession } from "@/services/api/auth";
|
||||
import { FullScreenLoader } from "@/components/ui/aceternity/full-screen-loader";
|
||||
import { useUserStore } from "@/stores/use-user-store";
|
||||
|
||||
export function AuthSessionHydrator({ children }: { children: ReactNode }) {
|
||||
const hydrated = useUserStore((state) => state.hydrated);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
getAuthSession()
|
||||
.then(async (payload) => {
|
||||
if (!cancelled) await applyUserSession(payload);
|
||||
})
|
||||
.catch(async () => {
|
||||
if (!cancelled) await applyUserSession({ user: null, systemChannels: [] });
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return hydrated ? children : <FullScreenLoader />;
|
||||
}
|
||||
15
web/src/components/auth/require-auth.tsx
Normal file
15
web/src/components/auth/require-auth.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { Navigate, useLocation } from "react-router";
|
||||
|
||||
import { FullScreenLoader } from "@/components/ui/aceternity/full-screen-loader";
|
||||
import { useUserStore } from "@/stores/use-user-store";
|
||||
|
||||
export function RequireAuth({ children }: { children: ReactNode }) {
|
||||
const location = useLocation();
|
||||
const hydrated = useUserStore((state) => state.hydrated);
|
||||
const user = useUserStore((state) => state.user);
|
||||
|
||||
if (!hydrated) return <FullScreenLoader />;
|
||||
if (!user) return <Navigate to={`/login?next=${encodeURIComponent(location.pathname + location.search)}`} replace />;
|
||||
return children;
|
||||
}
|
||||
Reference in New Issue
Block a user