40 lines
780 B
TypeScript
40 lines
780 B
TypeScript
import { auth } from "@/auth";
|
|
import { db } from "./db";
|
|
|
|
export default async function fetchUserUsingAPI(req: Request) {
|
|
const session = await auth()
|
|
|
|
if (session) {
|
|
const user = await db.user.findFirst({
|
|
where: {
|
|
name: session.user?.name
|
|
}
|
|
})
|
|
|
|
return {
|
|
id: user?.id,
|
|
username: user?.name
|
|
}
|
|
}
|
|
|
|
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,
|
|
username: user?.name
|
|
}
|
|
} |