2017 © Pedro Peláez
 

library laravel5_multiauth

Multiple auth driver for Laravel 5

image

chientd/laravel5_multiauth

Multiple auth driver for Laravel 5

  • Sunday, November 6, 2016
  • by tdchien
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 127 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

IMPORTANT: Laravel 5.1 and onward

Please head over to https://github.com/Kbwebs/MultiAuth as a replacement. kbwebs' fork is fully compatible with Laravel 5.1. Moving from my for to this should not need any changes in your code., (*1)

Laravel Multi Auth

  • Laravel: 5
  • Author: Ramon Ackermann
  • Author Homepage: https://github.com/sboo
  • Author: Ollie Read
  • Author Homepage: http://ollieread.com

For Laravel 4.2 version, see https://github.com/ollieread/multiauth, (*2)


IMPORTANT: Laravel 5.1 Default AuthController with its traits, (*3)

\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers

more specifically, (*4)

Illuminate\Foundation\Auth\AuthenticatesUsers::postLogin()

does not work! For the time being, you need to write your own Controller for this. I will work on it if I have time., (*5)


This package is not a replacement for laravels default Auth library, but instead something that sits between your code and the library., (*6)

Think of it as a factory class for Auth. Now, instead of having a single table/model to authenticate users against, you can now have multiple, and unlike the previous version of this package, you have access to all functions, and can even use a different driver for each user type., (*7)

On top of that, you can use multiple authentication types, simultaneously, so you can be logged in as a user, a master account and an admin, without conflicts!, (*8)

Custom Auth Drivers

At this current moment in time, custom Auth drivers written for the base Auth class will not work. I'm currently looking into this particular issue but for the meantime, you can work around this by changing your closure to return an instance of Ollieread\Multiauth\Guard instead of the default., (*9)

Installation

Firstly you want to include this package in your composer.json file., (*10)

    "require": {
            "sboo/multiauth" : "4.0.*"
    }

Now you'll want to update or install via composer., (*11)

composer update

Next you open up app/config/app.php and replace the 'Illuminate\Auth\AuthServiceProvider', with, (*12)

    'Ollieread\Multiauth\MultiauthServiceProvider',

and 'Illuminate\Auth\Passwords\PasswordResetServiceProvider' with, (*13)

    'Ollieread\Multiauth\Passwords\PasswordResetServiceProvider',

NOTE It is very important that you replace the default service providers., (*14)

Remove the original database migration for password_resets., (*15)

Configuration

is pretty easy too, take config/auth.php with its default values:, (*16)

    return [

        'driver' => 'eloquent',

        'model' => 'App\User',

        'table' => 'users',

        'password' => [
                'email' => 'emails.password',
                'table' => 'password_resets',
                'expire' => 60,
            ],

    ];

Now remove the first three options (driver, model and table) and replace as follows:, (*17)

    return [

        'multi' => [
            'admin' => [
                'driver' => 'eloquent',
                'model' => 'App\Admin',
            ],
            'client' => [
                'driver' => 'database',
                'table' => 'clients',
                'email' => 'client.emails.password',
            ]
        ],

        'password' => [
                'email' => 'emails.password',
                'table' => 'password_resets',
                'expire' => 60,
            ],

    ];

This is an example configuration. Note that you will have to create Models and migrations for each type of user. Use App\User.php and 2014_10_12_000000_create_users_table.php as an example., (*18)

If you wish to use a reminders email view per usertype, simply add an email option to the type, as shown in the above example., (*19)

To generate the reminders table you will need to run the following command., (*20)

php artisan multiauth:resets-table

Likewise, if you want to clear all reminders, you have to run the following command., (*21)

php artisan multiauth:clear-resets

You will also need to change the existing default Laravel 5 files to accommodate multiple auth and password types. Do as described in this gist:, (*22)

https://gist.github.com/sboo/10943f39429b001dd9d0, (*23)

Usage

Everything is done the exact same way as the original library, the one exception being that all method calls are prefixed with the key (account or user in the above examples) as a method itself., (*24)

    Auth::admin()->attempt(array(
        'email'     => $attributes['email'],
        'password'  => $attributes['password'],
    ));
    Auth::client()->attempt(array(
        'email'     => $attributes['email'],
        'password'  => $attributes['password'],
    ));
    Auth::admin()->check();
    Auth::client()->check();

I found that have to call the user() method on a user type called user() looked messy, so I have added in a nice get method to wrap around it., (*25)

    Auth::admin()->get();

In the instance where you have a user type that can impersonate another user type, example being an admin impersonating a user to recreate or check something, I added in an impersonate() method which simply wraps loginUsingId() on the request user type., (*26)

    Auth::admin()->impersonate('client', 1, true);

The first argument is the user type, the second is the id of said user, and the third is whether or not to remember the user, which will default to false, so can be left out more often than not., (*27)

And so on and so forth., (*28)

There we go, done! Enjoy yourselves., (*29)

Testing

Laravel integration/controller testing implements $this->be($user) to the base TestCase class. The implementation of #be() does not work correctly with Multiauth. To get around this, implement your own version of #be() as follows:, (*30)

    public function authenticateAs($type, $user) {
      $this->app['auth']->$type()->setUser($user);
    }

License

This package inherits the licensing of its parent framework, Laravel, and as such is open-sourced software licensed under the MIT license, (*31)

The Versions

06/11 2016

dev-master

9999999-dev

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read
by Chien Tran

laravel auth laravel 5 multi

23/12 2015

dev-develop

dev-develop

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi

16/06 2015

v4.0.9

4.0.9.0

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi

23/05 2015

v4.0.8

4.0.8.0

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi

13/04 2015

v4.0.7

4.0.7.0

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi

01/04 2015

v4.0.6

4.0.6.0

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi

31/03 2015

v4.0.5

4.0.5.0

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi

28/03 2015

v4.0.4

4.0.4.0

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi

28/03 2015

v4.0.3

4.0.3.0

Multiple auth driver for Laravel 5

  Sources   Download

MIT

The Requires

 

by Ollie Read

laravel auth laravel 5 multi