public function handle(Request $request)
{
$redirectUri = (string) $request->getAttribute('originalUri', $request->getUri())->withQuery('');
$this->provider = $this->getProvider($redirectUri);
$session = $request->getAttribute('session');
$queryParams = $request->getQueryParams();
$code = array_get($queryParams, 'code');
$state = array_get($queryParams, 'state');
if (!$code) {
$authUrl = $this->provider->getAuthorizationUrl($this->getAuthorizationUrlOptions());
$session->set('oauth2state', $this->provider->getState());
return new RedirectResponse($authUrl . '&display=popup');
} elseif (!$state || $state !== $session->get('oauth2state')) {
$session->forget('oauth2state');
echo 'Invalid state. Please close the window and try again.';
exit;
}
$this->token = $this->provider->getAccessToken('authorization_code', compact('code'));
$owner = $this->provider->getResourceOwner($this->token);
$identification = $this->getIdentification($owner);
$suggestions = $this->getSuggestions($owner);
return $this->authResponse->make($request, $identification, $suggestions);
}