webservices-ca@outloook.com's avatar

Laravel authenticates a user then redirects to the login page

I have a weird issue with Laravel 6. I followed all the directions to scaffold the authentication system and it works fine. But when I use a different table to authenticate against (along with a customized registration form) the system does not act as predicted.

web.php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Cats.php

class Cats extends Authenticatable
...

However, when I change the Users database to a different table I am not able to log in.

Symptoms:

  • I log in using a known account and a bad password. Result: These credentials do not match our records.
  • I log in using a known account and good password. Result: [I am redirected back to the login page]
  • I log in using a known account and good password. Result: [I am redirected back to the login page]
  • I log in using a known account and good password and select remember token. Result: [I am redirected back to the login page and the remember_token column in the table is populated]

Can someone point me in the right direction. Any help would be appreciated.

0 likes
2 replies
jlrdw's avatar

But when I use a different table to authenticate against (along with a customized registration form) the system does not act as predicted.

Of course it doesn't, it's expecting the uses table.

You need to write some customized code if you're going to use another table.

You're going to lose neat stuff built in like $user = Auth::user(); also.

You probably need to decide to use built in or customize your own.

webservices-ca@outloook.com's avatar

I did some digging by setting up two differenet Laravel instances. The first worked out of the box. The second worked up until the point were I modified the Cats.php model.

The modified Cats.php model had the following primary key variable set:

    protected $primaryKey = 'kittykat_id';

Subsequent logins after the change resulted in the login process redirecting me back to the login page. Both instances worked as designed when there was no modifications to the primaryKey.

When I removed this protected primary key variable I was able to log in to the users table as well as any other table I created to store cats (aka users) and their passwords.

Adding the following protected key to my Cats.php class confirmed the above.

protected $primaryKey = 'id';

So by default the Authenticatable.php class is using avendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php Class to retrieve the primaryKey

Is there any way to use a different primaryKey database column for authentication AND still use Eloquent?

Please or to participate in this conversation.