Commit 6178a968 authored by Taylor Otwell's avatar Taylor Otwell

Merge pull request #110 from ericbarnes/feature/remember_me

Feature/remember me
parents b7b5451a 6e0d69e2
......@@ -52,7 +52,19 @@ class Auth {
$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 @@ class Auth {
*
* @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 @@ class Auth {
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);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment