Custom success_handler / error_handler symfony 6

88 views Asked by At

I m newbies on Symfony (I've been started for a month), I want to do Login/Register System with JWT and I use library LexikJWTAuthenticationBundle. My Problem, I want to customize the login_check response and I made my custom success listeners like here,

and I'm see a error like this when I try to run :

<!-- Argument 1 passed to Symfony\Component\Security\Http\Authentication\CustomAuthenticationSuccessHandler::__construct() must implement interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface, instance of App\Listeners\AuthenticationSuccessListener given, called in /home/isha/work/coding/backend/api-colis/api-colis/var/cache/dev/ContainerA3xno95/getSecurity_Authenticator_JsonLogin_LoginService.php on line 48 (500 Internal Server Error) -->


Argument 1 passed to Symfony\Component\Security\Http\Authentication\CustomAuthenticationSuccessHandler::__construct() must implement interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface, instance of App\Listeners\AuthenticationSuccessListener given, called in /home/isha/work/coding/backend/api-colis/api-colis/var/cache/dev/ContainerA3xno95/getSecurity_Authenticator_JsonLogin_LoginService.php on line 48

error_500_symfony

I need help for, the custom success handler, custom response from Lexik JWT and for this problem. Thank You, have a nice day

I write my custom success_handler

<?php
// file : src/Listeners/AuthenticationSuccessListener.php

namespace App\Listeners;

use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;

class AuthenticationSuccessListener{

    public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event){
       
        $event->setData([
            'status' => $event->getResponse()->getStatusCode(),
            'data' => $event->getData(),
        ]);
    }
}

and register on a service.yaml like this :

# Custom listener success login
    app.listener.authentication_success_response:
        class:  App\Listeners\AuthenticationSuccessListener
        tags:
            -   { name: kernel.event_listener, event: lexik_jwt_authentication.handler.authentication_success, method: onAuthenticationSuccessResponse }   

and on a security.yaml :

json_login:
                check_path: /api/v1/login_check
                username_path: username
                password_path: password
                # success_handler: lexik_jwt_authentication.handler.authentication_success
                success_handler: app.listener.authentication_success_response
1

There are 1 answers

0
0xIsha On

Thanks for everyone, I found a solution. I rewrite my SuccessHandler like this :

<?php

namespace App\Listeners;

use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;

class AuthenticationSuccessListener extends AuthenticationSuccessHandler
{
    public function handleAuthenticationSuccess(UserInterface $user, $jwt = null)
    {
        $response = parent::handleAuthenticationSuccess($user, $jwt);

        $newData = [
            'id' => $user->getId(),
            'username' => $user->getUsername(),
            'is_active' => $user->getIsActive(),
            'access_token'=>  json_decode($response->getContent(),true)['token'],
        ];
        $response->setData($newData);

        return new JsonResponse([
            'status'=> $response->getStatusCode(),
            'message'=> 'LOGIN SUCCESS',
            'data'=> json_decode($response->getContent(),true)
        ]);
    }
}

and got response like this :

    {
    "status": 200,
    "message": "LOGIN SUCCESS",
    "data": {
        "id": 1,
        "username": "USER_AGENT_1",
        "is_active": 1,
        "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE3MDg0MzQxMTIsImV4cCI6MTcwODQzNzcxMiwicm9sZXMiOlsiUk9MRV9VU0VSIl0sInVzZXJuYW1lIjoiV0FEQSJ9.iG02AF775eYCoN7xPYdhIN9krlXume6b-YkTyCm_uxIxZ0gK84BCecCFtldANcdF76mpSyvd-o9xLZ5yc4fng-h2vVizHMmBR8DB1X1WLS8BAVkyp9dtYgzMADFns3hnlYlv2Op_hBrJaAg5KePXv9jbYr4bxTIDq2vpQBWlqbXvjTEW2vr90-CYcB0fN4CdN2zN7d5q4iPiS1mw4EWrwP2s88sGRgRbRCiUYkOh9VHYt-xUUT7y-UMRtMWCTNvZxvFKPc3u2MhpGiCeQcgFo5Ux1n_zAb0FhlHcjYjjqYkj3YGKr2E-niFJpsOvXt47TBFaENFDv-u3olI7LuF-5V4TubSWU1d6hzL1w2tsCF_9xR0xfPDJYEK3i3MRne27vS6xmePXtUrgq6oYw7v6rLa8dGgM4SZpPsyB6uqEwXv4hvpPVmy4eyUGrWXQ4kKt2pbOg13AB8weZJl_35cKU0ATMaRV2hc-62BzOxL_ZgpmwbT508817oM35EaAXXQz7EXICuMKdKgg8gETeeRDS2gcru-C_KVAIbJVtjtMUQtjRiInVy7QQBoXx-ZCgV4O39hmEoxGiX7ggwZE68gVHG_bPnURgz4FH4xY3APvoq5iWTjYW2hV4CC-9KTqztA4j9lsnoYEN9G_JQJw3X6ygJk-E9pv41-g1rXvvrNocy4"
    }
}