LexikJWTAuthenticationBundle trouble with token

17 Views Asked by At

hi i have a problem with the token because when i generate it my email is not delivered to the token data and when i insert this token into the header i get an error: "Warning: Array to string conversion"

I have no idea what I can do to fix it...

User Entity:

#[ORM\Entity(repositoryClass: AuthRepository::class)]
class User implements UserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: "CUSTOM")]
    #[ORM\Column(type: "guid", unique: true)]
    #[ORM\CustomIdGenerator(class: "Ramsey\Uuid\Doctrine\UuidGenerator")]
    private string $id ;

    #[ORM\Column(type: "first_name")]
    private FirstName $firstName;

    #[ORM\Column(type: "last_name")]
    private LastName $lastName;

    #[ORM\Column(type: "email", unique: true)]
    private Email $email;

    #[ORM\Column(type: "hashed_password")]
    private HashedPassword $hashedPassword;

    public static function create(SignUpCommand $credentials): self
    {
        $user = new self();
        $user->setFirstName($credentials->firstName);
        $user->setLastName($credentials->lastName);
        $user->setEmail($credentials->email);
        $user->setHashedPassword($credentials->hashedPassword);

        return $user;
    }



    public function verifyPassword(string $hashedPassword, string $plainPassword): bool
    {
        return HashedPassword::fromString($hashedPassword)->match($plainPassword);
    }

    public function getId(): ?string
    {
        return $this->id;
    }
    public function getEmail(): ?Email
    {
        return $this->email;
    }

    public function setFirstName(FirstName $firstName): void
    {
        $this->firstName = $firstName;
    }


    public function setLastName(LastName $lastName): void
    {
        $this->lastName = $lastName;
    }

    public function setEmail(Email $email): void
    {
        $this->email = $email;
    }

    public function setHashedPassword(HashedPassword $hashedPassword): void
    {
        $this->hashedPassword = $hashedPassword;
    }

    public function getPassword(): ?HashedPassword
    {
        return $this->hashedPassword;
    }

    public function getSalt(): ?string
    {
        // You can leave this method empty if you're using bcrypt or a modern hashing algorithm
        return null;
    }

    public function eraseCredentials()
    {
        // Implement this method if you store any sensitive data that should be cleared
        // For example, if you store plain-text passwords, you can clear them here


    }

    public function getRoles(): array
    {
        // Return an array of roles assigned to the user
        // If you don't have a concept of roles, you can return an empty array
        return ["ROLE_USER"];
    }

    public function getUserIdentifier(): string
    {
        // Return the unique identifier for the user (e.g., email, username)
        return $this->getEmail()->toString();
    }

}

for provide more idea about code example of Email ValueObject:

class Email implements \Stringable
{
    private function __construct(private readonly string $email)
    {
    }

    public static function fromString(string $email): self
    {
        Assertion::email($email, 'Not a valid email');

        return new self($email);
    }

    public function toString(): string
    {
        return $this->email;
    }

    public function __toString(): string
    {
        return $this->email;
    }
}

and code that generate token:

above i got user data from db and return token:
 
return  $this->jwtManager->create($user);

config secutiry.yaml:

providers:
        # users_in_memory: { memory: null }
        app_user_provider:
            entity:
               class: App\Domain\Entity\User
               property: email

    firewalls:       
        api:
            pattern:   ^/api
            stateless: true
            provider: app_user_provider
            jwt: ~
    
        dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false

            
        main:
            json_login:
                check_path: /api/sign-in    
                username_path: email
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
0

There are 0 best solutions below