How to implement multiple authentication drivers

Asked

Viewed 136 times

0

I am building a system where it is necessary to have two types of authentication, the first referring to ordinary users, who will access the front of the store, and the second for administrators, who will access the administration environment.

The problem: by default the Laravel 5 has only one authentication driver, which enables ordinary users logged in in front of the store to access the administration environment, and vice versa.

Identifying what is user and administrator is even easy with a level system (roles), but is not as effective as splitting these sessions.

How can I implement a second authentication session for administrators only? for example: Auth::admin()

  • Why the use of roles is not effective in this case?

1 answer

1


You can "emulate" a new Auth Class.

The Laravel Auth component is basically the Illuminate Auth Guard class, and this class have some dependencies.

So basically you have to create a new Guard class...

class Cilentguard extends Authguard {

public function getName()
{
    return 'login_' . md5('ClientAuth');
}

public function getRecallerName()
{
    return 'remember_' . md5('ClientAuth');
    }
}

And a Serviceprovider to initialize the class, passing the dependencies.

<?php 

use Illuminate\Support\ServiceProvider;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Auth\Reminders\PasswordBroker;
use Illuminate\Auth\Reminders\DatabaseReminderRepository;
use ClientGuard;
use ClientAuth;

class ClientServiceProvider extends ServiceProvider 
{

    public function register()
    {
        $this->registerAuth();
        $this->registerReminders();
    }

    protected function registerAuth()
    {
        $this->registerClientCrypt();
        $this->registerClientProvider();
        $this->registerClientGuard();
    }

    protected function registerClientCrypt()
    {
        $this->app['client.auth.crypt'] = $this->app->share(function($app)
        {
            return new BcryptHasher;
        });
    }

    protected function registerClientProvider()
    {
        $this->app['client.auth.provider'] = $this->app->share(function($app)
        {
            return new EloquentUserProvider(
                $app['client.auth.crypt'], 
                'Client'
            );
        });
    }

    protected function registerClientGuard()
    {
        $this->app['client.auth'] = $this->app->share(function($app)
        {
            $guard = new Guard(
                $app['client.auth.provider'], 
                $app['session.store']
            );

            $guard->setCookieJar($app['cookie']);
            return $guard;
        });
    }

    protected function registerReminders()
    {
        # DatabaseReminderRepository
        $this->registerReminderDatabaseRepository();

        # PasswordBroker
        $this->app['client.reminder'] = $this->app->share(function($app)
        {
            return new PasswordBroker(
                $app['client.reminder.repository'], 
                $app['client.auth.provider'], 
                $app['redirect'], 
                $app['mailer'], 
                'emails.client.reminder' // email template for the reminder
            );
        });
    }

    protected function registerReminderDatabaseRepository()
    {
        $this->app['client.reminder.repository'] = $this->app->share(function($app)
        {
            $connection   = $app['db']->connection();
            $table        = 'client_reminders';
            $key          = $app['config']['app.key'];

            return new DatabaseReminderRepository($connection, $table, $key);
        });
    }

    public function provides()
    {
        return array(
            'client.auth', 
            'client.auth.provider', 
            'client.auth.crypt', 
            'client.reminder.repository', 
            'client.reminder', 
        );
    }
}

In this Service Provider, I put some example of how to create a 'new' component to remember the password. Now you need to create two new stabs, one for authentication and the other to remember the password.

<?php 
use Illuminate\Support\Facades\Facade;

class ClientAuth extends Facade
{

    protected static function getFacadeAccessor() 
    {
        return 'client.auth';
    }
}

And

<?php 
use Illuminate\Support\Facades\Facade;

class ClientPassword extends Facade
{

    protected static function getFacadeAccessor() 
    {
        return 'client.reminder';
    }
}

Of course, to remember password, you need to create the table in the database in order to work. In this example, the table name should be client_reminders, as you can see in the registerReminderDatabaseRepository method in the Service Provider. The table structure is the same as the original table. After that, you can use your Clientauth in the same way as you use the Auth class. And the same thing for Clientpassword with Password Class.

ClientAuth::gust();
ClientAuth::attempt(array('email' => $email, 'password' => $password));

ClientPassword::remind($credentials);

Don’t forget to add your service preview to the service list providers in the file app/config/app.php.

  • Friend, just a question. Where should I put this new Guard Class? in case, Cilentguard.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.