Created
March 22, 2017 11:55
-
-
Save jrdnrc/6254578f1689a1451020a96191c656ac to your computer and use it in GitHub Desktop.
Socialite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
namespace JrdnRc\FplChecker\Laravel\Http\Controllers\OAuth; | |
use JrdnRc\FplChecker\Laravel\Http\Controllers\Controller; | |
use Laravel\Socialite\Contracts\Provider; | |
use Symfony\Component\HttpFoundation\Response; | |
final class AuthenticateViaGoogle extends Controller | |
{ | |
/** @var Provider */ | |
private $provider; | |
/** | |
* AuthenticateViaGoogle constructor. | |
* | |
* @param Provider $provider | |
*/ | |
public function __construct(Provider $provider) | |
{ | |
$this->provider = $provider; | |
} | |
/** | |
* @return Response | |
*/ | |
public function __invoke() : Response | |
{ | |
return $this->provider->redirect(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
namespace JrdnRc\FplChecker\Laravel\Http\Controllers\OAuth; | |
use Illuminate\Auth\SessionGuard; | |
use Illuminate\Contracts\Auth\Guard; | |
use Illuminate\Http\Request; | |
use JrdnRc\FplChecker\Laravel\Http\Controllers\Controller; | |
use JrdnRc\FplChecker\Laravel\User; | |
use Laravel\Socialite\Contracts\Provider; | |
use Symfony\Component\HttpFoundation\Response; | |
final class CompleteGoogleSignIn extends Controller | |
{ | |
/** @var Provider */ | |
private $provider; | |
/** @var Guard|SessionGuard */ | |
private $auth; | |
/** | |
* CompleteGoogleSignIn constructor. | |
* | |
* @param Provider $provider | |
* @param Guard $auth | |
*/ | |
public function __construct(Provider $provider, Guard $auth) | |
{ | |
$this->provider = $provider; | |
$this->auth = $auth; | |
} | |
/** | |
* @param Request $request | |
* @return Response | |
*/ | |
public function __invoke(Request $request) : Response | |
{ | |
if (! $request->query->has('code')) { | |
abort(422, 'Please provide a callback code'); | |
} | |
$this->authenticateUser(); | |
return response('ok'); | |
} | |
/** | |
* @return void | |
*/ | |
private function authenticateUser() | |
{ | |
/** @var \Laravel\Socialite\Two\User $user */ | |
$user = $this->provider->user(); | |
if (null === $systemUser = User::where('google_id', $user->getId())->first()) { | |
$systemUser = User::fromGoogleUser($user); | |
} | |
$token = [ | |
'access_token' => $user->token, | |
'refresh_token' => $user->refreshToken, | |
'expires_in' => $user->expiresIn, | |
'authorization' => 'Bearer', | |
]; | |
if (!$systemUser->tokenMatches($token)) { | |
$systemUser->saveNewToken($token); | |
} | |
$this->auth->login($systemUser, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment