Skip to content

Instantly share code, notes, and snippets.

@mostafa6765
Last active September 29, 2017 21:37
Show Gist options
  • Save mostafa6765/05b8514b44cbcde130c8936e4523726e to your computer and use it in GitHub Desktop.
Save mostafa6765/05b8514b44cbcde130c8936e4523726e to your computer and use it in GitHub Desktop.
Socialite laravel
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Contracts\Auth\Authenticatable;
use Socialite;
use App\User;
use 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 = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Redirect the user to the GitHub authentication page.
*
* @return Response
*/
public function redirectToProvider()
{
return Socialite::driver('github')->redirect();
}
/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback()
{
$SocialUser = Socialite::driver('github')->user();
$FindUser = User::where('email',$SocialUser->email)->first();
if($FindUser){
// check already auth socialite
Auth::login($FindUser);
return redirect('/home');
}else{
// retrive data from socialite
$user = new User;
$user->name = $SocialUser->name;
$user->email = $SocialUser->email;
$user->password =bcrypt('123456');
$user->save();
//Login Instance
$done = User::where('email',$SocialUser->email)->first();
// check already auth socialite
Auth::login($done);
return redirect('/home');
}
}
}
<? php
//socialite route
Route::get('socialite', 'Auth\LoginController@redirectToProvider')->name('socialite');
Route::get('socialite-login', 'Auth\LoginController@handleProviderCallback')->name('socialite.login');
Route::post('socialite-login', 'Auth\LoginController@handleProviderCallback')->name('socialite.login');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment