Created
October 17, 2018 18:11
-
-
Save johndavedecano/6d3d2360209d3548f29318a06a6617f3 to your computer and use it in GitHub Desktop.
Social Login
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\Auth; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Foundation\Auth\AuthenticatesUsers; | |
use Socialite; | |
use App\User; | |
use Illuminate\Support\Facades\Auth; | |
class LoginController extends Controller | |
{ | |
/* | |
|-------------------------------------------------------------------------- | |
| Login Controller | |
|-------------------------------------------------------------------------- | |
| | |
| This controller handles authenticating users for the application and | |
| redirecting them to your home screen. The controller uses a trait | |
| to conveniently provide its functionality to your applications. | |
| | |
*/ | |
use AuthenticatesUsers; | |
/** | |
* Where to redirect users after login. | |
* | |
* @var string | |
*/ | |
protected $redirectTo = '/admin'; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('guest')->except('logout'); | |
} | |
/** | |
* Redirect the user to the GitHub authentication page. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function redirectToProvider() | |
{ | |
return Socialite::driver('github')->redirect(); | |
} | |
/** | |
* Obtain the user information from GitHub. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function handleProviderCallback() | |
{ | |
$social = Socialite::driver('github')->user(); | |
$user = User::where(['email' => $social->getEmail()])->first(); | |
if(!$user) { | |
$user = User::create(['name' => $social->getName(), 'email' => $social->getEmail()]); | |
} | |
Auth::login($user); | |
return redirect()->to('/admin'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment