Skip to content

Commit

Permalink
fix: update logout handler accordingly to the previous changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Nov 22, 2022
1 parent 80363de commit 4f01f31
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions packages/medusa-plugin-auth/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -34,32 +34,41 @@ 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(','),
credentials: true,
};

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;
}

0 comments on commit 4f01f31

Please sign in to comment.