将 Laravel Socialite 与 API 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35966605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Laravel Socialite with an API?
提问by Rob
I'm trying to use Laravel Socialite package over an api. I try to pass the code into my api to fetch the user but it keeps giving me an error:
我正在尝试通过 api 使用 Laravel Socialite 包。我尝试将代码传递到我的 api 中以获取用户,但它一直给我一个错误:
Fatal error: Call to a member function pull() on null
Since I'm doing the request over an API, I take the following steps.
由于我是通过 API 执行请求,因此我采取以下步骤。
Send a request to api for the url to fetch the code:
向 api 发送请求以获取 url 以获取代码:
Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()
Then make a request with the above fetched url, which redirects with the code
parameter.
然后使用上面获取的 url 发出请求,该 url 使用code
参数重定向。
Send the code to the api and fetch the user:
将代码发送到 api 并获取用户:
$fb_user = Socialite::with('facebook')->user();
This is where it crashes. I'm not sure why.
这是它崩溃的地方。我不知道为什么。
I've used the package before and it works fine when I just have an app that reloads the page. But when I send it to an api (on a different domain) it crashes. I'm thinking there is some issue with how the code is generated. Is there anyway to fix this?
我以前使用过这个包,当我只有一个重新加载页面的应用程序时,它工作正常。但是当我将它发送到一个 api(在不同的域上)时它崩溃了。我认为代码的生成方式存在一些问题。有没有什么办法解决这一问题?
回答by Rob
Just found my answer. Need to use stateless
in both calls:
刚刚找到我的答案。需要stateless
在两个调用中使用:
Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()
$fb_user = Socialite::with('facebook')->stateless()->user();
Hope this helps someone.
希望这可以帮助某人。
回答by Sushant Pimple
I made SocialController.php
and url (POST request) /api/social-login
which accepts provider
and access_token
.
我制作了接受和的SocialController.php
网址(POST 请求)。/api/social-login
provider
access_token
SocialAccount
here is a laravel model where you'll provider and provider_user_id and local database user id. Below is the example of social_accounts
table
SocialAccount
这是一个 Laravel 模型,您将在其中提供 provider 和 provider_user_id 以及本地数据库用户 ID。下面是social_accounts
表格的例子
And in SocialController
:
并在SocialController
:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\User;
use App\SocialAccount;
use Socialite;
class SocialController extends Controller
{
public function social(Request $request) {
$provider = $request->input('provider');
switch($provider){
case SocialAccount::SERVICE_FACEBOOK:
$social_user = Socialite::driver(SocialAccount::SERVICE_FACEBOOK)->fields([
'name',
'first_name',
'last_name',
'email'
]);
break;
case SocialAccount::SERVICE_GOOGLE:
$social_user = Socialite::driver(SocialAccount::SERVICE_GOOGLE)
->scopes(['profile','email']);
break;
default :
$social_user = null;
}
abort_if($social_user == null , 422,'Provider missing');
$social_user_details = $social_user->userFromToken($request->input('access_token'));
abort_if($social_user_details == null , 400,'Invalid credentials'); //|| $fb_user->id != $request->input('userID')
$account = SocialAccount::where("provider_user_id",$social_user_details->id)
->where("provider",$provider)
->with('user')->first();
if($account){
return $this->issueToken($account->user);
}
else {
// create new user and social login if user with social id not found.
$user = User::where("email",$social_user_details->getEmail())->first();
if(!$user){
// create new social login if user already exist.
$user = new User;
switch($provider){
case SocialAccount::SERVICE_FACEBOOK:
$user->first_name = $social_user_details->user['first_name'];
$user->last_name = $social_user_details->user['last_name'];
break;
case SocialAccount::SERVICE_GOOGLE:
$user->first_name = $social_user_details->user['name']['givenName'];
$user->last_name = $social_user_details->user['name']['familyName'];
break;
default :
}
$user->email = $social_user_details->getEmail();
$user->username = $social_user_details->getEmail();
$user->password = Hash::make('social');
$user->save();
}
$social_account = new SocialAccount;
$social_account->provider = $provider;
$social_account->provider_user_id = $social_user_details->id;
$user->social_accounts()->save($social_account);
return $this->issueToken($user);
}
}
private function issueToken(User $user) {
$userToken = $user->token() ?? $user->createToken('socialLogin');
return [
"token_type" => "Bearer",
"access_token" => $userToken->accessToken
];
}
}
EDIT:
编辑:
I have created package for the same https://packagist.org/packages/pimplesushant/laravelsocialiteapi
我为相同的https://packagist.org/packages/pimplesushant/laravelsocialiteapi创建了包