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

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

Laravel 5 and Socialite - New Redirect After Login

laravelredirectlaravel-5laravel-socialite

提问by DonnaJo

Another newb question here, but hopefully someone can shed some light:

这里有另一个新问题,但希望有人能解释一下:

I am using Socialite with Laravel 5, and I want to be able to redirect the user to a page on the site after they have logged in. The problem is that using

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

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

simply redirects back to 'social-site/login?code=afkjadfkjdslkfjdlkfj...' (where 'social-site' is whatever site is being used i.e. facebook, twitter, google, etc.)

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

So, what appears to me to be happening is that the redirect() function in the Socialite/Contracts/Provider interface is overriding any redirect that I attempt after the fact.

所以,在我看来正在发生的事情是 Socialite/Contracts/Provider 界面中的 redirect() 函数覆盖了我事后尝试的任何重定向。

Just for clarification, my routes are set up properly. I have tried every version of 'redirect' you can imagine ('to', 'back', 'intended', Redirect::, etc.), and the method is being called from my Auth Controller (though I have tried it elsewhere as well).

只是为了澄清,我的路线设置正确。我已经尝试了您可以想象的所有“重定向”版本(“to”、“back”、“intended”、Redirect:: 等),并且正在从我的 Auth Controller 调用该方法(尽管我已在其他地方尝试过)同样)。

The question is, how do I override that redirect() once I am done storing and logging in the user with socialite? Any help is appreciated! Thank you in advance.

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

The code that contains the redirect in question is:

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

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 ] );
    }
}

... The SocialAuth class that runs before this, however, is about 500 lines long, as it has to determine if the user exists, register new users if necessary, show forms for different scenarios, etc. Meanwhile, here is the function that sends the information through from the Social Auth class:

... 然而,在此之前运行的 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);
    }
}

回答by hfingler

I believe you are overthinking this. Using Socialite is pretty straight forward:

我相信你想多了。使用 Socialite 非常简单:

Set up config/services.php. For facebook I have this:

设置config/services.php。对于 facebook 我有这个:

 'facebook' => [
    'client_id' => 'your_fb_id',
    'client_secret' => 'your_fb_secret',
    'redirect' => '>ABSOLUTE< url to redirect after login', //like: 'http://stuff'
  ],

Then set up two routes, one for login and one for callback (after login).

然后设置两条路由,一条用于登录,一条用于回调(登录后)。

In the login controller method:

在登录控制器方法中:

return \Socialize::with('facebook')->redirect();

Then in the callback function

然后在回调函数中

$fb_user = \Socialize::with('facebook')->user();
// check if user exists, create it and whatnot
//dd($fb_user);
return redirect()->route('some.route');

It should be pretty much similar for all other providers.

对于所有其他提供程序,它应该非常相似。

回答by Prakhar

I managed to workaround this problem, but I am unsure if this is the best way to fix it. Similar to what is stated in question, I got authenticated callback from the social media, but I was unable to redirect current response to another url.

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

Based on the callback request params, I was able to create and authenticate the user within my Laravel app. It worked good so far but the problems occured after this step when I tried to do a return redirect()->route('dashboard');. I tried all the flavours of redirect()helper and Redirectfacade but nothing helped.

基于回调请求参数,我能够在我的 Laravel 应用程序中创建和验证用户。到目前为止它运行良好,但是当我尝试执行return redirect()->route('dashboard');. 我尝试了redirect()助手和Redirect外观的所有口味,但没有任何帮助。

The blank page just stared at my face for over 2 days, before I checked this question. The behaviour was very similar. I got redirect from social-media to my app but could not further redirect in the same response cycle.

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

At this moment (when the callback was recieved by the app and user was authenticated), if I refreshed the page manually (F5), I got redirected to the intended page. My interpretation is similar to what's stated in this question earlier. The redirect from social-media callback was dominating the redirects I was triggering in my controller (May be redirect within Laravel app got suppressed because the redirect from social-media was still not complete). It's just my interpretation. Experts can throw more light if they think otherwise or have a better explaination.

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

To fix this I issued a raw http redirect using header("Location /dashboard");and applied auth middleware to this route. This way I could mock the refresh functionality ,redirect to dashboard (or intended url) and check for authentication in my DashboardController.

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

Once again, this is not a perfect solution and I am investigating the actual root of the problem, but this might help you to move ahead if you are facing similar problem.

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

回答by zmonteca

We are using the Socialite login in our UserController as a trait. We simply overrode the AuthenticatesSocialiteLogin::loginSuccess() in our controller.

我们在 UserController 中使用 Socialite 登录名作为特征。我们只是覆盖了控制器中的 AuthenticatesSocialiteLogin::loginSuccess()。

use Broco\SocialiteLogin\Auth\AuthenticatesSocialiteLogin;

class UserController extends BaseController
{
    use AuthenticatesSocialiteLogin;

    public function loginSuccess($user)
    {
        return redirect()->intended(url('/#login-success'));
    }

....