Laravel 5和Socialite-登录后新重定向 [英] Laravel 5 and Socialite - New Redirect After Login

查看:64
本文介绍了Laravel 5和Socialite-登录后新重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里还有另一个新问题,但希望有人可以阐明一下:

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

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

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

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

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

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

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

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.

为澄清起见,我的路线设置正确.我已经尝试过可以想象的每个版本的重定向"(到",返回",预期",重定向::等),并且该方法正在从我的Auth Controller调用(尽管我在其他地方也尝试过)也是如此).

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).

问题是,使用社交名流存储和登录用户后,如何覆盖该redirect()?任何帮助表示赞赏!预先谢谢你.

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

...但是,在此之前运行的SocialAuth类大约有500行,因为它必须确定用户是否存在,必要时注册新用户,显示适用于不同情况的表单,等等.同时,这是通过Social Auth类发送信息的功能:

... 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:

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

推荐答案

我相信您对此有过多的思考.使用Socialite非常简单:

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

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

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

 '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).

在登录控制器方法中:

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

然后在回调函数中

$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.

这篇关于Laravel 5和Socialite-登录后新重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆