简体   繁体   English

Oauth登录不起作用-重定向次数过多(ERR_TOO_MANY_REDIRECTS)

[英]Oauth Login is not working - Redirect too many times (ERR_TOO_MANY_REDIRECTS)

I have a facebook login button with this route: 我有一个使用此路线的Facebook登录按钮:

<a href="{{route('social.auth',['provider' => 'facebook'])}}"  

style="background-color: #3b5998; color:white;"> 
 <i class="fa fa-facebook" aria-hidden="true"></i> Login</a>

When the button is clicked the user is redirected to " http://proj.test/auth/facebook/callback?code=... = " 单击该按钮后,用户将被重定向到“ http://proj.test/auth/facebook/callback?code = ... =

But it appears an error: 但是出现一个错误:

Page is not working, proj.test redirect too many times. ERR_TOO_MANY_REDIRECTS.

Do you know where can be the error? 您知道错误在哪里吗?

Routes: 路线:

Auth::routes();


Route::get('auth/{provider}/callback', [
    'uses' => 'OauthController@redirectToProvider',
    'as'   => 'social.auth'
]);

Route::get('auth/{provider}/callback', [
    'uses' =>   'OauthController@redirectToProvider',
    'as' => 'social.auth'
]);
`

OAuthcontroller: OAuthcontroller:

class OauthController extends Controller

{
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->redirect();

    }
    public function handleProviderCallback()
    {
        $user = Socialite::driver('facebook')->user();

        return $user->getEmail();

        // $user->token;
    }
}

In the App domains I have: 在应用程序域中,我有:

proj.test

In the site url: 在网站网址中:

http://proj.test/
http://proj.test/facebook/auth/callback

Both of your routes are pointing to the redirectToProvider method. 您的两条路由都指向redirectToProvider方法。 Change 更改

Route::get('auth/{provider}/callback', 'OauthController@redirectToProvider');

To

Route::get('auth/{provider}/callback', 'OauthController@handleProviderCallback');

At the beginning you have: 开始时,您有:

Route::get('auth/{provider}/callback', [
    'uses' => 'OauthController@redirectToProvider',
    'as'   => 'social.auth'
]);

Route::get('auth/{provider}/callback', [
    'uses' =>   'OauthController@redirectToProvider',
    'as' => 'social.auth'
]);

That's why you get the Page is not working, proj.test redirect too many times. ERR_TOO_MANY_REDIRECTS. 这就是为什么您导致Page is not working, proj.test redirect too many times. ERR_TOO_MANY_REDIRECTS. Page is not working, proj.test redirect too many times. ERR_TOO_MANY_REDIRECTS. exception. 例外。 User was redirected to the provider login page and after sucessfull login he goes back to the... redirectToProvider! 用户被重定向到提供程序登录页面,成功登录后,他返回到... redirectToProvider! In seconds and subsequences calls redirectToProvider you already was successfully logged so the login windows doesn't appears and provider (facebook) instantly redirect back to the redirectToProvider method and story repeats as longs as browsers throws the exception. 在几秒钟内,子序列调用redirectToProvider,您已经成功登录,因此不会出现登录窗口,并且只要浏览器抛出异常,提供者(facebook)就会立即重定向回到redirectToProvider方法,并且故事会重复。

As @btl mentioned you have 2 equal routes, so you changed it to: 正如@btl提到的,您有2条相等的路由,因此将其更改为:

Route::get('auth/{provider}/callback', [
    'uses' => 'OauthController@redirectToProvider',
    'as' => 'social.auth'
]);

Route::get('auth/{provider}/callback', [
    'uses' => 'OauthController@handleProviderCallback',
    'as' => 'social.auth'
]);

Now you have 2 routes which points to redirectToProvider and handleProviderCallback methods, but... the handleProviderCallback override the first route. 现在,您有2条路由,它们指向redirectToProvider和handleProviderCallback方法,但是... handleProviderCallback覆盖了第一条路由。 You cannot define 2 routes with same method (get in this case) which share the same uri . 不能使用相同 的uri 定义具有相同方法的2条路线 (在本例中为get)。 Change first one to eg auth/{provider} . 将第一个更改为例如auth/{provider} In the second one remove the 'as' => 'social.auth' part ( route name must be unique ). 在第二部分中,删除'as' => 'social.auth'部分( 路由名称必须是唯一的 )。

In consequence when you click login button you goes to the handleProviderCallback and Scialite detects that state is not valid, that's why you get Laravel \\ Socialite \\ Two \\ InvalidStateException No message exception. 因此,当您单击登录按钮时,您转到handleProviderCallback并且Scialite检测到该状态无效,这就是为什么您得到Laravel \\ Socialite \\ Two \\ InvalidStateException No message异常的原因。 In redirectToProvier method Socialite store state value in session and pass it to the provider via ?state=... query param. 在redirectToProvier方法中,社交名流将状态值存储在会话中,并通过?state=...查询参数将其传递给提供程序。 After successfully login facebook redirect to the callback url with the same state you passed in the redirectToProvider method (implemented under the hood). 成功登录Facebook后,将重定向到具有您在redirectToProvider方法(在幕后实现)中传递的状态的状态的回调URL。 Socialite compare the state retrieved from the provider with the state stored in the session and throws an above exception if states are different. 社交名媛将从提供者获取的状态与会话中存储的状态进行比较,如果状态不同,则抛出上述异常。 Because you go directly to the handleProviderCallback you get the empty state value which will never match the one stored in the session (even if session is empty). 因为您直接进入handleProviderCallback,所以您将获得一个空状态值,该值将永远与会话中存储的状态值不匹配(即使session为空)。

Correct routes: 正确路线:

Route::get('auth/{provider}', [
    'uses' => 'OauthController@redirectToProvider',
    'as' => 'social.auth'
]);

Route::get('auth/{provider}/callback', [
    'uses' => 'OauthController@handleProviderCallback',
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM