Spaces:
Runtime error
Runtime error
add admin check
Browse files
app/api/collections/[id]/route.ts
CHANGED
@@ -1,17 +1,13 @@
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
3 |
import { RemoverDataset } from '@/utils/remover'
|
|
|
4 |
|
5 |
const prisma = new PrismaClient()
|
6 |
|
7 |
export async function DELETE(request: Request, { params }: { params: { id: string } }) {
|
8 |
const { headers } = request
|
9 |
-
|
10 |
-
const staff_flag_id = headers.get('x-staff-flag-id') ?? undefined
|
11 |
-
|
12 |
-
// @ts-ignore
|
13 |
-
const HF_ADMIN = process?.env?.HF_ADMIN.split(',') ?? []
|
14 |
-
const is_admin = staff_flag_id ? HF_ADMIN.includes(staff_flag_id) : false
|
15 |
|
16 |
if (!is_admin) {
|
17 |
return Response.json(
|
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
3 |
import { RemoverDataset } from '@/utils/remover'
|
4 |
+
import { isAdmin } from '@/utils/checker/is_admin'
|
5 |
|
6 |
const prisma = new PrismaClient()
|
7 |
|
8 |
export async function DELETE(request: Request, { params }: { params: { id: string } }) {
|
9 |
const { headers } = request
|
10 |
+
const is_admin = await isAdmin(headers)
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
if (!is_admin) {
|
13 |
return Response.json(
|
app/api/collections/[id]/visibility/route.ts
CHANGED
@@ -1,15 +1,13 @@
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
|
|
|
|
3 |
const prisma = new PrismaClient()
|
4 |
|
5 |
export async function PUT(request: Request, { params }: { params: { id: string } }) {
|
6 |
const { headers } = request
|
7 |
|
8 |
-
const
|
9 |
-
|
10 |
-
// @ts-ignore
|
11 |
-
const HF_ADMIN = process?.env?.HF_ADMIN.split(',') ?? []
|
12 |
-
const is_admin = staff_flag_id ? HF_ADMIN.includes(staff_flag_id) : false
|
13 |
|
14 |
if (!is_admin) {
|
15 |
return Response.json(
|
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
3 |
+
import { isAdmin } from '@/utils/checker/is_admin'
|
4 |
+
|
5 |
const prisma = new PrismaClient()
|
6 |
|
7 |
export async function PUT(request: Request, { params }: { params: { id: string } }) {
|
8 |
const { headers } = request
|
9 |
|
10 |
+
const is_admin = await isAdmin(headers)
|
|
|
|
|
|
|
|
|
11 |
|
12 |
if (!is_admin) {
|
13 |
return Response.json(
|
app/api/collections/route.ts
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
|
|
|
|
3 |
const prisma = new PrismaClient()
|
4 |
|
5 |
export async function GET(request: Request) {
|
@@ -8,13 +10,10 @@ export async function GET(request: Request) {
|
|
8 |
const userId = searchParams.get('id') ?? undefined
|
9 |
const page = searchParams.get('page') ? parseInt(searchParams.get('page') as string) : 0
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
const is_admin = staff_flag_id ? HF_ADMIN.includes(staff_flag_id) : false
|
16 |
-
|
17 |
-
console.log('is_admin', HF_ADMIN, is_admin, staff_flag_id)
|
18 |
|
19 |
const collections = await prisma.collection.findMany({
|
20 |
orderBy: {
|
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
3 |
+
import { isAdmin } from '@/utils/checker/is_admin'
|
4 |
+
|
5 |
const prisma = new PrismaClient()
|
6 |
|
7 |
export async function GET(request: Request) {
|
|
|
10 |
const userId = searchParams.get('id') ?? undefined
|
11 |
const page = searchParams.get('page') ? parseInt(searchParams.get('page') as string) : 0
|
12 |
|
13 |
+
let is_admin = false
|
14 |
+
if (headers.get('Authorization') ) {
|
15 |
+
is_admin = await isAdmin(headers) as boolean
|
16 |
+
}
|
|
|
|
|
|
|
17 |
|
18 |
const collections = await prisma.collection.findMany({
|
19 |
orderBy: {
|
app/api/login/route.ts
CHANGED
@@ -2,7 +2,7 @@ export async function GET() {
|
|
2 |
const REDIRECT_URI = `https://${process.env.SPACE_HOST}/login/callback`
|
3 |
return Response.json(
|
4 |
{
|
5 |
-
redirect: `https://huggingface.co/oauth/authorize?client_id=${process.env.OAUTH_CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=openid%20profile
|
6 |
status: 200,
|
7 |
ok: true
|
8 |
}
|
|
|
2 |
const REDIRECT_URI = `https://${process.env.SPACE_HOST}/login/callback`
|
3 |
return Response.json(
|
4 |
{
|
5 |
+
redirect: `https://huggingface.co/oauth/authorize?client_id=${process.env.OAUTH_CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=openid%20profile&state=STATE&response_type=code`,
|
6 |
status: 200,
|
7 |
ok: true
|
8 |
}
|
components/main/hooks/useCollections.ts
CHANGED
@@ -7,7 +7,7 @@ import { useUser } from "@/utils/useUser";
|
|
7 |
|
8 |
export const useCollections = (category: string) => {
|
9 |
const [loading, setLoading] = useState(false);
|
10 |
-
const { user, loading: userLoading } = useUser();
|
11 |
|
12 |
const client = useQueryClient();
|
13 |
|
@@ -24,7 +24,7 @@ export const useCollections = (category: string) => {
|
|
24 |
|
25 |
const response = await fetch(`/api/collections?${queryParams.toString()}`, {
|
26 |
headers: {
|
27 |
-
'
|
28 |
},
|
29 |
method: "GET",
|
30 |
})
|
|
|
7 |
|
8 |
export const useCollections = (category: string) => {
|
9 |
const [loading, setLoading] = useState(false);
|
10 |
+
const { user, loading: userLoading, token } = useUser();
|
11 |
|
12 |
const client = useQueryClient();
|
13 |
|
|
|
24 |
|
25 |
const response = await fetch(`/api/collections?${queryParams.toString()}`, {
|
26 |
headers: {
|
27 |
+
...(user?.sub ? { 'Authorization': token } : {})
|
28 |
},
|
29 |
method: "GET",
|
30 |
})
|
components/modal/useCollection.ts
CHANGED
@@ -3,10 +3,9 @@ import { useQuery, useQueryClient } from "@tanstack/react-query"
|
|
3 |
|
4 |
import { Collection, Image } from "@/utils/type"
|
5 |
import { useUser } from "@/utils/useUser"
|
6 |
-
import { set } from "lodash"
|
7 |
|
8 |
export const useCollection = (id?: string) => {
|
9 |
-
const { user } = useUser()
|
10 |
const [loading, setLoading] = useState(false)
|
11 |
|
12 |
const { data: open } = useQuery(["modal"], () => {
|
@@ -36,7 +35,7 @@ export const useCollection = (id?: string) => {
|
|
36 |
const response = await fetch(`/api/collections/${collection?.id}/visibility`, {
|
37 |
method: "PUT",
|
38 |
headers: {
|
39 |
-
'
|
40 |
}
|
41 |
})
|
42 |
|
@@ -62,7 +61,7 @@ export const useCollection = (id?: string) => {
|
|
62 |
const response = await fetch(`/api/collections/${collection?.id}`, {
|
63 |
method: "DELETE",
|
64 |
headers: {
|
65 |
-
'
|
66 |
}
|
67 |
})
|
68 |
|
|
|
3 |
|
4 |
import { Collection, Image } from "@/utils/type"
|
5 |
import { useUser } from "@/utils/useUser"
|
|
|
6 |
|
7 |
export const useCollection = (id?: string) => {
|
8 |
+
const { user, token } = useUser()
|
9 |
const [loading, setLoading] = useState(false)
|
10 |
|
11 |
const { data: open } = useQuery(["modal"], () => {
|
|
|
35 |
const response = await fetch(`/api/collections/${collection?.id}/visibility`, {
|
36 |
method: "PUT",
|
37 |
headers: {
|
38 |
+
'Authorization': user?.sub ? token : "",
|
39 |
}
|
40 |
})
|
41 |
|
|
|
61 |
const response = await fetch(`/api/collections/${collection?.id}`, {
|
62 |
method: "DELETE",
|
63 |
headers: {
|
64 |
+
'Authorization': user?.sub ? token : "",
|
65 |
}
|
66 |
})
|
67 |
|
utils/checker/is_admin.ts
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export const isAdmin = async (headers: Headers) => {
|
2 |
+
return new Promise(async (resolve, reject) => {
|
3 |
+
|
4 |
+
const Authorization = headers.get('Authorization') ?? undefined
|
5 |
+
|
6 |
+
// @ts-ignore
|
7 |
+
const HF_ADMIN = process?.env?.HF_ADMIN.split(',') ?? []
|
8 |
+
|
9 |
+
const userRequest = await fetch("https://huggingface.co/oauth/userinfo", {
|
10 |
+
method: "GET",
|
11 |
+
headers: {
|
12 |
+
Authorization: `Bearer ${Authorization}`,
|
13 |
+
},
|
14 |
+
})
|
15 |
+
|
16 |
+
const user = await userRequest.clone().json().catch(() => ({}));
|
17 |
+
const is_admin = user?.sub ? HF_ADMIN.includes(user?.sub) : false
|
18 |
+
|
19 |
+
resolve(is_admin)
|
20 |
+
})
|
21 |
+
|
22 |
+
}
|
utils/useUser.ts
CHANGED
@@ -70,6 +70,7 @@ export const useUser = () => {
|
|
70 |
user,
|
71 |
refetch,
|
72 |
loading,
|
|
|
73 |
getAuthorization
|
74 |
}
|
75 |
}
|
|
|
70 |
user,
|
71 |
refetch,
|
72 |
loading,
|
73 |
+
token: `Bearer ${value}`,
|
74 |
getAuthorization
|
75 |
}
|
76 |
}
|