matthanley/laravel-polyauth

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Polymorphic Authentication for Laravel

dev-master 2018-08-24 18:53 UTC

This package is not auto-updated.

Last update: 2021-11-27 14:46:06 UTC


README

Allows authentication using multiple User models in Laravel 5.

Installation

Installation is performed via Composer:

composer require matthanley/laravel-polyauth

Configuration

Register the auth driver by adding the following to the boot() method in app/Providers/AuthServiceProvider.php:

\Auth::provider('polymorphic', function ($app) {
    return $app->make(\PolyAuth\Providers\UserProvider::class);
});

Update config/auth.php to set your auth driver to polymorphic and define the models with which you wish to authenticate:

'providers' => [
    'users' => [
        'driver' => 'polymorphic',
        'models' => [
            App\User::class,
            App\Admin::class,
        ],
    ],
],

Ensure your user models are using globally unique identifiers and add events to enforce globally unique emails/usernames etc. For example:

/**
 * Migrations
 */

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->unique();
            $table->primary('id');
            /* ... */
        });
    }
}

/**
 * Models
 */

use Illuminate\Support\Facades\Auth;
use Ramsey\Uuid\Uuid;

class User extends Authenticatable
{

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            // Generate an ID
            $model->setAttribute($model->getKeyName(), Uuid::uuid1()->toString());
            // Make sure email is globally unique
            if (Auth::getProvider()->retrieveByCredentials(['email' => $model->email])) {
                throw new \Exception();
            }
        });
    }

    /* ... */
}