♻️ (viewer) Change to features-centric folder structure
This commit is contained in:
committed by
Baptiste Arnaud
parent
643571fe7d
commit
a9d04798bc
19
apps/viewer/src/features/auth/api/authenticateUser.ts
Normal file
19
apps/viewer/src/features/auth/api/authenticateUser.ts
Normal 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)
|
1
apps/viewer/src/features/auth/api/index.ts
Normal file
1
apps/viewer/src/features/auth/api/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export { authenticateUser } from './authenticateUser'
|
Reference in New Issue
Block a user