动态回调 url laravel

lea*_*eer 5 laravel

我试图使我的回调 url 动态化,因为我在多身份验证系统中配置社交名流。我尝试使用socialiteproviders/manager如下:

    $clientId = env($provider."_client_id");
    $clientSecret = env($provider."_client_secret");
    $redirectUrl = "the url i want";
    $config = new \SocialiteProviders\Manager\Config($clientId,$clientSecret,$redirectUrl);
    return Socialite::with($provider)->setConfig($config)->redirect();
Run Code Online (Sandbox Code Playgroud)

但它说:

调用未定义的方法 Laravel\Socialite\Two\FacebookProvider::setConfig()

尝试使用 facebook 登录时。

有人可以帮帮我吗?谢谢你。

Gar*_*ett 0

我可以重现并找到解决方案。您提供的代码已过时,我在这里找到了它的其他实例: https: //laravel.io/forum/07-28-2016-dynamic-callback-url-laravel-socialite

services.php默认情况下,Socialite 将通过传递 $providerName = 来获取提供程序配置facebook

所以你的代码现在变成:

// The services.php config will return null, fix it by using: strtoupper()

$clientId = env(strtoupper($provider . "_client_id"));
$clientSecret = env(strtoupper($provider . "_client_secret"));
$redirectUrl = "/the-url-i-want";

// ->redirect() acts as a closure, without it, you'll get an error like:
// "Serialization of 'Closure' is not allowed"

$user = Socialite::with($provider)->redirect();
return redirect()->to($redirectUrl)->with(['user', $user]);
Run Code Online (Sandbox Code Playgroud)

有关使用会话数据重定向的更多信息: https://laravel.com/docs/6.x/redirects#redirecting-with-flashed-session-data