overtrue / socialite

Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
MIT License
1.3k stars 241 forks source link

进行第三方登陆,有时候可以正常登录有时候就报错 #18

Closed dullme closed 8 years ago

dullme commented 8 years ago

我换其他帐号也是一样 微博也是, 但是清除一下cookie 后就可以正常了,然后qq正常后 微博也正常了,只要其中一个不正常那么 两个都是不正常的, 我测试了一下 IE正常 EDGE不报错但是 没登录成功 这是什么问题啊? php 代码

/**
 * QQ登录
 */
public function qqLogin(){
    $this->otherLogin('qq',1);
    return redirect('/');
}

/**
 * 第三方登录
 * @param unknown $name 第三方登录名称
 * @param unknown $type 登录类型 1QQ 2微博
 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
 */
public function otherLogin($name, $type){
    $socialite = new SocialiteManager(config('services'));
    $clientUser = $socialite->driver($name)->user();
    $user = User::where('account',$clientUser->getId())->first();
    if (!$user){
        $user = User::create([
                'account' => $clientUser->getId(),
                'password' => bcrypt(str_random(16)),
                'name' => $clientUser->getNickname(),
                'login_type' => $type,
                'avatar' => $clientUser->getAvatar(),
        ]);
    }
    \Auth::login($user);
}

报错

Whoops, looks like something went wrong. 1/1 InvalidStateException in AbstractProvider.php line 170:

in AbstractProvider.php line 170
at AbstractProvider->user() in LoginController.php line 56
at LoginController->otherLogin('qq', '1') in LoginController.php line 36
at LoginController->qqLogin()
at call_user_func_array(array(object(LoginController), 'qqLogin'), array()) in Controller.php line 80
at Controller->callAction('qqLogin', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(LoginController), object(Route), 'qqLogin') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(LoginController), object(Route), object(Request), 'qqLogin') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\Home\LoginController', 'qqLogin') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 58
overtrue commented 8 years ago

@dullme 建议使用一个干净的环境,清除所有的 cookie 后进行测试,这个报错原因是带回来的 state 值 与 cookie 中的 state 值匹配不上所致。

dullme commented 8 years ago

这些是$clientUser qq传回来的值,我不知道如何输出这个 state值?我想比对一下,因为我是用了一个新的laravel 5.2 项目 然以直接 接入qq也是同样的问题,就是 第一次登录必定会失败,删除cookie后可以正常登录,我还是不清楚具体问题的原因,如果能打印出 你说的 那个state值 看一下我可能会好懂一些!

User {#469 ▼

attributes: array:7 [▼

"id" => "1AE29162841F182BF91A445570C5"
"nickname" => "你好,再见!"
"name" => "你好,再见!"
"email" => null
"avatar" => "http://q.qlogo.cn/qqapp/101294860/1AE29162841F182BF91A445570C5/100"
"original" => array:18 [▼
  "ret" => 0
  "msg" => ""
  "is_lost" => 0
  "nickname" => "你好,再见!"
  "gender" => "男"
  "province" => ""
  "city" => "安道尔城"
  "year" => "1990"
  "figureurl" => "http://qzapp.qlogo.cn/qzapp/101294860/1AE29162841F182BF91A445570C5/30"
  "figureurl_1" => "http://qzapp.qlogo.cn/qzapp/101294860/1AE29162841F182BF91A445570C5/50"
  "figureurl_2" => "http://qzapp.qlogo.cn/qzapp/101294860/1AE29162841F182BF91A445570C5/100"
  "figureurl_qq_1" => "http://q.qlogo.cn/qqapp/101294860/1AE29162841F182BF91A445570C5/40"
  "figureurl_qq_2" => "http://q.qlogo.cn/qqapp/101294860/1AE29162841F182BF91A445570C5/100"
  "is_yellow_vip" => "0"
  "vip" => "0"
  "yellow_vip_level" => "0"
  "level" => "0"
  "is_yellow_year_vip" => "0"
]
"token" => AccessToken {#447 ▼
  #attributes: array:3 [▼
    "access_token" => "A506FB549101DB**_27AF150**_20CAF"
    "expires_in" => "7776000"
    "refresh_token" => "302A99E510068A**_AC60BA**_FF9D8F"
  ]
}

] }

overtrue commented 8 years ago

@dullme state 值在 url 里面,Laravel 可以使用 Input::all() 得到,另外我猜想原因可能是这样的:

“第一次登录必定会失败” 这里的第一次不是太清楚如何定义的?是重新建立项目还是怎么样的第一次?

dullme commented 8 years ago

超哥 已经解决了是我自己的问题,就是顶级域名和二级域名的关系,登录的时候访问的是顶级域名,而qq回调的时候是带www的二级域名,所以才导致的问题,我现在顶级域名重定向为www的二级域名就OK了!谢谢!

overtrue commented 8 years ago

@dullme 那太好了,毕竟下次别人遇到就好解决了。