This is the official repository Let’s Build a GitHub Login Authentication in Laravel (Laravel, Socialite, Breeze)
• Twitter: @codewithdary
• Instagram: @codewithdary
• TikTok: @codewithdary
• Blog: @codewithdary
• Patreon: @codewithdary
Setup the repository
git clone https://github.com/codewithdary/laravel-socialite-github-login.git
cd laravel-socialite-github-login.git
composer install
cp .env.example .env
php artisan key:generate
php artisan cache:clear && php artisan config:clear
php artisan serve
The following pieces of code have been added into this (empty) repository.
composer require laravel/breeze --dev
php artisan breeze:install --dark
php artisan migrate
npm install && npm run dev
composer require laravel/socialite
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('nickname')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('github_id')->nullable();
$table->string('auth_type')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
// App\Models\User.php
protected $fillable = [
'name',
'nickname',
'email',
'password',
'github_id',
'auth_type'
];
// login.blade.php
<div class="text-center">
<a href="{{ route('github.login') }}">
<img
src="https://icon-library.com/images/github-icon-white/github-icon-white-6.jpg"
alt="GitHub icon"
width="50px"
class="mx-auto scale-100 hover:scale-125 ease-in duration-200"
/>
</a>
</div>
// config\services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_CALLBACK_URI'),
],
Route::get('auth/github', [GitHubController::class, 'redirect'])->name('github.login');
Route::get('auth/github/callback', [GitHubController::class, 'callback']);
public function redirect()
{
return Socialite::driver('github')->redirect();
}
public function callback()
{
try {
$user = Socialite::driver('github')->user();
$gitUser = User::updateOrCreate([
'github_id' => $user->id,
],[
'name' => $user->name,
'nickname' => $user->nickname,
'email' => $user->email,
'github_token' => $user->token,
'auth_type' => 'github',
'password' => Hash::make(Str::random(10))
]);
Auth::login($gitUser);
return redirect()->route('dashboard');
} catch(\Exception $e) {
ray($e->getMessage());
}
}