Spaces:
Running
Running
Update app/api/auth/route.ts
Browse files- app/api/auth/route.ts +82 -68
app/api/auth/route.ts
CHANGED
|
@@ -1,86 +1,100 @@
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
|
| 3 |
export async function POST(req: NextRequest) {
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
headers: {
|
| 13 |
-
"Content-Type": "application/
|
|
|
|
| 14 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
-
|
| 17 |
-
}
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
).toString("base64")}`;
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
body: new URLSearchParams({
|
| 40 |
-
grant_type: "authorization_code",
|
| 41 |
-
code,
|
| 42 |
-
redirect_uri,
|
| 43 |
-
}),
|
| 44 |
-
});
|
| 45 |
|
| 46 |
-
const response = await request_auth.json();
|
| 47 |
-
if (!response.access_token) {
|
| 48 |
return NextResponse.json(
|
| 49 |
-
{ error: "Failed to retrieve access token" },
|
| 50 |
{
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
}
|
| 56 |
);
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
const userResponse = await fetch("https://huggingface.co/api/whoami-v2", {
|
| 60 |
-
headers: {
|
| 61 |
-
Authorization: `Bearer ${response.access_token}`,
|
| 62 |
-
},
|
| 63 |
-
});
|
| 64 |
-
|
| 65 |
-
if (!userResponse.ok) {
|
| 66 |
return NextResponse.json(
|
| 67 |
-
{
|
| 68 |
-
{ status:
|
| 69 |
);
|
| 70 |
}
|
| 71 |
-
const user = await userResponse.json();
|
| 72 |
-
|
| 73 |
-
return NextResponse.json(
|
| 74 |
-
{
|
| 75 |
-
access_token: response.access_token,
|
| 76 |
-
expires_in: response.expires_in,
|
| 77 |
-
user,
|
| 78 |
-
},
|
| 79 |
-
{
|
| 80 |
-
status: 200,
|
| 81 |
-
headers: {
|
| 82 |
-
"Content-Type": "application/json",
|
| 83 |
-
},
|
| 84 |
-
}
|
| 85 |
-
);
|
| 86 |
}
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
|
| 3 |
export async function POST(req: NextRequest) {
|
| 4 |
+
try {
|
| 5 |
+
const body = await req.json();
|
| 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(
|
| 16 |
+
`${process.env.OAUTH_CLIENT_ID}:${process.env.OAUTH_CLIENT_SECRET}`
|
| 17 |
+
).toString("base64")}`;
|
| 18 |
+
|
| 19 |
+
const host =
|
| 20 |
+
req.headers.get("host") ?? req.headers.get("origin") ?? "localhost:3000";
|
| 21 |
+
|
| 22 |
+
const url = host.includes("/spaces/enzostvs")
|
| 23 |
+
? "enzostvs-deepsite.hf.space"
|
| 24 |
+
: host;
|
| 25 |
+
|
| 26 |
+
const redirect_uri =
|
| 27 |
+
`${host.includes("localhost") ? "http://" : "https://"}` +
|
| 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",
|
| 35 |
+
code,
|
| 36 |
+
redirect_uri,
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
const options = {
|
| 40 |
+
method: "POST",
|
| 41 |
headers: {
|
| 42 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 43 |
+
Authorization,
|
| 44 |
},
|
| 45 |
+
body: params,
|
| 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 |
+
console.warn("Primary token endpoint failed:", err.message);
|
| 55 |
+
console.warn("Retrying via internal API endpoint...");
|
| 56 |
+
// Fallback to internal endpoint
|
| 57 |
+
return await fetch("https://api-inference.huggingface.co/oauth/token", options);
|
| 58 |
}
|
| 59 |
+
}
|
|
|
|
| 60 |
|
| 61 |
+
const request_auth = await fetchToken();
|
| 62 |
+
const response = await request_auth.json();
|
|
|
|
| 63 |
|
| 64 |
+
if (!response.access_token) {
|
| 65 |
+
return NextResponse.json(
|
| 66 |
+
{ error: "Failed to retrieve access token", details: response },
|
| 67 |
+
{ status: 400 }
|
| 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 |
+
});
|
| 75 |
+
|
| 76 |
+
if (!userResponse.ok) {
|
| 77 |
+
return NextResponse.json(
|
| 78 |
+
{ user: null, errCode: userResponse.status },
|
| 79 |
+
{ status: userResponse.status }
|
| 80 |
+
);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
const user = await userResponse.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
| 85 |
return NextResponse.json(
|
|
|
|
| 86 |
{
|
| 87 |
+
access_token: response.access_token,
|
| 88 |
+
expires_in: response.expires_in,
|
| 89 |
+
user,
|
| 90 |
+
},
|
| 91 |
+
{ status: 200 }
|
| 92 |
);
|
| 93 |
+
} catch (error) {
|
| 94 |
+
console.error("Auth callback error:", error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return NextResponse.json(
|
| 96 |
+
{ error: "Internal Server Error", details: error.message },
|
| 97 |
+
{ status: 500 }
|
| 98 |
);
|
| 99 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|