Skip to content

How to check if the user is logged in

Daniel Strøm edited this page Mar 22, 2015 · 18 revisions

Task

Check if the user is logged in (ie: user identity widget)

Solution

There are three ways.

View

ZfcUser provides a View Helper (zfcUserIdentity) which you can use from any view script in your application.

<!-- Test if the User is connected -->
<?php if(!$this->zfcUserIdentity()): ?>
    <!-- display the login form -->
    <?php echo $this->zfcUserLoginWidget(array('redirect'=>'application')); ?>
<?php else: ?>
    <!-- display the 'display name' of the user -->
    <?php echo $this->zfcUserIdentity()->getDisplayname(); ?>
<?php endif?>

You can also get user's fields (if the user is logged in), like email:

<?php echo $this->zfcUserIdentity()->getEmail(); ?>

Controller

ZfcUser provides a Controller Plugin (zfcUserAuthentication) which you can use from any controller in your application. You can check if the user is connected and get his data:

<?php
if ($this->zfcUserAuthentication()->hasIdentity()) {
    //get the email of the user
    echo $this->zfcUserAuthentication()->getIdentity()->getEmail();
    //get the user_id of the user
    echo $this->zfcUserAuthentication()->getIdentity()->getId();
    //get the username of the user
    echo $this->zfcUserAuthentication()->getIdentity()->getUsername();
    //get the display name of the user
    echo $this->zfcUserAuthentication()->getIdentity()->getDisplayname();
}
?>

You can get the zfcuser_auth_service in a controller by doing:

$authService = $this->zfcUserIdentity()->getAuthService();

Service Manager

<?php
$sm = $app->getServiceManager();
$auth = $sm->get('zfcuser_auth_service');
if ($auth->hasIdentity()) {
    echo $auth->getIdentity()->getEmail();
}
?>