hermes-web/lib/validate-api.ts

40 lines
780 B
TypeScript
Raw Normal View History

2024-01-02 02:26:20 -05:00
import { auth } from "@/auth";
2023-12-30 05:56:40 -05:00
import { db } from "./db";
export default async function fetchUserUsingAPI(req: Request) {
2024-01-02 02:26:20 -05:00
const session = await auth()
2023-12-30 05:56:40 -05:00
if (session) {
const user = await db.user.findFirst({
where: {
2024-01-02 02:26:20 -05:00
name: session.user?.name
2023-12-30 05:56:40 -05:00
}
})
return {
id: user?.id,
2024-01-02 02:26:20 -05:00
username: user?.name
2023-12-30 05:56:40 -05:00
}
}
const token = req.headers?.get('x-api-key')
if (token === null || token === undefined)
return null
const key = await db.apiKey.findFirst({
where: {
id: token as string
}
})
const user = await db.user.findFirst({
where: {
id: key?.userId
}
})
return {
id: user?.id,
2024-01-02 02:26:20 -05:00
username: user?.name
2023-12-30 05:56:40 -05:00
}
}