0
votes

I one action controller of my project (Sf 2.7.3), I autologin a user like that:

$providerKey = 'user_provider';
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->get('security.token_storage')->setToken($token);

$loginevent = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $loginevent);

Thats it's seems that works fine. In the Symfony toolbar appears:

Logged in as [email protected]

Authenticated No

Token class UsernamePasswordToken

My security.yml:

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false
    
    frontend:
        pattern:    ^/
        anonymous: ~
        http_basic: ~
        provider: user_provider
        form_login:
            login_path: user_login
            check_path: user_login_check
            use_referer: true
        logout:
            path:   user_logout
            target: /
        remember_me:
            key:      *****
            lifetime: 31536000


access_control:
    - { path: ^/private-zone/login,         roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/private-zone/login_check,   roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/private-zone/register,      roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/private-zone*,              roles: [ROLE_USER, ROLE_ADMIN] }
    
providers:
    user_provider:
        entity:
            class:        X\AccountBundle\Entity\User
            property:     email
            manager_name: ~
    in_memory:
        memory: ~
encoders:
    X\AccountBundle\Entity\User: { algorithm: sha512, iterations: 10 }

But if I go to the /private-zone path, it's redirecting to /login (infinitely).

If I login with the form login, everything works fine, and the toolbar info is the same. Anyone understant the problem?

2
Are you inside a controller or what? - DonCallisto
I would suggest to comment out use_referrer just to make a try and let me know - DonCallisto
@DonCallisto yes, inside a controller. - Carlos Vázquez
@DonCallisto the same happens - Carlos Vázquez

2 Answers

1
votes

According to this, the $providerKey argument of UsernamePasswordToken is the name of the firewall, so change $providerKey to 'frontend'.

0
votes

According to your toolbar, you are not authenticated. This might happen when you provide no roles to a user/usertoken. There is a difference between having a token, and having an authenticated token.

This might be the reason your private-zone login fails: it needs the user or admin role, or an anoymous token (or higher) that is authenticated.

Make sure that '$user->getroles();' returns at least one role, and see if a) authenticated is yes on the toolbar and b) fixes your login issue private-zone