0

I've set up a simple symfony/skeleton application that contains user entity and authentication. When after successful authentication I call a route that contains the following code:

public function index(Security $security)
{
   $username = $security->getUser()->getUsername();

This results in an error:

Call to a member function getUsername() on null

So obviously the security component does not now about the currently logged in user. The session however does contain the current UsernamePasswordToken (I checked using dump).

The same code works fine when I use website-skeleton. What is missing in skeleton?

My firewall is setup like that:

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: true
        logout:
            path: ^/user/logout
        json_login:
            check_path: /user/login
BenjaminH
  • 324
  • 3
  • 13
  • Well for one thing the skeleton does not even include the security bundle. Of course you must have loaded it otherwise Security would not be available. I'm not familiar with json_login. I assume you are following: https://symfony.com/doc/current/security/json_login_setup.html ? Did you define your own User object? – Cerad Feb 20 '19 at 12:49
  • Security bundle is installed, created the user using the maker bundle. I followed the json_login setup you mentioned. – BenjaminH Feb 20 '19 at 13:19
  • I'd have to setup a test case. Sometimes I run into problems with the [EquatableInterface](https://stackoverflow.com/questions/50584730/no-error-and-no-user-in-session-after-login-form-is-validated-in-symfony-4/50586510#50586510). You might also check the website-skeleton version of security.yaml. If you are willing to post your complete skeleton app to github I'd be willing to take a look. – Cerad Feb 20 '19 at 13:29
  • I've compared both `security.yaml` files. They are identical. – BenjaminH Feb 20 '19 at 13:30
  • I'm going to setup a minimal project and upload it to github. – BenjaminH Feb 20 '19 at 13:30
  • Ok, building a minimal project lead to success. So one of my dependencies must somehow introduce problems. I'm using code generated by the [openapi-generator](https://github.com/OpenAPITools/openapi-generator), I'll look into that component. – BenjaminH Feb 20 '19 at 17:35

1 Answers1

0

You can get the authenticated user from a controller that extends AbstractController just by using $this->getUser();.

titili
  • 207
  • 1
  • 6
  • Okay, what about `$security->getToken()->getUser();` ? – titili Feb 20 '19 at 11:15
  • this returns the string `anon.` – BenjaminH Feb 20 '19 at 13:22
  • So, Symfony consider that you're authenticate as anonymous, and that's why you can't retrieve an User. Have you followed the [Symfony tutorial](https://symfony.com/doc/master/security/json_login_setup.html) about `json_login` ? Don't know if you have already setup one but maybe missing an [User provider](https://symfony.com/doc/master/security/user_provider.html) ? – titili Feb 20 '19 at 13:47