Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
client/src/_core/hooks/useAuth.ts
CHANGED
|
@@ -3,6 +3,15 @@ import { trpc } from "@/lib/trpc";
|
|
| 3 |
import { TRPCClientError } from "@trpc/client";
|
| 4 |
import { useCallback, useEffect, useMemo } from "react";
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
type UseAuthOptions = {
|
| 7 |
redirectOnUnauthenticated?: boolean;
|
| 8 |
redirectPath?: string;
|
|
@@ -13,9 +22,15 @@ export function useAuth(options?: UseAuthOptions) {
|
|
| 13 |
options ?? {};
|
| 14 |
const utils = trpc.useUtils();
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
const meQuery = trpc.auth.me.useQuery(undefined, {
|
| 17 |
retry: false,
|
| 18 |
refetchOnWindowFocus: false,
|
|
|
|
|
|
|
| 19 |
});
|
| 20 |
|
| 21 |
const logoutMutation = trpc.auth.logout.useMutation({
|
|
@@ -36,23 +51,29 @@ export function useAuth(options?: UseAuthOptions) {
|
|
| 36 |
}
|
| 37 |
throw error;
|
| 38 |
} finally {
|
|
|
|
|
|
|
| 39 |
utils.auth.me.setData(undefined, null);
|
| 40 |
await utils.auth.me.invalidate();
|
| 41 |
}
|
| 42 |
}, [logoutMutation, utils]);
|
| 43 |
|
| 44 |
const state = useMemo(() => {
|
|
|
|
|
|
|
|
|
|
| 45 |
localStorage.setItem(
|
| 46 |
"manus-runtime-user-info",
|
| 47 |
-
JSON.stringify(
|
| 48 |
);
|
| 49 |
return {
|
| 50 |
-
user:
|
| 51 |
-
loading: meQuery.isLoading || logoutMutation.isPending,
|
| 52 |
error: meQuery.error ?? logoutMutation.error ?? null,
|
| 53 |
-
isAuthenticated: Boolean(
|
| 54 |
};
|
| 55 |
}, [
|
|
|
|
| 56 |
meQuery.data,
|
| 57 |
meQuery.error,
|
| 58 |
meQuery.isLoading,
|
|
|
|
| 3 |
import { TRPCClientError } from "@trpc/client";
|
| 4 |
import { useCallback, useEffect, useMemo } from "react";
|
| 5 |
|
| 6 |
+
const ACCESS_KEY_STORAGE = "access_key_verified";
|
| 7 |
+
|
| 8 |
+
// Mock user for access key authentication (no cookies needed)
|
| 9 |
+
const ACCESS_KEY_USER = {
|
| 10 |
+
id: "access_key_user",
|
| 11 |
+
name: "Demo User",
|
| 12 |
+
email: "demo@aimusic.attribution",
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
type UseAuthOptions = {
|
| 16 |
redirectOnUnauthenticated?: boolean;
|
| 17 |
redirectPath?: string;
|
|
|
|
| 22 |
options ?? {};
|
| 23 |
const utils = trpc.useUtils();
|
| 24 |
|
| 25 |
+
// Check if access key is verified (works in Safari iframe, no cookies needed)
|
| 26 |
+
const accessKeyVerified = typeof window !== "undefined" &&
|
| 27 |
+
sessionStorage.getItem(ACCESS_KEY_STORAGE) === "true";
|
| 28 |
+
|
| 29 |
const meQuery = trpc.auth.me.useQuery(undefined, {
|
| 30 |
retry: false,
|
| 31 |
refetchOnWindowFocus: false,
|
| 32 |
+
// Skip query if access key is verified - we don't need cookie auth
|
| 33 |
+
enabled: !accessKeyVerified,
|
| 34 |
});
|
| 35 |
|
| 36 |
const logoutMutation = trpc.auth.logout.useMutation({
|
|
|
|
| 51 |
}
|
| 52 |
throw error;
|
| 53 |
} finally {
|
| 54 |
+
// Also clear access key on logout
|
| 55 |
+
sessionStorage.removeItem(ACCESS_KEY_STORAGE);
|
| 56 |
utils.auth.me.setData(undefined, null);
|
| 57 |
await utils.auth.me.invalidate();
|
| 58 |
}
|
| 59 |
}, [logoutMutation, utils]);
|
| 60 |
|
| 61 |
const state = useMemo(() => {
|
| 62 |
+
// If access key is verified, use mock user (no cookies needed - Safari iframe fix)
|
| 63 |
+
const effectiveUser = accessKeyVerified ? ACCESS_KEY_USER : meQuery.data;
|
| 64 |
+
|
| 65 |
localStorage.setItem(
|
| 66 |
"manus-runtime-user-info",
|
| 67 |
+
JSON.stringify(effectiveUser)
|
| 68 |
);
|
| 69 |
return {
|
| 70 |
+
user: effectiveUser ?? null,
|
| 71 |
+
loading: !accessKeyVerified && (meQuery.isLoading || logoutMutation.isPending),
|
| 72 |
error: meQuery.error ?? logoutMutation.error ?? null,
|
| 73 |
+
isAuthenticated: Boolean(effectiveUser),
|
| 74 |
};
|
| 75 |
}, [
|
| 76 |
+
accessKeyVerified,
|
| 77 |
meQuery.data,
|
| 78 |
meQuery.error,
|
| 79 |
meQuery.isLoading,
|