Silex: the token store does not contain an authentication token

When trying to check if the user is authenticated or not in the layout

{% if is_granted('IS_AUTHENTICATED_FULLY') %} <p>Username: {{ app.user.username }}</p> {% endif %} 

I get an error like

 Twig_Error_Runtime in Template.php line 304: An exception has been thrown during the rendering of a template ("The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.") in "layout.html" at line 39. 

This is a security firewall configuration. I only need to allow users to enter the website.

 $app->register(new SecurityServiceProvider(), array( 'security.firewalls' => array( 'dev' => array( 'pattern' => '^/(_(profiler|wdt)|css|images|js)/', 'security' => false ), 'login' => array( 'pattern' => '^/login$', ), 'secured' => array( 'pattern' => '^.*$', 'form' => array('login_path' => '/login', 'check_path' => '/login_check'), 'logout' => array('logout_path' => '/logout'), 'users' => $app->share(function() use ($app) { // Specific class App\User\UserProvider is described below return new App\User\UserProvider($app['db']); }), ), 'unsecured' => array( 'anonymous' => true, ) ), 'security.access_rules' => array( // You can rename ROLE_USER as you wish array('^/.+$', 'ROLE_USER'), array('^/login$', 'SS'), // This url is available as anonymous user ) )); 

Any ideas for fixing this are welcome.

thanks

+6
source share
1 answer

Since the error message says that the error occurred in layout.html , I assume that it is used on every page, even such as / login, which is not behind the firewall. The error is caused by calling is_granted , if not behind the firewall.

So there are several options:

  • Use a separate layout for the login page that does not call is_granted
  • Check if an existing security token exists before calling is_granted

Option 1 should be obvious, so without going into details with this.

With option 2, you can do something like this to check for an existing security token:

 {% if app.security.token is not null and is_granted('IS_AUTHENTICATED_FULLY') %} 
+9
source

Source: https://habr.com/ru/post/981034/


All Articles