Skip to content

Commit

Permalink
Implemented Redirection with auth middleware, and fixed exception han…
Browse files Browse the repository at this point in the history
…dler
  • Loading branch information
DarkBrines committed Nov 14, 2024
1 parent 975d6cd commit 309afc3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/Controllers/Http/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion app/Exceptions/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 309afc3

Please sign in to comment.