From 3c7802c5ccc01dbd55cfecfd8b72d63a909bf370 Mon Sep 17 00:00:00 2001 From: Mohamed Akram Date: Sun, 18 Feb 2024 15:50:44 +0400 Subject: [PATCH] fix: auth route handlers not ending correctly --- src/authentication/login.handler.ts | 8 ++++---- src/authentication/logout.handler.ts | 9 +++------ src/authentication/protected-routes.handler.ts | 4 ++-- src/authentication/refresh.handler.ts | 5 ++--- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/authentication/login.handler.ts b/src/authentication/login.handler.ts index 04cb81d..e4014e1 100644 --- a/src/authentication/login.handler.ts +++ b/src/authentication/login.handler.ts @@ -30,7 +30,7 @@ export const withLogin = ( ...providerProps, }); reply.type('text/html'); - reply.send(login); + return reply.send(login); }); fastifyInstance.post(loginPath, async (req, reply) => { @@ -60,9 +60,9 @@ export const withLogin = ( req.session.set('adminUser', adminUser); if (req.session.redirectTo) { - reply.redirect(302, req.session.redirectTo); + return reply.redirect(302, req.session.redirectTo); } else { - reply.redirect(302, rootPath); + return reply.redirect(302, rootPath); } } else { const login = await admin.renderLogin({ @@ -71,7 +71,7 @@ export const withLogin = ( ...providerProps, }); reply.type('text/html'); - reply.send(login); + return reply.send(login); } }); }; diff --git a/src/authentication/logout.handler.ts b/src/authentication/logout.handler.ts index 02461a7..afb03cd 100644 --- a/src/authentication/logout.handler.ts +++ b/src/authentication/logout.handler.ts @@ -20,13 +20,10 @@ export const withLogout = ( if (provider) { await provider.handleLogout({ request, reply }); } - + if (request.session) { - request.session.destroy(() => { - reply.redirect(admin.options.loginPath); - }) - } else { - reply.redirect(admin.options.loginPath); + await request.session.destroy(); } + return reply.redirect(admin.options.loginPath); }); }; diff --git a/src/authentication/protected-routes.handler.ts b/src/authentication/protected-routes.handler.ts index 1c85b26..71f42b7 100644 --- a/src/authentication/protected-routes.handler.ts +++ b/src/authentication/protected-routes.handler.ts @@ -7,7 +7,7 @@ export const withProtectedRoutesHandler = ( ): void => { const { rootPath } = admin.options; - fastifyApp.addHook('preHandler', async (request, reply) => { + fastifyApp.addHook('preHandler', async (request, reply) => { const buildComponentRoute = AdminRouter.routes.find((r) => r.action === 'bundleComponents')?.path if (AdminRouter.assets.find((asset) => request.url.match(asset.path))) { return; @@ -28,7 +28,7 @@ export const withProtectedRoutesHandler = ( ? rootPath : redirectTo; - reply.redirect(admin.options.loginPath); + return reply.redirect(admin.options.loginPath); } }); }; diff --git a/src/authentication/refresh.handler.ts b/src/authentication/refresh.handler.ts index b360593..f528def 100644 --- a/src/authentication/refresh.handler.ts +++ b/src/authentication/refresh.handler.ts @@ -54,8 +54,7 @@ export const withRefresh = ( }; request.session.set('adminUser', admin); - request.session.save(() => { - reply.send(admin); - }); + await request.session.save(); + return reply.send(admin); }); };