PHP code example of bogardo / multiauth

1. Go to this page and download the library: Download bogardo/multiauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bogardo / multiauth example snippets


'driver'    => 'multiauth'

'multiauth' => [
 
    'identifier_key' => 'email',

    'entities' => [
        [
            'type'       => 'user',
            'table'      => 'users',
            'model'      => 'User',
            'identifier' => 'username'
        ],
        [
            'type'       => 'administrator',
            'table'      => 'admins',
            'model'      => 'Admin',
            'identifier' => 'email'
        ]
    ]
]

use Bogardo\Multiauth\User\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface
{

    use UserTrait, RemindableTrait;

    public $authtype = 'user';
    
}

use Bogardo\Multiauth\User\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Admin extends Eloquent implements UserInterface, RemindableInterface
{

    use UserTrait, RemindableTrait;

    public $authtype = 'administrator';
    
}

multiAuthUnique

multiAuthUnique:user,2

Validator::make(
    ['email' => '[email protected]'], // data array
    ['email' => 'multiAuthUnique']    // rules array
);

Validator::make(
    ['email' => '[email protected]'],              // data array
    ['email' => 'multiAuthUnique:administrator,5'] // rules array
);

/** @var Bogardo\Multiauth\Service $service */
$service = App::make('multiauth.service');

$entities = $service->getEntities();

// $entities Output
object(Bogardo\Multiauth\Entity\EntityCollection)
  protected 'items' => 
    array (size=2)
	   0 => 
        object(Bogardo\Multiauth\Entity\Entity)
          public 'type' => string 'user'
          public 'table' => string 'users'
          public 'model' => string 'User'
          public 'identifier' => string 'username'
      1 => 
        object(Bogardo\Multiauth\Entity\Entity)
          public 'type' => string 'administator'
          public 'table' => string 'admins'
          public 'model' => string 'Admin'
          public 'identifier' => string 'email'
      


$entity = $service->getEntityByType('user');

// $entity Output
object(Bogardo\Multiauth\Entity\Entity)
  public 'type' => string 'user'
  public 'table' => string 'users'
  public 'model' => string 'User'
  public 'identifier' => string 'username'



/** @var Bogardo\Multiauth\Service $service */
$service = App::make('multiauth.service');
$types = $service->getEntities()->lists('type');

foreach ($types as $type) {
    Route::filter('multiauth.' . $type, function($route, $request) use ($type) {

        if (Auth::guest()) {
            // The user is not logged in.
            return Redirect::to('/');
        }

        if (Auth::user()->authtype !== $type) {
            // The user is logged in, but is not of correct type
            return Redirect::to('/');
        }
    });
}

Route::get('admin', ['before' => 'multiauth.administrator', function() {
    echo 'Admins Only!!';
}]);

Auth::loginUsingId('administrator.2');

Auth::onceUsingId('user.54')