gpt4 book ai didi

Laravel 5 和 Socialite - 登录后新的重定向

转载 作者:行者123 更新时间:2023-12-02 15:47:45 26 4
gpt4 key购买 nike

这里还有一个新问题,但希望有人能阐明一些:

我正在 Laravel 5 中使用 Socialite,我希望能够在用户登录后将用户重定向到网站上的页面。问题是使用

return redirect('any-path-I-put-here');

简单地重定向回“social-site/login?code=afkjadfkjdslkfjdlkfj...”(其中“social-site”是正在使用的任何网站,即 facebook、twitter、google 等)

因此,在我看来正在发生的事情是,Socialite/Contracts/Provider 接口(interface)中的 redirect() 函数会覆盖我在事后尝试的任何重定向。

为了澄清,我的路线设置正确。我已经尝试了您可以想象的每个版本的“重定向”(“到”、“返回”、“预期”、Redirect::等),并且该方法是从我的 Auth Controller 调用的(尽管我在其他地方尝试过)以及)。

问题是,一旦我完成了社交名流用户的存储和登录,如何覆盖该重定向()?任何帮助表示赞赏!预先感谢您。

包含相关重定向的代码是:

public function socialRedirect( $route, $status, $greeting, $user )
{

$this->auth->login( $user, true );

if( $status == 'new_user' ) {
// This is a new member. Make sure they see the welcome modal on redirect
\Session::flash( 'new_registration', true );

return redirect()->to( $route );// This is just the most recent attempt. It originated with return redirect($route);, and has been attempted every other way you can imagine as well (as mentioned above). Hardcoding (i.e., 'home') returns the exact same result. The socialite redirect always overrides anything that is put here.
}
else {
return redirect()->to( $route )->with( [ 'greeting' => $greeting ] );
}
}

...然而,在此之前运行的 SocialAuth 类大约有 500 行长,因为它必须确定用户是否存在,必要时注册新用户,显示不同场景的表单等。同时,这里是从 Social Auth 类发送信息的函数:

private function socialLogin( $socialUser, $goto, $provider, $status, $controller )
{
if( is_null( $goto ) ) {
$goto = 'backlot/' . $socialUser->profile->custom_url;
}

if( $status == 'new_user' ) {
return $controller->socialRedirect($goto, $status, null, $socialUser);
}
else {
// This is an existing member. Show them the welcome back status message.
$message = 'You have successfully logged in with your ' .
ucfirst( $provider ) . ' credentials.';

$greeting =
flash()->success( 'Welcome back, ' . $socialUser->username . '. ' . $message );

return $controller->socialRedirect($goto, $status, $greeting, $socialUser);
}
}

最佳答案

我设法解决了这个问题,但我不确定这是否是解决它的最佳方法。与所讨论的内容类似,我从社交媒体获得了经过身份验证的回调,但我无法将当前响应重定向到另一个网址。

根据回调请求参数,我能够在 Laravel 应用程序中创建并验证用户。到目前为止效果很好,但是当我尝试执行 return redirect()->route('dashboard'); 时,在这一步之后出现了问题。我尝试了 redirect() 帮助程序和 Redirect 外观的所有风格,但没有任何帮助。

在我检查这个问题之前,空白页盯着我的脸看了两天多。行为非常相似。我从社交媒体重定向到我的应用程序,但无法在同一响应周期中进一步重定向。

此时(当应用程序收到回调并且用户已通过身份验证时),如果我手动刷新页面(F5),我会被重定向到预期页面。我的解释与之前这个问题中所说的类似。来自社交媒体回调的重定向主导了我在 Controller 中触发的重定向(可能是 Laravel 应用程序内的重定向被抑制,因为来自社交媒体的重定向仍未完成)。这只是我的解释。如果专家有不同的想法或有更好的解释,他们可以提供更多的见解。

为了解决这个问题,我使用 header("Location/dashboard"); 发出了原始 http 重定向,并将身份验证中间件应用于此路由。这样我就可以模拟刷新功能,重定向到仪表板(或预期的 url)并检查 DashboardController 中的身份验证。

再说一次,这不是一个完美的解决方案,我正在调查问题的实际根源,但如果您面临类似的问题,这可能会帮助您继续前进。

关于Laravel 5 和 Socialite - 登录后新的重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30487429/

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