From 4f01f31f04da12b6169816175d2ab221fc6fb53f Mon Sep 17 00:00:00 2001 From: adrien2p Date: Tue, 22 Nov 2022 10:03:45 +0100 Subject: [PATCH] fix: update logout handler accordingly to the previous changes --- packages/medusa-plugin-auth/src/api/index.ts | 37 ++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/packages/medusa-plugin-auth/src/api/index.ts b/packages/medusa-plugin-auth/src/api/index.ts index 39c5482..fb92b33 100644 --- a/packages/medusa-plugin-auth/src/api/index.ts +++ b/packages/medusa-plugin-auth/src/api/index.ts @@ -4,7 +4,7 @@ import wrapHandler from '@medusajs/medusa/dist/api/middlewares/await-middleware' import loadConfig from '@medusajs/medusa/dist/loaders/config'; import cors from 'cors'; -import { AUTH_TOKEN_COOKIE_NAME, AuthOptions } from '../types'; +import { ADMIN_AUTH_TOKEN_COOKIE_NAME, AuthOptions, STORE_AUTH_TOKEN_COOKIE_NAME } from '../types'; import { loadJwtOverrideStrategy } from '../auth-strategies/jwt-override'; import { getGoogleRoutes } from '../auth-strategies/google'; import { getFacebookRoutes } from '../auth-strategies/facebook'; @@ -34,24 +34,22 @@ function loadRouters(configModule: ConfigModule, options: AuthOptions): Router[] function getLogoutRouter(configModule: ConfigModule): Router { const router = Router(); - const logoutHandler = async (req, res) => { - if (req.session) { - req.session.jwt = {}; - req.session.destroy(); - } - - res.clearCookie(AUTH_TOKEN_COOKIE_NAME); - - res.status(200).json({}); - }; - const adminCorsOptions = { origin: configModule.projectConfig.admin_cors.split(','), credentials: true, }; router.use('/admin/auth', cors(adminCorsOptions)); - router.delete('/admin/auth', wrapHandler(logoutHandler)); + router.delete('/admin/auth', wrapHandler(async (req, res) => { + if ((req as unknown as Request & { session: any }).session) { + (req as unknown as Request & { session: any }).session.jwt = {}; + (req as unknown as Request & { session: any }).session.destroy(); + } + + res.clearCookie(ADMIN_AUTH_TOKEN_COOKIE_NAME); + + res.status(200).json({}); + })); const storeCorsOptions = { origin: configModule.projectConfig.store_cors.split(','), @@ -59,7 +57,18 @@ function getLogoutRouter(configModule: ConfigModule): Router { }; router.use('/store/auth', cors(storeCorsOptions)); - router.delete('/store/auth', wrapHandler(logoutHandler)); + router.delete('/store/auth', wrapHandler(async (req, res) => { + if ((req as unknown as Request & { session: any }).session) { + (req as unknown as Request & { session: any }).session.jwt = {}; + // The bellow line will be available in the next version of medusa core + /*(req as unknown as Request & { session: any }).session.jwt_store = {};*/ + (req as unknown as Request & { session: any }).session.destroy(); + } + + res.clearCookie(STORE_AUTH_TOKEN_COOKIE_NAME); + + res.status(200).json({}); + })); return router; }