我试图允许用户使用Facebook登录,但我的用户管理是基于哨兵的如您所知,如果您从Facebook连接,除非您正常创建帐户,否则您将不需要密码。有没有办法告诉哨兵(http://docs.cartalyst.com/sentry-2/installation/laravel-4)这是Facebook登录,不需要"密码"
我尝试给该帐户一个临时密码,但我收到了没有为用户提供哈希器,即使我对其进行哈希处理。
对此有什么建议吗?
我也使用 http://maxoffsky.com/code-blog/integrating-facebook-login-into-laravel-application/作为指南
Route::get('login/fb/callback', function() {
$code = Input::get('code');
if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');
$facebook = new Facebook(Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) return Redirect::to('/')->with('message', 'There was an error');
$me = $facebook->api('/me');
$profile = Profile::whereUid($uid)->first();
if (empty($profile)) {
$user = new User;
$user->name = $me['first_name'].' '.$me['last_name'];
$user->email = $me['email'];
$user->photo = 'https://graph.facebook.com/'.$me['username'].'/picture?type=large';
$user->save();
$profile = new Profile();
$profile->uid = $uid;
$profile->username = $me['username'];
$profile = $user->profiles()->save($profile);
}
$profile->access_token = $facebook->getAccessToken();
$profile->save();
$user = $profile->user;
Auth::login($user);
return Redirect::to('/')->with('message', 'Logged in with Facebook');
});
我认为当你创建用户时,你需要使用Sentry::createUser()
$user = Sentry::createUser(array(
'name' => $me['first_name'].' '.$me['last_name'],
'email' => $me['email'],
'password' => 'test',
'photo' => 'https://graph.facebook.com/'.$me['username'].'/picture?type=large',
));
然后使用 Sentry::login($user, false); 强制用户在没有密码的情况下登录。
您可能还想在密码字段中放置一些内容,而不是测试您是否也有常规的非Facebook登录。
此外,您可能必须激活用户,具体取决于您对该电子邮件的计划:
//You could email this to the user from here.
$activationCode = $user->getActivationCode();
//OR just activate immediately.
$user->attemptActivation($activationCode);
我正在考虑做类似的事情,并考虑使用facebook uid作为密码。这行不通吗?
编辑:我可以为我确认以下工作:
function callback()
{
$code = Input::get('code');
if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');
$facebook = new Facebook(Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) return Redirect::to('/')->with('message', 'There was an error');
$me = $facebook->api('/me');
//dd($me);
//Check if user profile exists
$profile = Profile::whereUid($uid)->first();
if (empty($profile)) {
// Create the user
$user = Sentry::createUser(array(
'email' => $me['email'],
'password' => $uid,
'first_name' => $me['first_name'],
'last_name' => $me['last_name'],
'photo' => 'https://graph.facebook.com/'.$me['username'].'/picture?type=large',
'activated' => 1
));
// Find the group using the group id
$registered = Sentry::findGroupById(2);
// Assign the group to the user
$user->addGroup($registered);
$profile = new Profile();
$profile->uid = $uid;
$profile->username = $me['username'];
$profile = $user->profiles()->save($profile);
}
$profile->access_token = $facebook->getAccessToken();
$profile->save();
$user = $profile->user;
Sentry::login($user, false);
$user = Sentry::getUser();
echo $user->first_name . " logged in.";
//return Redirect::to('/')->with('message', 'Logged in with Facebook');
}
另请注意,您需要自定义哨兵使用此方法 (http://forums.laravel.io/viewtopic.php?pid=48274#p48274) 使用的模型,以便指定与配置文件的关系