简体   繁体   中英

Symfony3 Guard Authentication - prevent InteractiveLoginEvent from being dispatched for API requests

I'm using the new Guard authentication for authenticating API requests. Diving into the Guard code, I see that it triggers a InteractiveLoginEvent once the authentication was successful. My application listens for that event to track user logins, so I don't necessarily want it to be triggered for API requests.

I was wondering whether there was a way to bypass the event being dispatched in Guard for API authentication? Or did I misunderstand the purpose of the InteractiveLoginEvent ?

I don't think you can (or necessarily should) stop the InteractiveLoginEvent from firing, but what about modifying your login listener to ignore API requests? If all your API endpoints are mounted under the /api URL root, you can do something like:

public function onLogin(InteractiveLoginEvent $event)
{
    // Don't process login event for API requests
    $request = $event->getRequest();
    if (strpos($request->getPathInfo(), '/api') === 0) {
        return;
    }

    // … rest of your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM