route et logique du logout
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { FastifyInstance } from 'fastify'
|
||||
import { RegisterSchema, LoginSchema } from '../schemas/auth.schema.js'
|
||||
import { registerUser, loginUser } from '../services/auth.service.js'
|
||||
import { registerUser, loginUser, logoutUser } from '../services/auth.service.js'
|
||||
import { signAuthToken } from '../services/authToken.service.js'
|
||||
import { verifyAuth } from '../middleware/verifyAuth.js'
|
||||
|
||||
export default async function authRoutes(fastify: FastifyInstance) {
|
||||
fastify.post('/auth/register', async (request, reply) => {
|
||||
@@ -40,4 +41,10 @@ export default async function authRoutes(fastify: FastifyInstance) {
|
||||
|
||||
return reply.status(200).send({ user })
|
||||
})
|
||||
|
||||
fastify.post('/auth/logout', { preHandler: verifyAuth }, async (request, reply) => {
|
||||
await logoutUser(fastify.prisma, request.user.userId)
|
||||
reply.clearCookie('authToken', { path: '/' })
|
||||
return reply.status(200).send({ message: 'Déconnecté avec succès' })
|
||||
})
|
||||
}
|
||||
@@ -68,4 +68,11 @@ export async function loginUser(prisma: PrismaClient, input: LoginInput) {
|
||||
tokenVersion: user.tokenVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export async function logoutUser(prisma: PrismaClient, userId: string) {
|
||||
await prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: { tokenVersion: { increment: 1 } },
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user