laravel 向laravel的socialite包中的回调uri发送附加参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38391775/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:09:40  来源:igfitidea点击:

Sending additional parameters to callback uri in socialite package for laravel

phplaravel

提问by FancyNancy

Im trying to use Socialite package for laravel and I would like to know how to pass additional parameters to callback url. It seems that OAuth allows additional params, but I haven't found any solution for laravel on how to pass them. Currently my methods look like this

我正在尝试为 laravel 使用 Socialite 包,我想知道如何将附加参数传递给回调 url。似乎 OAuth 允许额外的参数,但我还没有找到任何关于如何传递它们的 laravel 解决方案。目前我的方法看起来像这样

    public function login($provider)
    {
        return Socialite::with($provider)->redirect();
    }


   public function callback(SocialAccountService $service, $provider)
    {
        $driver   = Socialite::driver($provider);
        $user = $service->createOrGetUser($driver, $provider);
        $this->auth()->login($user, true);
        return redirect()->intended('/');

    }

Suppose I want to get $user_rolevariable in my callback method. How do I pass it there?

假设我想$user_role在我的回调方法中获取变量。我如何通过它?

回答by mikicaivosevic

You need to use stateparam if you want to pass some data.

如果要传递一些数据,则需要使用状态参数。

Example for your provider

您的提供商的示例

$provider = 'google';
return Socialite::with(['state' => $provider])->redirect();

Your callback function:

您的回调函数:

$provider = request()->input('state');
$driver = Socialite::driver($provider)->stateless();

gist example

要点示例

回答by chebaby

For some reason, Optional Parametersdidn't work for me, so i ended up by using session to pass variables from redirectmethod to the callbackmethod. it's not the best way to do it, but it does the trick.

出于某种原因,可选参数对我不起作用,所以我最终使用 session 将变量从重定向方法传递到回调方法。这不是最好的方法,但它确实有效。

Simplified example

简化示例

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use Socialite;

class FacebookController extends Controller {

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function redirect($my_variable)
    {
        session(['my_variable' => $my_variable]);

        return Socialite::driver('facebook')->redirect();
    }


    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function callback(Request $request)
    {
        try {

            $facebookAccount = Socialite::driver('facebook')->stateless()->user();

            $my_variable = session('my_variable');

            // your logic...

            return redirect()->route('route.name');


        } catch (Exception $e) {


            return redirect()->route('auth.facebook');
        }
    }
}

回答by Manisha

Changing redirect_uri in config on fly will redirect back to the intended domain but it does not return the user when tried to get user with following

动态更改配置中的 redirect_uri 将重定向回目标域,但在尝试通过以下方式获取用户时不会返回用户

Socialite::driver($social)->user()

回答by Ravindra Lande

You can update on the fly callback URL as like:

您可以像这样动态更新回调 URL:

public function redirectToProvider($providerName)
{
    config([
        "services.$providerName.redirect" => config("services.$providerName.redirect").'?queryString=test'
    ]);

    try {
        return Socialite::driver($providerName)->redirect();

    } catch (\Exception $e) {

        return redirect('login');

    }

}

It return queryString(request->all()) in callback URL function.

它在回调 URL 函数中返回queryString(request->all())。