Kaynağa Gözat

login data

master
Daniel 2 yıl önce
ebeveyn
işleme
dbb032d66d
4 değiştirilmiş dosya ile 44 ekleme ve 93 silme
  1. +3
    -0
      README.md
  2. +5
    -0
      config/services.yaml
  3. +0
    -93
      src/Entity/ApiToken.php
  4. +36
    -0
      src/EventListener/AuthenticationSuccessListener.php

+ 3
- 0
README.md Dosyayı Görüntüle

@@ -32,6 +32,9 @@
# Ddev Commands:

ddev describe - zeigt Urls und installierte Komponenten

# Xdebug
ddev ssh -> export XDEBUG_CONFIG="idekey=PHPSTORM"
-------------------------
# Symfony:



+ 5
- 0
config/services.yaml Dosyayı Görüntüle

@@ -22,3 +22,8 @@ services:

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

acme_api.event.authentication_success_listener:
class: App\EventListener\AuthenticationSuccessListener
tags:
- { name: kernel.event_listener, event: lexik_jwt_authentication.on_authentication_success, method: onAuthenticationSuccessResponse }

+ 0
- 93
src/Entity/ApiToken.php Dosyayı Görüntüle

@@ -1,93 +0,0 @@
<?php

namespace App\Entity;

use App\Repository\ApiTokenRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ApiTokenRepository::class)]
class ApiToken
{
private const PERSONAL_ACCESS_TOKEN_PREFIX = 'spt_';

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\ManyToOne(inversedBy: 'apiTokens')]
#[ORM\JoinColumn(nullable: false)]
private ?User $ownedBy = null;

#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $expiresAt = null;

#[ORM\Column(length: 68)]
private string $token;

#[ORM\Column]
private array $scopes = [];

public function __construct(string $tokenType = self::PERSONAL_ACCESS_TOKEN_PREFIX)
{
$this->token = $tokenType.bin2hex(random_bytes(32));
}

public function getId(): ?int
{
return $this->id;
}

public function getOwnedBy(): ?User
{
return $this->ownedBy;
}

public function setOwnedBy(?User $ownedBy): self
{
$this->ownedBy = $ownedBy;

return $this;
}

public function getExpiresAt(): ?\DateTimeImmutable
{
return $this->expiresAt;
}

public function setExpiresAt(?\DateTimeImmutable $expiresAt): self
{
$this->expiresAt = $expiresAt;

return $this;
}

public function getToken(): ?string
{
return $this->token;
}

public function setToken(string $token): self
{
$this->token = $token;

return $this;
}

public function getScopes(): array
{
return $this->scopes;
}

public function setScopes(array $scopes): self
{
$this->scopes = $scopes;

return $this;
}

public function isValid(): bool
{
return $this->expiresAt === null || $this->expiresAt > new \DateTimeImmutable();
}
}

+ 36
- 0
src/EventListener/AuthenticationSuccessListener.php Dosyayı Görüntüle

@@ -0,0 +1,36 @@
<?php
/**
* @author Daniel Knudsen <d.knudsen@spawntree.de>
* @date 18.01.24
*/


namespace App\EventListener;


use App\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Core\User\UserInterface;

class AuthenticationSuccessListener
{
/**
* @param AuthenticationSuccessEvent $event
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event): void
{
$data = $event->getData();
$user = $event->getUser();

if (!$user instanceof User) {
return;
}

$data['email'] = $user->getEmail();
$data['firstName'] = $user->getFirstName();
$data['lastName'] = $user->getLastName();
$data['roles'] = $user->getRoles();

$event->setData($data);
}
}

Yükleniyor…
İptal
Kaydet