diff --git a/back/src/__tests__/auth/logout.test.ts b/back/src/__tests__/auth/logout.test.ts new file mode 100644 index 000000000..638c873bf --- /dev/null +++ b/back/src/__tests__/auth/logout.test.ts @@ -0,0 +1,11 @@ +import supertest from 'supertest' +import { expect, test, describe } from 'vitest' +import { server } from '../globalSetup' +import { pathRoot } from '../../routes/routes' + +describe('Testing authentication endpoint', () => { + test('should succeed', async () => { + const response = await supertest(server).get(`${pathRoot.v1.auth}/logout`) + expect(response.status).toBe(204) + }) +}) diff --git a/back/src/controllers/auth/logoutController.ts b/back/src/controllers/auth/logoutController.ts new file mode 100644 index 000000000..5e556bf0d --- /dev/null +++ b/back/src/controllers/auth/logoutController.ts @@ -0,0 +1,6 @@ +import { Context } from 'koa' + +export const logoutController = async (ctx: Context) => { + ctx.cookies.set('token', null) + ctx.status = 204 +} diff --git a/back/src/controllers/index.ts b/back/src/controllers/index.ts index c6a2056c1..71aa6e30d 100644 --- a/back/src/controllers/index.ts +++ b/back/src/controllers/index.ts @@ -1,4 +1,5 @@ export { loginController } from './auth/loginController' +export { logoutController } from './auth/logoutController' export { registerController } from './auth/registerController' export { authMeController } from './auth/authMeController' export { createResource } from './resources/createResourceController' diff --git a/back/src/routes/authRouter.ts b/back/src/routes/authRouter.ts index fab0ab35a..f27b487dd 100644 --- a/back/src/routes/authRouter.ts +++ b/back/src/routes/authRouter.ts @@ -3,6 +3,7 @@ import { z } from 'zod' import { authMeController, loginController, + logoutController, registerController, } from '../controllers' import { validate, authMiddleware } from '../middleware' @@ -19,6 +20,8 @@ authRouter.post( loginController ) +authRouter.get('/logout', logoutController) + authRouter.post( '/register', validate(z.object({ body: userRegisterSchema })),