mise en place de la vérification du tioken d\'authntification
This commit is contained in:
@@ -22,4 +22,7 @@ export const Errors = {
|
||||
INVALID_TOKEN: new AppError('INVALID_TOKEN', 400, 'Invalid or already used token'),
|
||||
TOKEN_EXPIRED: new AppError('TOKEN_EXPIRED', 400, 'Token has expired'),
|
||||
ALREADY_CONFIRMED: new AppError('ALREADY_CONFIRMED', 400, 'User is already confirmed'),
|
||||
|
||||
//Auth errors
|
||||
UNAUTHORIZED: new AppError('UNAUTHORIZED', 401, 'Non authentifié'),
|
||||
}
|
||||
16
src/middleware/verifyAuth.ts
Normal file
16
src/middleware/verifyAuth.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { FastifyRequest, FastifyReply } from 'fastify'
|
||||
import { verifyAuthToken } from '../services/authToken.service.js'
|
||||
import { Errors } from '../errors/AppError.js'
|
||||
|
||||
export async function verifyAuth(request: FastifyRequest, reply: FastifyReply) {
|
||||
const token = request.cookies['authToken']
|
||||
|
||||
if (!token) throw Errors.UNAUTHORIZED
|
||||
|
||||
try {
|
||||
const payload = await verifyAuthToken(request.server, request.server.prisma, token)
|
||||
request.user = payload
|
||||
} catch {
|
||||
throw Errors.UNAUTHORIZED
|
||||
}
|
||||
}
|
||||
7
src/types/fastify.d.ts
vendored
7
src/types/fastify.d.ts
vendored
@@ -4,4 +4,11 @@ declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
prisma: PrismaClient
|
||||
}
|
||||
|
||||
interface FastifyRequest {
|
||||
user: {
|
||||
userId: string
|
||||
tokenVersion: number
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user