Spaces:
Running
Running
File size: 1,270 Bytes
7bb6a57 0781de4 9fceaf0 7bb6a57 0781de4 7bb6a57 0781de4 7bb6a57 0781de4 7bb6a57 0781de4 7bb6a57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/** @type {import('./$types').RequestHandler} */
import { json } from '@sveltejs/kit';
export async function POST({ request, fetch }) {
const { code } = await request.json()
if (!code) {
return json({
message: `No code provided`,
}, {
status: 400
})
}
const first_space_host = process.env.SPACE_HOST?.split(',')[0] ?? process.env.SPACE_HOST
const REDIRECT_URI = `https://${first_space_host}/login/callback`;
const Authorization = `Basic ${Buffer.from(
`${process.env.OAUTH_CLIENT_ID}:${process.env.OAUTH_CLIENT_SECRET}`
).toString("base64")}`;
const formData = new FormData();
formData.append("grant_type", "authorization_code");
formData.append("code", code);
formData.append("redirect_uri", REDIRECT_URI);
const request_auth = await fetch("https://huggingface.co/oauth/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization,
},
body: formData,
});
const data = await request_auth.json();
const { access_token } = await request_auth.json();
if (!access_token) {
return json({
message: `No access token provided`,
}, {
status: 400
})
}
return json({
ok: true,
token: access_token
})
}
|