我创建了一个Android应用程序,它与我使用PHP设置的API进行通信,并尝试使用Laravel重写PHP内容。我想要做的重大改变之一是允许OAuth登录到我的应用程序中,该应用程序与本地服务器上的用户帐户相关联,而我对这样做所涉及的应用程序流感到困惑。
在注册之前,我是否应该在我的安卓应用程序上进行OAuth身份验证,然后在调用我的API创建本地用户帐户时,存储某种凭据和用户信息?
此外,在使用OAuth登录用户时,我如何知道由提供者身份验证的人(比如Facebook )与我服务器上的本地用户帐户相关联?有什么我可以储存的记号吗?
感谢您提供的任何概述信息。
发布于 2013-12-28 10:18:51
我对HybridAuth也做了同样的事情(没有来自我的站点的HybridAuth提供程序),还有一个packagist.org的composer软件包。
我有两张桌子:
我的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提供程序,例如添加/配置一个混合提供程序。
对你的问题:
在注册之前,我是否应该在我的安卓应用程序上进行OAuth身份验证,然后在调用我的API创建本地用户帐户时,存储某种凭据和用户信息?
我将与任何其他OAuth提供程序一样处理它:就像上面的示例一样,一个函数要调用(我只使用通过Laravel登录,所以我没有即将到期的提供者会话的问题,用户不需要创建另一个帐户)。
此外,在使用OAuth登录用户时,我如何知道由提供者身份验证的人(比如Facebook )与我服务器上的本地用户帐户相关联?有什么我可以储存的记号吗?
这是由身份验证表中的user_id列完成的。
希望这能有所帮助。
https://stackoverflow.com/questions/20711561
复制相似问题