firmantr3 / laravel-sso
Laravel Single Sign On (SSO) authenticatable credentials implementation.
Requires
- illuminate/auth: ^5.0|^6.0
- illuminate/container: ^5.0|^6.0
- illuminate/contracts: ^5.0|^6.0
- illuminate/database: ^5.0|^6.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
README
Create just one unique credential for multiple authenticatable users that has different eloquent model.
Introduction
Given you need to implement a school management application, that have 3 different user: admins, teachers, students. Sometimes, admins can be teacher as well, how if the email and password don't need to be different? Well, Laravel SSO to the rescue!
Installation
composer require firmantr3/laravel-sso
Publish migration by running artisan vendor:publish
:
php artisan vendor:publish
Configuration
Update your laravel auth config: /config/auth.php
, and use sso
provider, like so:
'providers' => [ 'admins' => [ 'driver' => 'sso', 'model' => App\Admin::class, ], 'teachers' => [ 'driver' => 'sso', 'model' => App\Teacher::class, ], 'students' => [ 'driver' => 'sso', 'model' => App\Student::class, ], ],
You can use preset Credential model: Firmantr3\LaravelSSO\Models\Credential
. You can also create your own Credential
model class that extends from Firmantr3\LaravelSSO\Models\Credential
so you can customize the child relations, and then update your class namespace in config/sso.php
.
Sample Usage
Your user models must have credential_id
AND Laravel's default user attributes like remember_token
, check your user model migration. The credential_id
should be unique for each models, to prevent duplication.
User model's class must extend Firmantr3\LaravelSSO\Models\User
like so:
<?php namespace App\Admin; use Firmantr3\LaravelSSO\Models\User; class Admin extends User { public $timestamps = false; protected $table = 'admins'; }
You can attach existing users using attachUser
method on a Credential model like so:
$credential = Firmantr3\LaravelSSO\Models\Credential::first(); $admin = $credential->attachUser(\App\Admin::create());
Or you can use createAuthenticatableUser
method on a Credential:
$adminClass = \App\Admin::class; $adminAttributes = []; $admin = $credential->createAuthenticatableUser($adminClass, $adminAttributes);
Test
vendor/bin/phpunit