我做了同樣的事情(沒有從我的網站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規範,並有了我的道路。謝謝! –
一條評論:如果用戶創建帳戶,然後嘗試使用(未鏈接的)社交提供商登錄,則如果用戶使用不同的電子郵件地址(如一次性電子郵件),則最終可能會有多個帳戶。 –