This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

app.user is NULL via FOSUserBundle, even when user is logged

Discuss Symfony 2 bundles here

app.user is NULL via FOSUserBundle, even when user is logged

by deb0rian » Fri Jan 16, 2015 2:49 pm

My app.user in Twig is NULL after logging into my application programatically, though Symfony's toolbar is showing the correct session and username.

Login code:

Code: Select all
  $email = $request->request->get('email');
    $user = $userManager->findUserByEmail($email);
    $token = new UsernamePasswordToken($user->getEmail(), $user->getPassword(), "main", $user->getRoles());
    $this->get("security.context")->setToken($token);

    // Fire the login event
    // Logging the user in above the way we do it doesn't do this automatically
    $event = new InteractiveLoginEvent($request, $token);
    $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

security.yml configuration of a firewall:

firewalls:
Code: Select all
# 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
            csrf_provider: form.csrf_provider
        logout: true
        anonymous: true

Although I specify "main" firewall for login, maybe it falls back to "dev" and fails to retrieve User instance properly?
deb0rian
Junior Member
 
Posts: 1
Joined: Fri Jan 16, 2015 2:47 pm