Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,395 Bytes
b1a4d81 d6da254 b1a4d81 d6da254 b1a4d81 d6da254 b1a4d81 d6da254 b1a4d81 8fce765 b628664 8fce765 b1a4d81 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import { json, type RequestEvent } from '@sveltejs/kit';
import prisma from '$lib/prisma';
/** @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 me = await response.clone().json().catch(() => ({}));
if (!me?.sub) {
return json({
error: {
token: "Token is invalid"
}
}, { status: 401 })
}
const userExist = await prisma.user.findFirst({
where: {
sub: me.sub
}
})
let user;
if (!userExist) {
user = await prisma.user.create({
data: {
sub: me.sub,
name: me.name,
preferred_username: me.preferred_username,
picture: me.picture
}
})
} else {
user = await prisma.user.update({
where: {
sub: me.sub
},
data: {
name: me.name,
preferred_username: me.preferred_username,
picture: me.picture
}
})
}
return json({
user: {
...user,
is_admin: process?.env?.SECRET_HF_ADMIN?.includes(user.sub)
}
})
}
|