43 lines
752 B
TypeScript
43 lines
752 B
TypeScript
import { auth } from "@/auth";
|
|
import { db } from "./db";
|
|
|
|
export default async function fetchUser(req: Request) {
|
|
const session = await auth()
|
|
|
|
if (session) {
|
|
const user = fetch(session.user.id)
|
|
if (user) {
|
|
return user
|
|
}
|
|
}
|
|
|
|
const token = req.headers?.get('x-api-key')
|
|
if (!token)
|
|
return null
|
|
|
|
const key = await db.apiKey.findFirst({
|
|
where: {
|
|
id: token as string
|
|
}
|
|
})
|
|
|
|
if (!key) return null
|
|
|
|
return fetch(key.userId)
|
|
}
|
|
|
|
const fetch = async (userId: string) => {
|
|
const user = await db.user.findFirst({
|
|
where: {
|
|
id: userId
|
|
}
|
|
})
|
|
|
|
if (!user) return null
|
|
|
|
return {
|
|
id: user.id,
|
|
username: user.name,
|
|
role: user.role,
|
|
}
|
|
} |