gpt4 book ai didi

php - Laravel Socialite - 在重定向之前保存 URL

转载 作者:行者123 更新时间:2023-12-04 00:31:15 26 4
gpt4 key购买 nike

我正在使用 Laravel 5.4Socialite,因此我的用户可以使用 Facebook 登录。

我的网站使用子域 newyork.example.com, paris.example.com example.com

回调登录的 Facebook URL 重定向必须是 absolute 所以我设置了 http://example.com

登录/ Facebook

public function redirectToProvider()
{

$_SESSION['originalURL'] ="http://paris.example.com";

return Socialite::driver('facebook')
->scopes(['rsvp_event', 'public_profile'])
->redirect();
}

登录/facebook/回调

 public function handleProviderCallback(SocialAccountService $service)
{
$user = $service->createOrGetUser(Socialite::driver('facebook')->user());
// $user->token;

Auth::login($user, true);

$originalURL = $_SESSION['originalURL'];

return redirect()->to($originalURL);
}

问题

当我在 route login/facebook 时,我可以使用 HTTP_POST 查看原始 URL paris.example.com

当我在路线 login/facebook/callbackHTTP_POSTexample.com 因为重定向的 URL 是 example.com。我尝试将 URL 保存在 session var 中,但 $_SESSION 是空的。

问题

在 facebook 登录回调重定向后,我该如何获取原始 url。 ?因此,如果我使用 paris.example.com 开始登录过程,我将被重定向到 example.com,然后我将获得要重定向的保存 url

session .php

'cookie' => 'laravel_session',

/*
|--------------------------------------------------------------------------
| Session Cookie Path
|--------------------------------------------------------------------------
|
| The session cookie path determines the path for which the cookie will
| be regarded as available. Typically, this will be the root path of
| your application but you are free to change this when necessary.
|
*/

'path' => '/',

/*
|--------------------------------------------------------------------------
| Session Cookie Domain
|--------------------------------------------------------------------------
|
| Here you may change the domain of the cookie used to identify a session
| in your application. This will determine which domains the cookie is
| available to in your application. A sensible default has been set.
|
*/

'domain' => env('SESSION_DOMAIN', '.localhost.com'),

/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------
|
| By setting this option to true, session cookies will only be sent back
| to the server if the browser has a HTTPS connection. This will keep
| the cookie from being sent to you if it can not be done securely.
|
*/

'secure' => env('SESSION_SECURE_COOKIE', false),

/*
|--------------------------------------------------------------------------
| HTTP Access Only
|--------------------------------------------------------------------------
|
| Setting this value to true will prevent JavaScript from accessing the
| value of the cookie and the cookie will only be accessible through
| the HTTP protocol. You are free to modify this option if needed.
|
*/

'http_only' => true,

最佳答案

我是这样用的,很管用

public function socialConnectRedirect($type, Request $request)
{
Session::put('redirect', $request->input('redirectTo'));
if($type=='facebook'){
return Socialite::driver($type)->scopes(['email', 'public_profile', 'user_birthday', 'user_location'])->redirect();
}

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

在 handleSocialCallback 函数中,用户登录后

Auth::login($checkUser);
return redirect(Session::get('redirect'));
Session::forget('redirect');

关于php - Laravel Socialite - 在重定向之前保存 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44621555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com