3

我正在构建一个系统,当用户第一次使用 Facebook 登录时,他没有提供密码。所以我尝试使用来自 facebook 的凭据让他登录。

Sentry::authenticate($credentials, false);  

上面的命令总是要求输入密码。如何在不要求他们输入密码的情况下登录用户?

4

1 回答 1

11

在 Sentry 中,您可以通过两种不同的方式登录:

1) 在您的登录表单上输入密码时,您告诉 Sentry 查找用户并检查密码以同时验证和登录他/她:

// Set login credentials
$credentials = array(
    'email'    => Input::get('email'),
    'password' => Input::get('password'),
);

// Try to authenticate the user
$user = Sentry::authenticate($credentials, false);

2)当用户通过其他方式进行身份验证时,如 OAuth,您只需要强制它登录到您的系统:

// Find the user using the user id or e-mail
$user = Sentry::findUserById($userId);

// or 

$user = Sentry::findUserByLogin($email);

// and

// Log the user in
Sentry::login($user, false);

您选择一个或另一个,您不必同时使用两者。

3) 作为第三个示例,假设您有一个旧用户数据库,并且您的密码使用 MD5 散列:

// Find the user

$user = Sentry::findUserByLogin($email);

// Check the password

if ($user->password !== md5(Input::get('password'))
{
    Redirect::back()->withMessage('Password is not correct.');
}

// And, if password is correct, force the login:

Sentry::login($user, false);
于 2013-12-18T19:51:15.237 回答