2
0

♻️ (viewer) Change to features-centric folder structure

This commit is contained in:
Baptiste Arnaud
2022-11-15 10:28:03 +01:00
committed by Baptiste Arnaud
parent 643571fe7d
commit a9d04798bc
80 changed files with 523 additions and 491 deletions

View File

@ -0,0 +1,19 @@
import { User } from 'db'
import { NextApiRequest } from 'next'
import prisma from '@/lib/prisma'
export const authenticateUser = async (
req: NextApiRequest
): Promise<User | undefined> => authenticateByToken(extractBearerToken(req))
const authenticateByToken = async (
apiToken?: string
): Promise<User | undefined> => {
if (!apiToken) return
return (await prisma.user.findFirst({
where: { apiTokens: { some: { token: apiToken } } },
})) as User
}
const extractBearerToken = (req: NextApiRequest) =>
req.headers['authorization']?.slice(7)