Spaces:
Sleeping
Sleeping
fix: Expose Oauth Client ID to static frontend
Browse files- Added `/api/config/oauth` GET endpoint to FastAPI to expose the Space's automatically injected `OAUTH_CLIENT_ID`.
- Modified `ClientApp.tsx` to fetch the Client ID before attempting OAuth redirect validation or login URL generation.
- Fixes "Missing clientId" errors thrown by `@huggingface/hub` caused by the static Next.js HTML being completely disconnected from the Docker container's environment variables.
- api.py +6 -0
- frontend/src/components/ClientApp.tsx +16 -2
api.py
CHANGED
|
@@ -149,6 +149,12 @@ def generate_image(req: ImageGenerationRequest):
|
|
| 149 |
else:
|
| 150 |
raise HTTPException(status_code=500, detail=status_msg or "Failed to generate image.")
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
@app.post("/api/save_character")
|
| 153 |
def save_character(req: PromptRequest):
|
| 154 |
args = req.to_args()
|
|
|
|
| 149 |
else:
|
| 150 |
raise HTTPException(status_code=500, detail=status_msg or "Failed to generate image.")
|
| 151 |
|
| 152 |
+
@app.get("/api/config/oauth")
|
| 153 |
+
def get_oauth_config():
|
| 154 |
+
return {
|
| 155 |
+
"oauth_client_id": os.environ.get("OAUTH_CLIENT_ID")
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
@app.post("/api/save_character")
|
| 159 |
def save_character(req: PromptRequest):
|
| 160 |
args = req.to_args()
|
frontend/src/components/ClientApp.tsx
CHANGED
|
@@ -45,7 +45,12 @@ export default function ClientApp() {
|
|
| 45 |
// 1. Handle OAuth Redirect
|
| 46 |
const handleOAuth = async () => {
|
| 47 |
try {
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if (oauthResult) {
|
| 50 |
setHfToken(oauthResult.accessToken);
|
| 51 |
localStorage.setItem('hf_auth_token', oauthResult.accessToken);
|
|
@@ -53,7 +58,7 @@ export default function ClientApp() {
|
|
| 53 |
window.history.replaceState({}, document.title, window.location.pathname);
|
| 54 |
setStatusMsg("Successfully authenticated with Hugging Face!");
|
| 55 |
}
|
| 56 |
-
} catch (e) {
|
| 57 |
console.error("OAuth error", e);
|
| 58 |
}
|
| 59 |
};
|
|
@@ -263,7 +268,16 @@ export default function ClientApp() {
|
|
| 263 |
|
| 264 |
const handleHFLogin = async () => {
|
| 265 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
const url = await oauthLoginUrl({
|
|
|
|
| 267 |
scopes: "openid profile inference-api"
|
| 268 |
});
|
| 269 |
window.location.href = url;
|
|
|
|
| 45 |
// 1. Handle OAuth Redirect
|
| 46 |
const handleOAuth = async () => {
|
| 47 |
try {
|
| 48 |
+
// First fetch the client ID provided by Hugging Face to our backend
|
| 49 |
+
const configRes = await fetch(`${API_URL}/api/config/oauth`);
|
| 50 |
+
const config = await configRes.json();
|
| 51 |
+
const clientId = config.oauth_client_id;
|
| 52 |
+
|
| 53 |
+
const oauthResult = await oauthHandleRedirectIfPresent(clientId ? { clientId } : undefined);
|
| 54 |
if (oauthResult) {
|
| 55 |
setHfToken(oauthResult.accessToken);
|
| 56 |
localStorage.setItem('hf_auth_token', oauthResult.accessToken);
|
|
|
|
| 58 |
window.history.replaceState({}, document.title, window.location.pathname);
|
| 59 |
setStatusMsg("Successfully authenticated with Hugging Face!");
|
| 60 |
}
|
| 61 |
+
} catch (e: any) {
|
| 62 |
console.error("OAuth error", e);
|
| 63 |
}
|
| 64 |
};
|
|
|
|
| 268 |
|
| 269 |
const handleHFLogin = async () => {
|
| 270 |
try {
|
| 271 |
+
const configRes = await fetch(`${API_URL}/api/config/oauth`);
|
| 272 |
+
const config = await configRes.json();
|
| 273 |
+
const clientId = config.oauth_client_id;
|
| 274 |
+
|
| 275 |
+
if (!clientId) {
|
| 276 |
+
throw new Error("OAUTH_CLIENT_ID not found. Is your Space configured correctly with hf_oauth: true?");
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
const url = await oauthLoginUrl({
|
| 280 |
+
clientId: clientId,
|
| 281 |
scopes: "openid profile inference-api"
|
| 282 |
});
|
| 283 |
window.location.href = url;
|