enzostvs HF staff commited on
Commit
0781de4
·
1 Parent(s): f92945b
Files changed (1) hide show
  1. src/routes/api/auth/+server.ts +11 -7
src/routes/api/auth/+server.ts CHANGED
@@ -2,7 +2,7 @@
2
 
3
  import { json } from '@sveltejs/kit';
4
 
5
- export async function POST({ request }) {
6
  const { code } = await request.json()
7
 
8
  if (!code) {
@@ -13,24 +13,28 @@ export async function POST({ request }) {
13
  })
14
  }
15
 
16
- const REDIRECT_URI = `https://${process.env.SPACE_HOST}/login/callback`;
 
 
17
  const Authorization = `Basic ${Buffer.from(
18
  `${process.env.OAUTH_CLIENT_ID}:${process.env.OAUTH_CLIENT_SECRET}`
19
  ).toString("base64")}`;
20
 
 
 
 
 
 
21
  const request_auth = await fetch("https://huggingface.co/oauth/token", {
22
  method: "POST",
23
  headers: {
24
  "Content-Type": "application/x-www-form-urlencoded",
25
  Authorization,
26
  },
27
- body: new URLSearchParams({
28
- grant_type: "authorization_code",
29
- code: code,
30
- redirect_uri: REDIRECT_URI,
31
- }),
32
  });
33
 
 
34
  const { access_token } = await request_auth.json();
35
 
36
  if (!access_token) {
 
2
 
3
  import { json } from '@sveltejs/kit';
4
 
5
+ export async function POST({ request, fetch }) {
6
  const { code } = await request.json()
7
 
8
  if (!code) {
 
13
  })
14
  }
15
 
16
+ const first_space_host = process.env.SPACE_HOST?.split(',')[0] ?? process.env.SPACE_HOST
17
+
18
+ const REDIRECT_URI = `https://${first_space_host}/login/callback`;
19
  const Authorization = `Basic ${Buffer.from(
20
  `${process.env.OAUTH_CLIENT_ID}:${process.env.OAUTH_CLIENT_SECRET}`
21
  ).toString("base64")}`;
22
 
23
+ const formData = new FormData();
24
+ formData.append("grant_type", "authorization_code");
25
+ formData.append("code", code);
26
+ formData.append("redirect_uri", REDIRECT_URI);
27
+
28
  const request_auth = await fetch("https://huggingface.co/oauth/token", {
29
  method: "POST",
30
  headers: {
31
  "Content-Type": "application/x-www-form-urlencoded",
32
  Authorization,
33
  },
34
+ body: formData,
 
 
 
 
35
  });
36
 
37
+ const data = await request_auth.json();
38
  const { access_token } = await request_auth.json();
39
 
40
  if (!access_token) {