2023-12-30 05:56:40 -05:00
|
|
|
import { db } from "@/lib/db"
|
2024-01-04 16:57:32 -05:00
|
|
|
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
2023-12-30 05:56:40 -05:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
|
|
export async function GET(req: Request, { params } : { params: { id: string } }) {
|
|
|
|
try {
|
2024-01-04 16:57:32 -05:00
|
|
|
const user = await fetchUserWithImpersonation(req)
|
|
|
|
if (!user) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
2024-01-04 16:57:32 -05:00
|
|
|
}
|
|
|
|
|
2023-12-30 05:56:40 -05:00
|
|
|
let id = req.headers?.get('x-api-key')
|
|
|
|
if (id == null) {
|
2024-01-04 16:57:32 -05:00
|
|
|
return NextResponse.json(null);
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const tokens = await db.apiKey.findFirst({
|
|
|
|
where: {
|
|
|
|
id: id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return NextResponse.json(tokens);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("[TOKEN/GET]", error);
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function DELETE(req: Request, { params } : { params: { id: string } }) {
|
|
|
|
try {
|
2024-01-04 16:57:32 -05:00
|
|
|
const user = await fetchUserWithImpersonation(req)
|
|
|
|
if (!user) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
2024-01-04 16:57:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const { id } = params
|
2023-12-30 05:56:40 -05:00
|
|
|
const token = await db.apiKey.delete({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
userId: user?.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return NextResponse.json(token);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("[TOKEN/DELETE]", error);
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
}
|