Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

tighten/novapackages

336 stars
3 code files
View tighten/novapackages on GitHub

composer.json

Open in GitHub
{
//
"require": {
"php": "^7.4",
//
"laravel/socialite": "^5.2"
},
//
}

routes/web.php

Open in GitHub
use Illuminate\Support\Facades\Route;
 
//
Route::get('login/github', 'Auth\LoginController@redirectToProvider')->name('login');
Route::get('login/github/callback', 'Auth\LoginController@handleProviderCallback');
//

app/Http/Controllers/Auth/LoginController.php

Open in GitHub
use App\Events\NewUserSignedUp;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
 
class LoginController extends Controller
{
//
public function redirectToProvider()
{
session()->flash('url.intended', url()->previous());
 
return Socialite::driver('github')->redirect();
}
 
public function handleProviderCallback()
{
$user = $this->createOrUpdateUser(Socialite::driver('github')->user());
 
if ($user->wasRecentlyCreated) {
event(new NewUserSignedUp($user));
}
 
Auth::login($user, false);
 
return redirect()->intended();
}
 
private function createOrUpdateUser($socialiteUser)
{
if (is_null($user = User::forSocialiteUser($socialiteUser))) {
return User::create($this->socialiteUserAttributes($socialiteUser));
}
 
$user->update($this->socialiteUserAttributes($socialiteUser));
 
return $user;
}
 
private function socialiteUserAttributes($socialiteUser)
{
return [
'name' => $socialiteUser->getName() ?: $socialiteUser->getNickname(),
'email' => $socialiteUser->getEmail(),
'avatar' => $socialiteUser->getAvatar(),
'github_username' => $socialiteUser->getNickname(),
'github_user_id' => $socialiteUser->getId(),
];
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.