title: Verify if user is authenticated in symfony2
body: <p>I tried to create a simple login application in symfony2 and I don't understand a thing.
  My routing file :</p>
  
  <pre><code>shop_show_login_page:
  path: /login
  defaults: { _controller: ShopDesktopBundle:User:loginPage }
  
  shop_login_user:
  path: /loginUser
  defaults: { _controller: ShopDesktopBundle:User:loginCheck }
  </code></pre>
  
  <p>My controller with this 2 methods : </p>
  
  <pre><code>public function loginPageAction(){
      return $this-&gt;render('ShopDesktopBundle:User:loginPage.html.twig');
  }
  public function loginCheckAction(Request $request){
      $request = $this-&gt;get('request');
      $password = $request-&gt;request-&gt;get('password');
      $login    = $request-&gt;request-&gt;get('username');
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
      $repository = $em-&gt;getRepository('ShopDesktopBundle:Customer');
      $user = $repository-&gt;findOneBy(array('customer_login'=&gt; $login, 'customer_password'=&gt; $password));
      if($user){
          return $this-&gt;redirect($this-&gt;generateUrl('shop_desktop_homepage'));
      }else{
          return $this-&gt;render('ShopDesktopBundle:User:loginPage.html.twig',array('message_failed' =&gt; 'Error'));
      }
  }
  </code></pre>
  
  <p>My form in view : </p>
  
  <pre><code> &lt;form action="{{ path('shop_login_user') }}" method="post"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-user"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input type="text" class="form-control" placeholder="Username" name="username"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-lock"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input type="text" class="form-control" placeholder="Password" name="password"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;button type="submit" class="button"&gt;Autentificare&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>This system on login work fine but the problem is in the layout.html.twig:</p>
  
  <pre><code>{% if app.user %}
       &lt;p&gt;Test&lt;/p&gt;
  {% elseif not app.user %}
       &lt;span&gt;&lt;a href="{{ path('shop_show_login_page') }}" style="color: #ffffff;"&gt;Login / Register&lt;/a&gt;&lt;/span&gt;
  {% endif %}
  </code></pre>
  
  <p>My security.yml:</p>
  
  <pre><code>security:
  # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
  encoders:
      Symfony\Component\Security\Core\User\User: plaintext
  
  # http://symfony.com/doc/current/book/security.html#hierarchical-roles
  role_hierarchy:
      ROLE_ADMIN:       ROLE_USER
      ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  
  # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
  providers:
      in_memory:
          memory:
              users:
                  user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                  admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
  
  # the main part of the security, where you can set up firewalls
  # for specific sections of your app
  firewalls:
      # disables authentication for assets and the profiler, adapt it according to your needs
      secured_area:
          pattern:    ^/
          form_login:
              check_path: shop_login_user
              login_path: shop_show_login_page
          logout:
              path:   _demo_logout
              target: _demo
          #anonymous: ~
          #http_basic:
          #    realm: "Secured Demo Area"
  
  # with these settings you can restrict or allow access for different parts
  # of your application based on roles, ip, host or methods
  # http://symfony.com/doc/current/cookbook/security/access_control.html
  access_control:
      #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
  </code></pre>
  
  <p>So I want to see in the page "Test" if user is authentificated with succes and Login/Register if authentification failed. I tried with "if app.user" but I don't understand the principle.</p>