Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Laravel Socialite Github Login Demo
* Created by PhpStorm.
* User: Seth Phat
* Date: 5/4/2019
* Time: 3:41 PM
namespace App\Http\Controllers\Social;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Socialite;
class GithubAuthController extends Controller
const SERVICE = "github";
public function redirectLogin()
return Socialite::driver(static::SERVICE)->redirect();
public function handleCallback() {
try {
// get user information
* @var $user \Laravel\Socialite\Two\User
$user = Socialite::driver(static::SERVICE)->user();
// check if user registered or not in the system
$userDB = User::query()->where('email', $user->getEmail())->first();
// if not registered yet, create a new user record
if (empty($userDB)) {
$userDB = new User;
$userDB->name = $user->getName();
$userDB->email = $user->getEmail();
// more info if you have any
// if db error
if (!$userDB->save()) {
throw new \Exception('Create user failed due to DB Error...');
// ok, it saved. login now
// login to the system now
// redirect back to homepage with success
return redirect()
->with('success', 'Login successfully!');
} catch (\Exception $e) {
return redirect()
->with('error', 'Login via Github failed. Please try again.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.