user3440145
user3440145

Reputation: 843

Symfony FOS user bundle - Switch User failed: "Username "*" does not exist."

It really is what the headline states - I can't figure out why user switching isn't working. In my security.yml I have:

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        pattern: ^/
        form_login:
            provider:                       fos_userbundle                  # using FOSUserBundle for user authentication
            check_path:                     fos_user_security_check
            login_path:                     fos_user_security_login
            csrf_token_generator:           security.csrf.token_manager     # CSRF token - can be changed?
            default_target_path:            default_logged_in_target        # default route to go to after login
#                default_target_path:            fos_user_profile_show
            always_use_default_target_path: true                            # ignore the requested url and allways go to default route after login
        logout:
              path:                         fos_user_security_logout
              target:                       default_loged_out_target

        logout:       true
        anonymous:    true
        switch_user:  true

Calling the URL

[ProjectPath]/account?_switch_user=testuser

results in the error

Switch User failed: "Username "testuser" does not exist."

"testuser" however is a perfectly valid user and I can log in normaly with that user when logging in the standard way. The user I am logged in with when calling the URL with "_switch_user" does have the role "ROLE_ALLOWED_TO_SWITCH" - however this doesn't seem to be the problem. I am stuck here. Any hints are highly appreciated.

EDIT:

The stacktrace shows that in "SwitchUserListener.php" the call to

$this->tokenStorage->setToken($this->attemptSwitchUser($request));

fails and is catched resulting in the given error.

Upvotes: 0

Views: 1983

Answers (2)

roelleor
roelleor

Reputation: 443

Ok, I made a stupid mistake, but perhaps someone else did as well. I use email for authenticating, but used the username for switching the user. Make sure you use the same property. So in my case I needed to use the email: [email protected]

Upvotes: 1

user3440145
user3440145

Reputation: 843

I could get it to work adding the fos_userbundle user provider to security.yml like this:

firewalls:
..
    main:
        ...
        switch_user:
            provider:   fos_userbundle

Upvotes: 3

Related Questions