1
votes

There are many code snippets how to check if authenticated user is authorized to request a resource. But i cannot find a example to check if the user is authenticated at all.

I've got a kernel request listener, in which i'm trying to check if a user is authenticated, using this code:

if (TRUE === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) 
{
    //doing stuff
}

I get the following error message:

AuthenticationCredentialsNotFoundException in classes.php line 5307:
The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.

I guess, this is because the user is not logged in at all, and therefore doesn't have any authentication token.

So my question is: how do i, in a symfony controller, check if a user is authenticated before i check if he is authorized?

2
Are you sure that you need to check for it manually? Symfony Security can do it for you by denying access to certain routes for unauthorized users. - svgrafov

2 Answers

0
votes

You can check it like that :

$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
0
votes

Referring to the official documentation, you can use IS_AUTHENTICATED_FULLY or IS_AUTHENTICATED_REMEMBERED:

if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
    throw $this->createAccessDeniedException();
}

// do stuff