hermes-web/lib/validate-api.ts

44 lines
957 B
TypeScript
Raw Normal View History

2023-12-30 05:56:40 -05:00
import { getServerSession } from "next-auth";
import { db } from "./db";
export default async function fetchUserUsingAPI(req: Request) {
const session = await getServerSession()
console.log("server session:", session)
if (session) {
const user = await db.user.findFirst({
where: {
username: session.user?.name?.toLowerCase() as string
}
})
return {
id: user?.id,
username: user?.username
}
}
const token = req.headers?.get('x-api-key')
console.log("x-api-key:", token)
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
}
})
console.log("user:", user)
return {
id: user?.id,
username: user?.username
}
}