Spaces:
Sleeping
Sleeping
File size: 3,012 Bytes
6678fa1 5a5180d d6f26ea 5a5180d d6f26ea 5a5180d 6678fa1 5a5180d 6678fa1 5a5180d 6678fa1 5a5180d 6678fa1 5a5180d 6678fa1 5a5180d 6678fa1 5a5180d 6678fa1 5a5180d 6678fa1 5a5180d 6678fa1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import { getLoginUrl } from "@/const";
import { trpc } from "@/lib/trpc";
import { TRPCClientError } from "@trpc/client";
import { useCallback, useEffect, useMemo } from "react";
const ACCESS_KEY_STORAGE = "access_key_verified";
// Mock user for access key authentication (no cookies needed)
// ID must match DEMO_USER_ID in importTestSamples.ts
const ACCESS_KEY_USER = {
id: 1,
name: "Demo User",
email: "demo@aimusic.attribution",
};
type UseAuthOptions = {
redirectOnUnauthenticated?: boolean;
redirectPath?: string;
};
export function useAuth(options?: UseAuthOptions) {
const { redirectOnUnauthenticated = false, redirectPath = getLoginUrl() } =
options ?? {};
const utils = trpc.useUtils();
// Check if access key is verified (works in Safari iframe, no cookies needed)
const accessKeyVerified = typeof window !== "undefined" &&
sessionStorage.getItem(ACCESS_KEY_STORAGE) === "true";
const meQuery = trpc.auth.me.useQuery(undefined, {
retry: false,
refetchOnWindowFocus: false,
// Skip query if access key is verified - we don't need cookie auth
enabled: !accessKeyVerified,
});
const logoutMutation = trpc.auth.logout.useMutation({
onSuccess: () => {
utils.auth.me.setData(undefined, null);
},
});
const logout = useCallback(async () => {
try {
await logoutMutation.mutateAsync();
} catch (error: unknown) {
if (
error instanceof TRPCClientError &&
error.data?.code === "UNAUTHORIZED"
) {
return;
}
throw error;
} finally {
// Also clear access key on logout
sessionStorage.removeItem(ACCESS_KEY_STORAGE);
utils.auth.me.setData(undefined, null);
await utils.auth.me.invalidate();
}
}, [logoutMutation, utils]);
const state = useMemo(() => {
// If access key is verified, use mock user (no cookies needed - Safari iframe fix)
const effectiveUser = accessKeyVerified ? ACCESS_KEY_USER : meQuery.data;
localStorage.setItem(
"manus-runtime-user-info",
JSON.stringify(effectiveUser)
);
return {
user: effectiveUser ?? null,
loading: !accessKeyVerified && (meQuery.isLoading || logoutMutation.isPending),
error: meQuery.error ?? logoutMutation.error ?? null,
isAuthenticated: Boolean(effectiveUser),
};
}, [
accessKeyVerified,
meQuery.data,
meQuery.error,
meQuery.isLoading,
logoutMutation.error,
logoutMutation.isPending,
]);
useEffect(() => {
if (!redirectOnUnauthenticated) return;
if (meQuery.isLoading || logoutMutation.isPending) return;
if (state.user) return;
if (typeof window === "undefined") return;
if (window.location.pathname === redirectPath) return;
window.location.href = redirectPath
}, [
redirectOnUnauthenticated,
redirectPath,
logoutMutation.isPending,
meQuery.isLoading,
state.user,
]);
return {
...state,
refresh: () => meQuery.refetch(),
logout,
};
}
|