Laravel 5.8 Social Login with multiple providers(using scaffold, no coding required)
Laravel supplies Socialite for OAuth Login, but there is no migration to storage the social account details, even less link multiple providers to a user.
Following “DRY”, the same code should only be written once by one person, then share it as a package, others just install it, no coding required.
Nobody has done it, so I do it.
Do you know Laravel official command to make the login page? it’s
php artisan make:auth
I write a similar command to make the socialite login page:
composer require sinkcup/laravel-make-auth-socialite
php artisan make:auth-socialite
php artisan migrate
This package has these features:
- Automatically generate database, pages, and routes for Laravel Socialite.
- Login with multiple providers using the same email will be determined as one user.
- When logged in, you can link all providers to the current user, and login with them next time.
Let’s experiment to verify it.
prepare
- a Google mail
- a GitHub account registered by this email
- a Facebook account registered by mobile number or another email.
steps
- Login with Google, then logout.
- Login with GitHub
- Link Facebook, then logout.
- Login with Facebook
expect
there should be 1 user in the users table.
config
change config/services.php
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
],
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_CALLBACK_URL'),
],
'facebook' => [
'client_id' => env('FACEBOOK_APP_ID'),
'client_secret' => env('FACEBOOK_APP_SECRET'),
'redirect' => env('FACEBOOK_VALID_OAUTH_REDIRECT_URI'),
],
change .env
AUTH_SOCIAL_LOGIN_PROVIDERS=Google,GitHub,Facebook
# GitHub allows localhost(need to submit in the Google APIs console)
GOOGLE_CLIENT_ID=123-asdf.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=secret
GOOGLE_REDIRECT_URI=http://localhost/login/google/callback
# GitHub allows localhost(need to submit in the Developer settings)
GITHUB_CLIENT_ID=foo
GITHUB_CLIENT_SECRET=bar
GITHUB_CALLBACK_URL=http://localhost/login/github/callback
# Facebook allows localhost(no need to submit), it's very friendly to developers
FACEBOOK_APP_ID=123456
FACEBOOK_APP_SECRET=secret
FACEBOOK_VALID_OAUTH_REDIRECT_URI=http://localhost/login/facebook/callback
visiting the Login page in a browser, you can see everything is OK:
If you have any question, create an issue in the repo: github.com/sinkcup/laravel-make-auth-socialite