Good afternoon everyone. I’m using discord oauth2, after successful login the user appears. But after reloading the page it is deleted.
Logs:
NOTICE: PHP message: [info] Matched route "auth_discord_login".
NOTICE: PHP message: [debug] Checking for authenticator support.
NOTICE: PHP message: [debug] Checking support on authenticator.
NOTICE: PHP message: [debug] Checking support on authenticator.
NOTICE: PHP message: [debug] Remember-me cookie detected.
NOTICE: PHP message: [debug] Read existing security token from the session.
...
NOTICE: PHP message: [warning] Username could not be found in the selected user provider.
NOTICE: PHP message: [debug] Token was deauthenticated after trying to refresh it.
NOTICE: PHP message: [debug] Authenticator does not support the request.
NOTICE: PHP message: [debug] Clearing remember-me cookie.
NOTICE: PHP message: [info] Authenticator failed.
NOTICE: PHP message: [debug] Clearing remember-me cookie.
My security config:
security:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
users:
entity:
class: 'AppUserDomainUser'
property: 'username'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
remember_me:
secret: '%kernel.secret%'
always_remember_me: true
lifetime: 2592000 # 1 month in seconds
path: / # cookie path
signature_properties: ['username']
token_provider:
doctrine: true
lazy: true
provider: users
custom_authenticators:
- AppUserApplicationOauth2DiscordAuthenticatorService
logout:
path: auth_discord_app_logout
target: app_index
And auth service and controller code:
https://gist.github.com/LevPrav999/c2abad5f0d62d9cb8a160a40eb55b0a6
Debug bar:
Before refresh:
user id is shown
After refresh:
N/A is shown
My User.php class:
#[ORMEntity(repositoryClass: BotRepositoryInterface::class)]
#[ORMTable(name: 'users')]
#[ORMHasLifecycleCallbacks]
class User implements UserInterface
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id;
#[ORMColumn(type: 'string', length: 255)]
private ?string $username;
#[ORMColumn(type: 'string', length: 250)]
#[AssertConstraintsNotBlank]
private ?string $description = "";
#[ORMColumn(type: 'json')]
#[AssertConstraintsNotBlank]
private array $roles = [];
/**
* @param string|null $discordId
*/
public function __construct(?string $discordId)
{
$this->username = $discordId;
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): void
{
$this->id = $id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(?string $discordId): void
{
$this->username = $discordId;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): void
{
$this->description = $description;
}
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
public function getUserIdentifier(): string
{
return $this->username;
}
/** @see Serializable::serialize() */
public function serialize(): string
{
return serialize(array(
$this->id,
$this->username,
$this->description,
$this->roles
));
}
/** @see Serializable::unserialize() */
public function unserialize(string $serialized): void
{
list (
$this->id,
$this->username,
$this->description,
$this->roles
) = unserialize($serialized);
}
}
What i need to do to fix this error?
I tried changing the User class by adding serialization