upnorthal's avatar

5.2 - Auth - Multiple Tables

Given version 5.2 now supports multiple auth tables, I though I would give this a go.

I have created an additional table called person.

I've modified auth.php, but adding a new persons source.

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],

         'persons' => [
             'driver' => 'eloquent',
             'model' => 'App\Person::class',
         ],

I have created new routes and controllers for my persons table (I've also created some new views)

Route::group(['middleware' => ['web']], function () {

    // Authentication routes...
    Route::get('personauth/login', 'PersonAuth\PersonAuthController@getLogin');
    Route::post('personauth/login', 'PersonAuth\PersonAuthController@postLogin');
    Route::get('personauth/logout', 'PersonAuth\PersonAuthController@getLogout');

    // Registration routes...
    Route::get('personauth/register', 'PersonAuth\PersonAuthController@getRegister');
    Route::post('personauth/register', 'PersonAuth\PersonAuthController@postRegister');


});

Within my PersonAuthController, I have added a method to allow new person users to be created (just overriding the same method from the trait RegistersUsers)

(also I've over-ridden the methods that show the login and register view to point to my new templates)

    protected function create(array $data)
    {
        return Person::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }


    public function showRegistrationForm()
    {
        return view('personauth.register');
    }

   public function showLoginForm()
    {
        if (view()->exists('personauth.authenticate')) {
            return view('personauth.authenticate');
        }

        return view('personauth.login');
    }

I can create new person users fine.

How am I supposed to authenticate against my new person table? Looking in the AuthenticatesUsers trait I don't get what I am supposed to change in the method login ?

I think this is the bit that does the actual auth check

   if (Auth::attempt($credentials, $request->has('remember'))) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

How do I tell this to query against my person table?

0 likes
8 replies
upnorthal's avatar

okay, I should have read the manual! seems I just needed to reference a guard - which I have yet to work out how to define

       if (Auth::guard('persons')->attempt($credentials, $request->has('remember'))) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }
TaylorOtwell's avatar
Level 5

If you're going to use the typical session guard, define a new guard in your authentication configuration. If you want the default AuthenticatesUsers trait to use this guard for its login, you need to set the new guard as the default as well at the top of the configuration file.

5 likes
upnorthal's avatar

Fantastic, thanks Taylor.

RE: ". If you want the default AuthenticatesUsers trait to use this guard for its login, you need to set the new guard as the default as well at the top of the configuration file."

What if I want the normal user table authentication to work in parallel with my new person's table? (so I will still need the standard 'web'guard to remain)

Is there some way I can override the default in my PersonAuthController?

upnorthal's avatar

For anyone reading. ignore my last comment please - sorry, the answer is do nothing - user and persons auth both work together!

Within your view templates (and anywhere else I guess), where you reference Auth facade, simply bolt on the guard.

   <!-- Right Side Of Navbar -->
                <ul class="nav navbar-nav navbar-right">
                    <!-- Authentication Links -->

                    @if (Auth::guard('persons')->guest())                                  <!-- ******* Specify the guard('value') here -->

                        <li><a href="personauth/login">Login</a></li>
                        <li><a href="personauth/register">Register</a></li>
                    @elseif 
2 likes
AshAsley's avatar

hi upnorthal.if you are able to do the multi auth correctly can you help me too. Can you show the personAuthController and evry other configuration you have done for two different tables of users i.e users and person

arslan2037's avatar

I have working Laravel 5.2 Authentication for two tables but i need for three tables can anyone help me to do this, If anyone need it email me m.arslan2037@gmail.com

iameric's avatar

Gentlemen,

I have had my config/auth.php updated with respective guard and correct model and I have successfully created any new users from a custom registration form. And every new user that successfully registered, redirected to correct path and logged in. However, when any registered user tries to login again, they fail.

My table for handling user information is called customers. I have created a model using php artisan make:model customers.

Thing i have done so far:

  1. edit config/auth.php, under provider, users, i change default model from App\User::class into App\customer::class

  2. update AuthController.php with: protected $guard = 'web'; which is still default since i have modified the providers value.

  3. in my model, customers.php, i change customers extends Authenticatable. and at the top, i add this Use Illuminate\Foundation\Auth\Customers as Authenticatable;

  4. Add a new file under laravel/framework/src/illuminate/foundation/auth/Customers.php with the same content as User.php, only that i modify the class name from User extends Model into customers extends Model

I wonder if i missed out something...

[Update] Gentlemen,

My 'Password' field in my table is capital 'P', and when I change my table into 'password', it works... And I found that you can change the password field into whatever name you like but you have to override an existing laravel getAuthPassword() method by inserting this method under your model that extends Authenticatable

[code] public function getAuthPassword() { return $this->password; // change to whatever password field here } [/code]

Hopefully my explanation helps CMIIW Thanks

ivanngdlc's avatar

A question. You can create Multiple Authentication with several tables and have a single route?, That is to say that it is not this way. Http://localhost/admin/login and http://localhost/user/login In the place of that having only one route of login: Http://localhost/all_users/login How would you do?

Please or to participate in this conversation.