Answer the question
In order to leave comments, you need to log in
What is the right way to use CustomAuthentifer in symfony 5.3.6 security and not break the test loginUser?
Hello!
The problem of obtaining information about an authorized user.
When I remove the main block in security.yaml , the $this->client->loginUser($testUser) test snippet works properly
and AccountController::getUser() returns User - as it should.
But if the main block is uncommented, then the target route returns null instead of the expected user =(((
More details below:
There is a security.yaml config :
security:
enable_authenticator_manager: true
encoders:
App\Model\User\Entity\User\User:
algorithm: bcrypt
providers:
app_user_provider:
entity:
class: App\Model\User\Entity\User\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
provider: app_user_provider
custom_authenticators:
- App\Security\LoginFormAuthenticator
access_control:
namespace App\Security;
class LoginFormAuthenticator extends AbstractAuthenticator {
...
class UserinfoTest extends DbWebTestCase
{
private const API_PREFIX = '/api/v1';
protected function setUp(): void
{
$this->client = static::createClient();
$this->client->disableReboot();
$this->em = static::getContainer()->get('doctrine')->getManager();
}
public function test_userinfo_success()
{
$userRepository = $this->em->getRepository(User::class);
$testUser = $userRepository->findOneBy(['email' => '[email protected]']);
$this->client->loginUser($testUser);
$this->client->request('GET', self::API_PREFIX . '/account/userinfo');
...
}
}
namespace App\Controller\Api\V1\Account;
#[Route('api/v1/account')]
class AccountController extends BaseController
{
#[Route('/userinfo')]
public function userInfo(): JsonResponse
{
$user = $this->getUser();
return $this->json([
'code' => 200,
'data' => [
'user' => [
'username' => (string)$user->getUsername(),
]
]
], 200, []);
}
protected function getUser(): User
{
return parent::getUser(); // !!!!!!!!! ПРОБЛЕМА ТУТ
}
}
namespace App\Model\User\Entity\User;
class User implements UserInterface, UserLoaderInterface, UserProviderInterface {
...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question