Last active
September 29, 2017 21:37
-
-
Save mostafa6765/05b8514b44cbcde130c8936e4523726e to your computer and use it in GitHub Desktop.
Socialite laravel
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 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'); | |
} | |
} | |
} |
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 | |
//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