44 lines
957 B
TypeScript
44 lines
957 B
TypeScript
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
|
|
}
|
|
} |