我做了同样的事情(没有从我的网站OAuth提供商)与HybridAuth还有一个composer package at packagist.org。
我有两个表:
- 用户的列ID,电子邮件,密码(表跟一般的用户和用户的相关信息)
- 认证的列:ID,USER_ID,提供商,provider_uid(认证表与OAuth的东西)
我在我的AuthController中的代码(路由/登录/社会Route::controller('login','AuthController');
)
<?php
class AuthController extends BaseController {
//@see http://www.mrcasual.com/on/coding/laravel4-package-management-with-composer/
public function getSocial($action = '')
{
// check URL segment
if ($action == 'auth') {
// process authentication
try {
Hybrid_Endpoint::process();
}
catch (Exception $e) {
// redirect back to http://URL/social/
return Redirect::to('login/social');
}
return;
}
try {
// create a HybridAuth object
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
// authenticate with provider
if($action != ''){
// only allow facebook and google
if(in_array($action,array('google','facebook'))){
$provider = $socialAuth->authenticate($action);
}else{
// catch this form_error in the login form
return Redirect::to('login')->withErrors(array('form_errors'=>'The url was invalid'));
}
}else{
return Redirect::to('login');
}
// fetch user profile
$userProfile = $provider->getUserProfile();
}
catch(Exception $e) {
// exception codes can be found on HybBridAuth's web site
return Redirect::to('login')->withErrors(array('form_errors'=>'Error on Login: #'.$e->getCode().': '.$e->getMessage()));
}
/*access user profile data
echo "Connected with: <b>{$provider->id}</b><br />";
echo "As: <b>{$userProfile->displayName}</b><br />";
echo "<pre>" . print_r($userProfile, true) . "</pre><br />";
die();*/
//check if User exists
if($user_id = DB::table('authentications')->where('provider', $provider->id)->where('provider_uid',$userProfile->identifier)->pluck('user_id')){
//login user
Auth::loginUsingId($user_id);
//update user details (eg photo, name, etc)
//Here you can update the user details
return Redirect::to('dashboard');
}else{
//lets see if we already know this email -> connect it with the registered account
if($user = User::where('email',$userProfile->email)->first()){
$user->authentications()->save(new Authentication(array('provider'=>$provider->id, 'provider_uid'=>$userProfile->identifier)));
Auth::login($user);
//here you can update the user details
return Redirect::to('dashboard')->with('user_social_linked',$provider->id);
}else{
//register user
$user = $this->createUser(array('email'=>$userProfile->email,'password'=>''));
$user->authentications()->save(new Authentication(array('provider'=>$provider->id, 'provider_uid'=>$userProfile->identifier)));
Auth::login($user);
//here you can set/update the user details
return Redirect::to('dashboard')->with('user_created',true);
}
}
}
private function createUser($credentials)
{
$user = new User();
$user->email = $credentials['email'];
$user->password = $credentials['password'];
$user->save();
Auth::login($user);
return $user;
}
}
用户模型还具有以下功能
<?php
public function setPasswordAttribute($password){
//disable blank passwords => disables account login (eg login only with third party aka social providers)
if($password != ''){
$this->attributes['password'] = Hash::make($password);
}else{
$this->attributes['password'] = $password;
}
}
现在你可以把自己的OAuth提供者,就像任何其他的OAuth提供者,例如通过添加/配置hybridauth提供商。
您的问题:
我应该做我的OAuth认证在我的Android应用程序在注册前,然后保存某种形式的凭证与用户信息一起,当我打电话给我的API来创建我的本地用户帐户?
我会处理它同任何其他OAuth的提供商:就像上面的例子中一个函数调用(我只能通过Laravel验证使用的登录,所以我不到期的供应商会议有问题,用户不必创建另一个帐户)。
此外,在使用OAuth登录用户时,如何知道提供者认证的人员(例如Facebook)与我的服务器上的本地用户帐户相关联?是否有某种我可以存储的令牌?
这由认证表中的user_id列完成。
希望有所帮助。
非常有帮助。阅读OAuth2规范,并有了我的道路。谢谢! –
一条评论:如果用户创建帐户,然后尝试使用(未链接的)社交提供商登录,则如果用户使用不同的电子邮件地址(如一次性电子邮件),则最终可能会有多个帐户。 –