From 309afc3372236e7c6d109e61d7e4e57f2934939b Mon Sep 17 00:00:00 2001 From: Raphael Roumezin Date: Thu, 14 Nov 2024 20:58:41 +0100 Subject: [PATCH] Implemented Redirection with auth middleware, and fixed exception handler --- app/Controllers/Http/UsersController.ts | 4 +++- app/Exceptions/Handler.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Controllers/Http/UsersController.ts b/app/Controllers/Http/UsersController.ts index b2c866d..83ca768 100644 --- a/app/Controllers/Http/UsersController.ts +++ b/app/Controllers/Http/UsersController.ts @@ -25,10 +25,12 @@ export default class UsersController { const login = request.input('login') const password = request.input('password') + const redirectTo = request.input('next', '/') + try { const user = await auth.verifyCredentials(login, password) await auth.login(user) - response.redirect().toPath(request.input('redirect', '/')) + response.redirect().toPath(redirectTo) } catch { session.flash({ error: 'Invalid credentials' }) response.redirect().toRoute('auth.login') diff --git a/app/Exceptions/Handler.ts b/app/Exceptions/Handler.ts index 706ce9b..b1465b8 100644 --- a/app/Exceptions/Handler.ts +++ b/app/Exceptions/Handler.ts @@ -33,7 +33,9 @@ export default class ExceptionHandler extends HttpExceptionHandler { ctx.session.flash({ error: ctx.response.getStatus() }) if (error instanceof AuthenticationException) { - ctx.response.redirect().toPath(error.redirectTo) + return ctx.response.redirect().toPath(error.redirectTo) } + + return ctx.response.redirect().back() } }