Spaces:
Running
Running
Update app/api/auth/route.ts
Browse files- app/api/auth/route.ts +13 -14
app/api/auth/route.ts
CHANGED
|
@@ -6,10 +6,7 @@ export async function POST(req: NextRequest) {
|
|
| 6 |
const { code } = body;
|
| 7 |
|
| 8 |
if (!code) {
|
| 9 |
-
return NextResponse.json(
|
| 10 |
-
{ error: "Code is required" },
|
| 11 |
-
{ status: 400 }
|
| 12 |
-
);
|
| 13 |
}
|
| 14 |
|
| 15 |
const Authorization = `Basic ${Buffer.from(
|
|
@@ -28,7 +25,6 @@ export async function POST(req: NextRequest) {
|
|
| 28 |
url +
|
| 29 |
"/auth/callback";
|
| 30 |
|
| 31 |
-
// Helper to handle fallback between main and internal Hugging Face API
|
| 32 |
async function fetchToken() {
|
| 33 |
const params = new URLSearchParams({
|
| 34 |
grant_type: "authorization_code",
|
|
@@ -46,15 +42,18 @@ export async function POST(req: NextRequest) {
|
|
| 46 |
};
|
| 47 |
|
| 48 |
try {
|
| 49 |
-
// Try the main endpoint first
|
| 50 |
const res = await fetch("https://huggingface.co/oauth/token", options);
|
| 51 |
if (res.ok) return res;
|
| 52 |
throw new Error(`Primary endpoint failed: ${res.status}`);
|
| 53 |
-
} catch (err) {
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
console.warn("Retrying via internal API endpoint...");
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
|
@@ -68,7 +67,6 @@ export async function POST(req: NextRequest) {
|
|
| 68 |
);
|
| 69 |
}
|
| 70 |
|
| 71 |
-
// Retrieve user info
|
| 72 |
const userResponse = await fetch("https://huggingface.co/api/whoami-v2", {
|
| 73 |
headers: { Authorization: `Bearer ${response.access_token}` },
|
| 74 |
});
|
|
@@ -90,10 +88,11 @@ export async function POST(req: NextRequest) {
|
|
| 90 |
},
|
| 91 |
{ status: 200 }
|
| 92 |
);
|
| 93 |
-
} catch (error) {
|
| 94 |
-
|
|
|
|
| 95 |
return NextResponse.json(
|
| 96 |
-
{ error: "Internal Server Error", details:
|
| 97 |
{ status: 500 }
|
| 98 |
);
|
| 99 |
}
|
|
|
|
| 6 |
const { code } = body;
|
| 7 |
|
| 8 |
if (!code) {
|
| 9 |
+
return NextResponse.json({ error: "Code is required" }, { status: 400 });
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
|
| 12 |
const Authorization = `Basic ${Buffer.from(
|
|
|
|
| 25 |
url +
|
| 26 |
"/auth/callback";
|
| 27 |
|
|
|
|
| 28 |
async function fetchToken() {
|
| 29 |
const params = new URLSearchParams({
|
| 30 |
grant_type: "authorization_code",
|
|
|
|
| 42 |
};
|
| 43 |
|
| 44 |
try {
|
|
|
|
| 45 |
const res = await fetch("https://huggingface.co/oauth/token", options);
|
| 46 |
if (res.ok) return res;
|
| 47 |
throw new Error(`Primary endpoint failed: ${res.status}`);
|
| 48 |
+
} catch (err: unknown) {
|
| 49 |
+
const message =
|
| 50 |
+
err instanceof Error ? err.message : JSON.stringify(err);
|
| 51 |
+
console.warn("Primary token endpoint failed:", message);
|
| 52 |
console.warn("Retrying via internal API endpoint...");
|
| 53 |
+
return await fetch(
|
| 54 |
+
"https://api-inference.huggingface.co/oauth/token",
|
| 55 |
+
options
|
| 56 |
+
);
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
|
|
|
| 67 |
);
|
| 68 |
}
|
| 69 |
|
|
|
|
| 70 |
const userResponse = await fetch("https://huggingface.co/api/whoami-v2", {
|
| 71 |
headers: { Authorization: `Bearer ${response.access_token}` },
|
| 72 |
});
|
|
|
|
| 88 |
},
|
| 89 |
{ status: 200 }
|
| 90 |
);
|
| 91 |
+
} catch (error: unknown) {
|
| 92 |
+
const message = error instanceof Error ? error.message : String(error);
|
| 93 |
+
console.error("Auth callback error:", message);
|
| 94 |
return NextResponse.json(
|
| 95 |
+
{ error: "Internal Server Error", details: message },
|
| 96 |
{ status: 500 }
|
| 97 |
);
|
| 98 |
}
|