使用Laravel Socialite和API?

问题描述 投票:9回答:2

我正试图在api上使用Laravel Socialite包。我尝试将代码传递给我的api来获取用户,但它一直给我一个错误:

Fatal error: Call to a member function pull() on null

由于我正在通过API执行请求,因此我采取以下步骤。

向api发送请求以获取代码:

Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()

然后使用上面提取的url发出请求,该url使用code参数重定向。

将代码发送到api并获取用户:

$fb_user = Socialite::with('facebook')->user();

这是它崩溃的地方。我不知道为什么。

我之前使用过这个包,当我只有一个重新加载页面的应用程序时,它工作正常。但当我将它发送到api(在不同的域上)时,它会崩溃。我在想如何生成代码存在一些问题。有没有什么办法解决这一问题?

php laravel laravel-socialite
2个回答
21
投票

刚刚找到我的答案。需要在两个电话中使用stateless

Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()

$fb_user = Socialite::with('facebook')->stateless()->user();

希望这有助于某人。


2
投票

我做了SocialController.php和url(POST请求)/api/social-login接受provideraccess_token

SocialAccount这里是一个laravel模型,您将提供者和provider_user_id以及本地数据库用户ID。下面是social_accounts表的示例

enter image description here

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
    ];
}
}

编辑:

我为同一个https://packagist.org/packages/pimplesushant/laravelsocialiteapi创建了包

© www.soinside.com 2019 - 2024. All rights reserved.