I use PHP and Silex to build a web app and I implemented basic authentication via SecurityServiceProvider
like this:
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'private' => array(
'remember_me' => array(
'key' => $config['secret_key'],
'lifetime' => $config['remember_me_duration'],
),
'pattern' => '^/admin',
'form' => array('login_path' => '/login', 'check_path' => '/admin/login_check'),
'logout' => array('logout_path' => '/admin/logout'),
'users' => $app->share(function () use ($app) {
// ...
}),
),
'public' => array(
'pattern' => '^/$',
'anonymous' => true,
),
'login' => array(
'pattern' => '^/login$',
'anonymous' => true,
),
),
));
As you can see the /admin
paths are secured, there I can use $app['security']->getToken()->getUser();
to get the actual user that is authenticated and display something like Logged in as $username, but if I do this on the /login
or /
routes the user will always be anon.
even if I'm authenticated.
How can I get the authenticated user, if there is one, otherwise the anon.
string, on the /
and /login
routes in order to display a message like: Logged in as $username ?
I also tried to use is_granted('IS_AUTHENTICATED_FULLY'
function in the twig templates to check if the user is authenticated, but on /login
and /
it returns false (even if I'm authenticated) since the anonymous user takes precedence over the authenticated ones, so no success here.