Spaces:
Running
Running
login through iframe
Browse files- hooks/useUser.ts +43 -0
hooks/useUser.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { ProjectType, User } from "@/types";
|
|
| 8 |
import { api } from "@/lib/api";
|
| 9 |
import { toast } from "sonner";
|
| 10 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
export const useUser = (initialData?: {
|
|
@@ -63,8 +64,50 @@ export const useUser = (initialData?: {
|
|
| 63 |
client.setQueryData(["me.projects"], projects);
|
| 64 |
};
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
const openLoginWindow = async () => {
|
| 67 |
setCurrentRoute(window.location.pathname);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return router.push("/auth");
|
| 69 |
};
|
| 70 |
|
|
|
|
| 8 |
import { api } from "@/lib/api";
|
| 9 |
import { toast } from "sonner";
|
| 10 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 11 |
+
import { useBroadcastChannel } from "@/lib/useBroadcastChannel";
|
| 12 |
|
| 13 |
|
| 14 |
export const useUser = (initialData?: {
|
|
|
|
| 64 |
client.setQueryData(["me.projects"], projects);
|
| 65 |
};
|
| 66 |
|
| 67 |
+
// Listen for OAuth callback from popup window
|
| 68 |
+
useBroadcastChannel("auth", (message: any) => {
|
| 69 |
+
if (message.type === "user-oauth" && message.code) {
|
| 70 |
+
loginFromCode(message.code);
|
| 71 |
+
}
|
| 72 |
+
});
|
| 73 |
+
|
| 74 |
+
const isInIframe = () => {
|
| 75 |
+
try {
|
| 76 |
+
return window.self !== window.top;
|
| 77 |
+
} catch {
|
| 78 |
+
return true;
|
| 79 |
+
}
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
const openLoginWindow = async () => {
|
| 83 |
setCurrentRoute(window.location.pathname);
|
| 84 |
+
|
| 85 |
+
// If in iframe, open OAuth in popup window
|
| 86 |
+
if (isInIframe()) {
|
| 87 |
+
try {
|
| 88 |
+
const response = await api.get("/auth/login-url");
|
| 89 |
+
const loginUrl = response.data?.loginUrl;
|
| 90 |
+
|
| 91 |
+
if (loginUrl) {
|
| 92 |
+
const width = 600;
|
| 93 |
+
const height = 700;
|
| 94 |
+
const left = (window.screen.width - width) / 2;
|
| 95 |
+
const top = (window.screen.height - height) / 2;
|
| 96 |
+
|
| 97 |
+
window.open(
|
| 98 |
+
loginUrl,
|
| 99 |
+
"HuggingFace Login",
|
| 100 |
+
`width=${width},height=${height},left=${left},top=${top},popup=yes`
|
| 101 |
+
);
|
| 102 |
+
return;
|
| 103 |
+
}
|
| 104 |
+
} catch (error) {
|
| 105 |
+
console.error("Failed to open login popup:", error);
|
| 106 |
+
toast.error("Failed to open login window");
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Default: navigate to auth page (for non-iframe contexts)
|
| 111 |
return router.push("/auth");
|
| 112 |
};
|
| 113 |
|