Skip to content

Commit

Permalink
fix: Use lexik_jwt_authentication.on_authentication_success event t…
Browse files Browse the repository at this point in the history
…o update user last login date
  • Loading branch information
ambroisemaupate committed Sep 12, 2024
1 parent ae36c59 commit fbe532f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
3 changes: 0 additions & 3 deletions lib/RoadizCoreBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,6 @@ services:
Solarium\Core\Client\Client:
factory: ['RZ\Roadiz\CoreBundle\SearchEngine\ClientRegistry', 'getClient']

RZ\Roadiz\CoreBundle\Security\Authentication\JwtAuthenticationSuccessHandler:
decorates: 'lexik_jwt_authentication.handler.authentication_success'

RZ\Roadiz\CoreBundle\Security\Authorization\Chroot\NodeChrootResolver:
alias: RZ\Roadiz\CoreBundle\Security\Authorization\Chroot\NodeChrootChainResolver

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\EventSubscriber;

use Doctrine\Persistence\ManagerRegistry;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use RZ\Roadiz\CoreBundle\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final readonly class JwtAuthenticationSuccessEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private ManagerRegistry $managerRegistry,
) {
}

/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
'lexik_jwt_authentication.on_authentication_success' => 'onAuthenticationSuccess',
AuthenticationSuccessEvent::class => 'onAuthenticationSuccess',
];
}

public function onAuthenticationSuccess(AuthenticationSuccessEvent $event): void
{
$user = $event->getUser();
/*
* Check if user is a passwordless user
*/
if (!($user instanceof User)) {
return;
}

$user->setLastLogin(new \DateTime());
$this->managerRegistry->getManager()->flush();
}
}

This file was deleted.

0 comments on commit fbe532f

Please sign in to comment.