coloredscience / laravel-auth
Colored ScienceProvider for Laravel Socialite
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: ^5.6 || ^7.0
- socialiteproviders/manager: ~2.0 || ~3.0
This package is auto-updated.
Last update: 2023-03-29 01:17:11 UTC
README
This is an extended provider of Laravel Socialite for ColoredScience authentication using the SocialiteProviders Manager.
Installation
composer require laravel/socialite
composer require coloredscience/laravel-auth
Create your app on Colored User to obtain API keys.
Configuration
-
Add a configurable service in
config\services.php
for Laravel Socialite connection:'coloredsci' => [ 'client_id' => env('CS_CLIENT_ID'), 'client_secret' => env('CS_CLIENT_SECRET'), 'redirect' => env('APP_URL').env('CS_REDIRECT'), ],
-
Remove
Laravel\Socialite\SocialiteServiceProvider
from yourproviders[]
array inconfig\app.php
if you have added it already. -
Register this provider instead
'providers' => [ // a whole bunch of providers // remove 'Laravel\Socialite\SocialiteServiceProvider', \SocialiteProviders\Manager\ServiceProvider::class, // add ];
-
Add
SocialiteProviders\Manager\SocialiteWasCalled
event to yourlisten[]
array inapp/Providers/EventServiceProvider
-
Add the Colored Science listener to the
SocialiteProviders\Manager\SocialiteWasCalled[]
that you just createdprotected $listen = [ \SocialiteProviders\Manager\SocialiteWasCalled::class => [ 'ColoredScience\\LaravelAuth\\ColoredScienceExtendSocialite@handle', ], ];
-
Create your Authentication Controller to manage the authentication flow
php artisan make:controller Auth\\CSAuthController
-
Register your Auth Routes
Route::get('/login', 'Auth\CSAuthController@login' )->name( 'login' ); Route::get('/callback', 'Auth\CSAuthController@handleCallback' )->name( 'callback' ); Route::get('/logout', 'Auth\CSAuthController@logout' )->name( 'logout' )->middleware('auth');
-
Create a login and logout function to handle the CS Socialite authentication and logout, more usage options are available via Laravel Socialite
public function login() { return Socialite::with('coloredsci')->redirect(); } public function logout() { \Auth::logout(); return \Redirect::intended('/'); }
-
Create a callback function to handle the redirect callback, more usage options are available via Laravel Socialite
public function handleCallback() { $user = Socialite::driver('coloredsci')->user(); //optional $authUser = $this->updateOrCreateUser($user); Auth::login($authUser, true); return redirect('/home'); } //optional public function updateOrCreateUser($user) { return $authUser = User::updateOrCreate( ['email' => $user->email], [ 'fname' => $user->fname, 'lname' => $user->lname, 'nickname' => $user->nickname, 'avatar' => $user->avatar, 'phone' => $user->phone, 'email_verified_at' => $user->email_verified_at, ] ); }