Instantly share code, notes, and snippets.
Created
October 28, 2020 18:26
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save ajoy39/71be22ad1782a6a3a51a3ef106caf1de to your computer and use it in GitHub Desktop.
Socialite Controller
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 | |
namespace App\Http\Controllers; | |
use App\User; | |
use App\Services\RegistrationService; | |
use App\SocialiteAccount; | |
use Illuminate\Http\RedirectResponse; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Str; | |
use Laravel\Socialite\AbstractUser as SocialiteUser; | |
use Socialite; | |
class SocialiteController extends Controller | |
{ | |
protected $registration_service; | |
/** | |
* Create a new controller instance. | |
* | |
* @param RegistrationService $registrationService | |
*/ | |
public function __construct(RegistrationService $registrationService) | |
{ | |
parent::__construct(); | |
$this->registration_service = $registrationService; | |
} | |
/** | |
* Redirect the user to the Twitter authentication page. | |
* | |
* @param \Illuminate\Http\Request $request Incoming HTTP Request | |
* @param string $provider Oauth Provider to use | |
* @param string $action The action the user is attempting | |
* | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function redirectToProvider(Request $request, $provider, $action): RedirectResponse | |
{ | |
$request->session()->put('action', $action); | |
return Socialite::driver($provider)->redirect(); | |
} | |
/** | |
* Determine if the user has an account | |
* | |
* @param \Illiminate\Http\Request $request The incoming HTTP request | |
* @param string $provider Oauth Provider to use | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function handleCallback(Request $request, $provider): RedirectResponse | |
{ | |
$provider_user = Socialite::driver($provider)->user(); | |
$action = $request->session()->pull('action'); | |
switch ($action) { | |
case 'login': | |
return $this->_login($provider, $provider_user); | |
case 'register': | |
return User::where('email', $provider_user->getEmail())->exists() ? | |
redirect()->login() : | |
$this->_register($provider, $provider_user); | |
case 'link': | |
return $this->_link($provider, $provider_user, auth()->user()); | |
default: | |
return new \Exception(); | |
} | |
} | |
/** | |
* Attempt login a using Socialite | |
* | |
* @param string $provider The Oauth Provider | |
* @param \Laravel\Socialite\AbstractUser $user A Socialite User | |
* | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
private function _login($provider, SocialiteUser $user): RedirectResponse | |
{ | |
$socialite_account = SocialiteAccount::whereProvider($provider) | |
->whereProviderUserId($user->getId()) | |
->with('user') | |
->first(); | |
if ($socialite_account) { | |
\Auth::login($socialite_account->user); | |
return redirect()->to('/podcasts'); | |
} else { | |
return redirect() | |
->route('login') | |
->with( | |
[ | |
"message" => [ | |
'status' => 'error', | |
'message' => __( | |
'auth.oauth.login_failed', | |
['provider' => Str::title($provider)] | |
) | |
] | |
] | |
); | |
} | |
} | |
/** | |
* Register a new user with Socialite | |
* | |
* @param string $provider The Oauth Provider | |
* @param \Laravel\Socialite\AbstractUser $user A Socialite User | |
* | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
private function _register($provider, SocialiteUser $user): RedirectResponse | |
{ | |
$user = $this->registration_service->createSocialiteUser($user, $provider); | |
\Auth::login($user); | |
return redirect()->to('/welcome'); | |
} | |
/** | |
* Link Socialite Account to User | |
* | |
* @param string $provider The Oauth Provider | |
* @param SocialiteUser $socialite_user A Socialite User | |
* @param User $user The user to link to | |
* | |
* @return RedirectResponse | |
*/ | |
private function _link(string $provider, SocialiteUser $socialite_user, User $user): RedirectResponse | |
{ | |
$user->socialiteAccounts()->create( | |
[ | |
'provider' => $provider, | |
'provider_user_id' => $socialite_user->getId() | |
] | |
); | |
return redirect() | |
->route('account.index') | |
->with( | |
'message', | |
[ | |
'status' => 'success', | |
'message' => __( | |
'auth.oauth.linked', | |
['provider' => Str::title($provider)] | |
) | |
] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment