hwi / HWIOAuthBundle

OAuth client integration for Symfony. Supports both OAuth1.0a and OAuth2.
MIT License
2.26k stars 793 forks source link

Failure handler not overriding #1999

Open aynaitlamine opened 1 month ago

aynaitlamine commented 1 month ago
Q A
Bug? yes
New Feature? no
Support question? no yes
Version 2.x

Actual Behavior

What is the actual behavior? Upon encountering an issue, the system currently redirects users to '/' without displaying any error message.

Expected Behavior

What is the behavior you expect? The expected behavior is to display an error message in JSON format when using Lexik. Specifically, while the success_handler (lexik_jwt_authentication.handler.authentication_success) functions correctly, the failure_handler (lexik_jwt_authentication.handler.authentication_failure) fails to override the default behavior.

heather817 commented 1 month ago

This is also causing me problems - when the authentication dies for any reason, there are no errors logged or sent to the user, which was not the behavior with previous versions.

heather817 commented 1 month ago

I did find that you can listen to the LoginFailureEvent to implement what this bundle used to do:

<?php

namespace App\EventListener;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Security\Http\Event\LoginFailureEvent;
use Twig\Environment;

final class LoginFailureListener
{
    private Environment $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    #[AsEventListener(event: LoginFailureEvent::class)]
    public function onLoginFailureEvent(LoginFailureEvent $event): void
    {
        $exception = $event->getException();
        $message = sprintf(
            'An Authentication Error: %s with code: %s',
            $exception->getMessage(),
            $exception->getCode()
        );

        // Customize your response object to display the exception details
        $response = new Response(
            $this->twig->render('@HWIOAuth/Connect/login.html.twig', ['error' => $message])
        );

        // sends the modified response object to the event
        $event->setResponse($response);
    }
}