ongudidan commited on
Commit
1e66528
·
verified ·
1 Parent(s): ec67eb6

Update app/api/auth/route.ts

Browse files
Files changed (1) hide show
  1. 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
- const body = await req.json();
5
- const { code } = body;
 
6
 
7
- if (!code) {
8
- return NextResponse.json(
9
- { error: "Code is required" },
10
- {
11
- status: 400,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  headers: {
13
- "Content-Type": "application/json",
 
14
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  }
16
- );
17
- }
18
 
19
- const Authorization = `Basic ${Buffer.from(
20
- `${process.env.OAUTH_CLIENT_ID}:${process.env.OAUTH_CLIENT_SECRET}`
21
- ).toString("base64")}`;
22
 
23
- const host =
24
- req.headers.get("host") ?? req.headers.get("origin") ?? "localhost:3000";
 
 
 
 
25
 
26
- const url = host.includes("/spaces/enzostvs")
27
- ? "enzostvs-deepsite.hf.space"
28
- : host;
29
- const redirect_uri =
30
- `${host.includes("localhost") ? "http://" : "https://"}` +
31
- url +
32
- "/auth/callback";
33
- const request_auth = await fetch("https://huggingface.co/oauth/token", {
34
- method: "POST",
35
- headers: {
36
- "Content-Type": "application/x-www-form-urlencoded",
37
- Authorization,
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
- status: 400,
52
- headers: {
53
- "Content-Type": "application/json",
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
- { user: null, errCode: userResponse.status },
68
- { status: userResponse.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
  }