Skip to content

Instantly share code, notes, and snippets.

@eypsilon
Last active January 31, 2022 04:22
Show Gist options
  • Select an option

  • Save eypsilon/8f3a5934e54367b0f4fdbcab72210c04 to your computer and use it in GitHub Desktop.

Select an option

Save eypsilon/8f3a5934e54367b0f4fdbcab72210c04 to your computer and use it in GitHub Desktop.
Alternate Auto Logout for PHP-Auth (https://github.com/delight-im/PHP-Auth)
<?php declare(strict_types=1);
// namespace Many\Authentication\Utils;
use Delight\Auth\Auth;
/**
* Auto Logout for PHP-Auth (https://github.com/delight-im/PHP-Auth)
*
* @package ManyAuthentication
* @author Engin Ypsilon <engin.ypsilon@gmail.com>
*/
class AutoLogout
{
/** @var String session field for users last action time */
const SESSION_FIELD_LAST_ACTION = 'auth_last_action';
/**
* Logs the user automatically out after X seconds
*
* @param Object $auth instanceOf Delight\Auth\Auth
* @param Int $seconds max lifetime in seconds
* @return Bool returns true, if the session is expired
*/
public function watch(Auth $auth, Int $seconds): Bool
{
if ($auth->check())
if ($l = ($_SESSION[self::SESSION_FIELD_LAST_ACTION] ?? false) AND \time() > $l+$seconds) {
$auth->logOut();
return true;
} else $_SESSION[self::SESSION_FIELD_LAST_ACTION] = \time();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment