使用Passport通过Laravel / Socialite对用户进行身份验证

时间:2017-11-13 16:31:48

标签: laravel-5 laravel-socialite laravel-passport

我尝试做什么:

我有一个Laravel应用程序,它是我的应用程序的API和我使用此API的SPA。

API的验证在Laravel / Passport之上,对于我的应用程序我使用自己的API如何由Laravel提供。 用户可以通过电子邮件或Laravel / Socialite进行身份验证。这很有效,除非我尝试通过社交网站通过Laravel / Passport验证第三方应用的用户。

所以我打电话给这个应用程序:

export const login = function(){
    const query = http_build_query({
        client_id: apiId,
        redirect_url: apiRedirect,
        response_type: 'code',
        scope: ''
    });

    const url = apiBase+'/oauth/authorize?'+query

    return window.location.replace(url);
}

当用户未经过身份验证时,他会从Passport重定向到Login Page。 当我向' / login'发出请求后那里运作良好。 现在我尝试从此登录页面发出get请求:

socialRedirect({provider}){
    window.location.replace("social/redirect/"+provider);
    throw {};
}

我的Laravel控制器是:

public function getSocialRedirect( $provider )
{
    $providerKey = Config::get('services.' . $provider);

    if (empty($providerKey)) {

        return back()
            ->with('status_message', [
                'type'      => 'error',
                'message'   => [trans('auth.social.provider_not_found', ['provider' => $provider])]
            ]);
    }

    return Socialite::driver( $provider )->redirect();
}

然后处理答案的功能:

public function getSocialHandle( $provider )
{
    if (Input::get('denied') != '') {
        return back()
            ->with('status_message', [
                'type'      => 'error',
                'message'   => [trans('auth.social.denied', ['provider' => $provider])]
            ]);
    }

    //User of the Social Network
    $user = Socialite::driver( $provider )->user();

    //Here I check if the User is already in my database, etc, etc..

    //Then I create A new User in my Database

    $newUser = new User();

    auth()->login($newUser, true);

    //And then I try to come to the Passport page or back to my SPA depending on from where I called this.

    return redirect()->intended($this->redirectPath());
}

我当然使用了Laravel登录控制器中的特征重定向用户。

但是,由于请求来自例如Facebook而不是最初尝试对用户进行身份验证的应用程序,因此无法正常工作。

因此,当用户尝试将我的Oauth用于其他应用时,用户总是登陆我的SPA。

任何人都有想法如何解决这个问题?

0 个答案:

没有答案
相关问题