Last active
November 18, 2024 23:39
-
-
Save AnandPilania/43c9842276738a098ea9241985ba7559 to your computer and use it in GitHub Desktop.
LARAVEL: Social Auth via Socialite with Breeze
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
... | |
'facebook' => [ | |
'client_id' => env('FACEBOOK_CLIENT_ID'), | |
'client_secret' => env('FACEBOOK_CLIENT_SECRET'), | |
'redirect' => env('APP_URL').'/auth/facebook/callback', | |
], | |
'twitter' => [ | |
'client_id' => env('TWITTER_CLIENT_ID'), | |
'client_secret' => env('TWITTER_CLIENT_SECRET'), | |
'redirect' => env('APP_URL').'/auth/twitter/callback', | |
], | |
'google' => [ | |
'client_id' => env('GOOGLE_CLIENT_ID'), | |
'client_secret' => env('GOOGLE_CLIENT_SECRET'), | |
'redirect' => env('APP_URL').'/auth/google/callback', | |
], | |
... |
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\Models; | |
use Illuminate\Contracts\Auth\MustVerifyEmail; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Notifications\Notifiable; | |
class User extends Authenticatable | |
{ | |
use HasFactory, Notifiable; | |
protected $fillable = [ | |
'name', | |
'email', | |
'password', | |
'avatar', | |
]; | |
protected $hidden = [ | |
'password', | |
'remember_token', | |
]; | |
protected $casts = [ | |
'email_verified_at' => 'datetime', | |
]; | |
public function getNameAttribute($name = null): string | |
{ | |
return $this->using_provider | |
? $this->currentSocialite()->name | |
: $name; | |
} | |
public function currentSocialite() | |
{ | |
return $this->using_provider | |
? $this->socialite()->where('provider_name', $this->using_provider)->first() | |
: null; | |
} | |
public function socialite(): HasMany | |
{ | |
return $this->hasMany(Socialite::class); | |
} | |
public function getAvatarAttribute($avatar = null) | |
{ | |
return $this->using_provider | |
? $this->currentSocialite()->avatar | |
: ($avatar ?? '/img/placeholders/avatar.jpg'); | |
} | |
} |
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
... | |
Route::prefix('auth')->group(function () { | |
Route::get('/{provider?}', [SocialiteController::class, 'redirectToProvider']) | |
->name('auth.provider'); | |
Route::get('/{provider?}/callback', [SocialiteController::class, 'handleProviderCallback']) | |
->name('auth.callback'); | |
}); | |
... |
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
... | |
GOOGLE_CLIENT_ID= | |
GOOGLE_CLIENT_SECRET= | |
FACEBOOK_CLIENT_ID= | |
FACEBOOK_CLIENT_SECRET= | |
TWITTER_CLIENT_ID= | |
TWITTER_CLIENT_SECRET= | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment