Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import { json, type RequestEvent } from '@sveltejs/kit'; | |
/** @type {import('./$types').RequestHandler} */ | |
export async function GET(request : RequestEvent) { | |
if (!request.cookies.get('hf_access_token')) { | |
return json({ | |
error: { | |
token: "You must be logged" | |
} | |
}, { status: 401 }) | |
} | |
const response = await fetch("https://huggingface.co/oauth/userinfo", { | |
method: "GET", | |
headers: { | |
Authorization: `Bearer ${request.cookies.get('hf_access_token')}`, | |
}, | |
}) | |
const user = await response.clone().json().catch(() => ({})); | |
if (!user?.sub) { | |
return json({ | |
error: { | |
token: "Token is invalid" | |
} | |
}, { status: 401 }) | |
} | |
return json({ | |
user | |
}) | |
} | |