Merge pull request #110 from ericbarnes/feature/remember_me
Feature/remember me
This commit is contained in:
commit
6178a968ea
|
@ -52,7 +52,19 @@ public static function user()
|
|||
|
||||
$id = IoC::container()->core('session')->get(Auth::user_key);
|
||||
|
||||
return static::$user = call_user_func(Config::get('auth.user'), $id);
|
||||
static::$user = call_user_func(Config::get('auth.user'), $id);
|
||||
|
||||
if (is_null(static::$user) AND ! is_null($cookie = Crypter::decrypt(\Cookie::get('remember'))))
|
||||
{
|
||||
$cookie = explode('|', $cookie);
|
||||
if ($cookie[2] == md5(\Request::server('HTTP_USER_AGENT'))
|
||||
AND ! is_null(static::$user = call_user_func(Config::get('auth.user'), $cookie[0])))
|
||||
{
|
||||
static::login(static::$user);
|
||||
}
|
||||
}
|
||||
|
||||
return static::$user;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,14 +75,18 @@ public static function user()
|
|||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param bool $remember
|
||||
* @param int $ttl - Default is one week.
|
||||
* @return bool
|
||||
*/
|
||||
public static function attempt($username, $password = null)
|
||||
public static function attempt($username, $password = null, $remember = false, $ttl = 10080)
|
||||
{
|
||||
if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password)))
|
||||
{
|
||||
static::login($user);
|
||||
|
||||
if ($remember) static::remember($user);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -108,4 +124,17 @@ public static function logout()
|
|||
IoC::container()->core('session')->forget(Auth::user_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a cookie so that users are remembered.
|
||||
*
|
||||
* @param object $user
|
||||
* @param int $ttl - Default is one week.
|
||||
* @return bool
|
||||
*/
|
||||
public static function remember($user, $ttl = 10080)
|
||||
{
|
||||
static::$user = $user;
|
||||
$cookie = Crypter::encrypt(implode('|', array($user->id, \Request::ip(), md5(\Request::server('HTTP_USER_AGENT')), time())));
|
||||
\Cookie::put('remember', $cookie, $ttl);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue