chuoke / laravel-user-identify
Laravel multi identifier auth provider to thin user model/table.
Fund package maintenance!
:vendor_name
Requires
- php: ^8.0
- spatie/laravel-package-tools: ^1.16.4
Requires (Dev)
- nunomaduro/collision: ^8.4.0
- orchestra/testbench: ^9.3.0
- pestphp/pest: ^2.35.1
- pestphp/pest-plugin-laravel: ^2.4.0
- vimeo/psalm: ^5.25.0
README
Why this?
In-depth learning and understanding about Laravel by transforming its features.
Nowadays, there are many ways to log in, especially social login has become very popular. Usually we would add login identifiers to the users
table, and while this is makes the use of this identification information and access to Laravel's existing logic simple. But this makes the users
table very bloated, and these login identifiers are usually of little use in business logic. In order to simplify the users
table and More convenient way to add login ways. I created this package.
With the help of this package, my user
table is concise.
CREATE TABLE `users` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `avatar` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) )
The users
table's email
, password
, remember_token
, and any social login ids are converted into the identifier credentials table user_identifiers
.
If you need to add other authentication identifier, just add one record.
Installation
Not fully available !!!!
At present, I only use a some login method based on this package, welcome to improve it.
You can install the package via composer:
composer require chuoke/laravel-user-identify
php artisan vendor:publish --provider="Chuoke\UserIdentify\UserIdentifyServiceProvider"
You can publish and run the migrations with:
php artisan vendor:publish --tag="user-identify-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="user-identify-config"
Config
This is the contents of the published config file:
In config/user-identify.php
:
return [ 'idetifier_model' => Chuoke\UserIdentify\Models\UserIdentifier::class, 'idetifier_table' => 'user_identifiers', 'idetifier_user_key' => 'user_id', 'user_model' => App\Models\User::class, 'user_key' => 'id', 'auth_provider_name' => 'user_identify', 'actions' => [ 'user_save_from_socialite' => \Chuoke\UserIdentify\Actions\UserSaveFromSocialite::class, ], ];
Add user_identify
guard provider to auth.php
config file like this:
In config/auth.php
:
return [ 'defaults' => [ 'guard' => env('AUTH_GUARD', 'web'), 'passwords' => env('AUTH_PASSWORD_BROKER', 'users'), ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'user_identify', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'user_identify' => [ 'driver' => config('user-identify.auth_provider_name', 'user_identify'), ], ], // ... ];
Next, register auth provider:
In AuthServiceProvider.php
:
use Chuoke\UserIdentify\UserIdentifyProvider; use Illuminate\Contracts\Foundation\Application; class AppServiceProvider extends ServiceProvider { public function boot(): void { Auth::provider(config('user-identify.auth_provider_name'), function (Application $app, array $config) { return new UserIdentifyProvider( $app['hash'], config('user-identify.user_model'), config('user-identify.idetifier_model'), ); }); } }
Usage
Sign in with GitHub account.
<?php namespace App\Web\Controllers\Auth; use Illuminate\Http\Request; use App\Web\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Laravel\Socialite\Facades\Socialite; class GithubController extends Controller { public function redirect(Request $request) { return Socialite::driver('github')->redirect(); } public function callback(Request $request) { /** @var \Laravel\Socialite\AbstractUser $githubUser */ $githubUser = Socialite::driver('github')->user(); $githubUser['socialite_type'] = 'github'; $successful = Auth::attempt([ 'identifier' => $githubUser, ], true); if ($successful) { return redirect('/dashboard'); } return redirect('/login'); } }
If the user does not exist, it is automatically created.
And you can see the source code for more details.
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.