Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alexander_kraemer's avatar

Authentication in Laravel 5.1

So I started a new Project, and wanted to try out Laravel because everybody I know says that it's awesome! And I used my Playground to start something simple to get to know laravel better.

Now I've started my "real" Project, and I have problems with the Autentication. I'm a little bit of a perfectionist, so I first created my Databasestructure. I always add 3 letters of the tablename to every columnname as prefix, so that I have unique tablenames, and don't get problems when joining multiple tables :)

Register is working pretty fine, no problem there. After my Registration, I get logged in automatically, but when I logout and try to login The Error "These credentials do not match our records." occurs. So I tried to figure out, where the problem could be, but I didn't get any solution yet.

I hope someone here can help me out :)

My usertable structure is:

Schema::create('user', function (Blueprint $table) {
            $table->increments('usr_id')->unsigned();
            $table->string('usr_username');
            $table->string('usr_firstname');
            $table->string('usr_lastname');
            $table->string('usr_password', 60);
            $table->rememberToken();
            $table->timestamps();
        });

IF you need any further information, or you have any questions, Just ask :) Thank you! Greets, Alex

0 likes
16 replies
lrobi2015's avatar

Hi,

I am not very sure if I am understanding you correctly, but I think the problem has to do with what you named your username column => "usr_username". I believe that the authentication class it uses looks for columns called username and/or email.

alexander_kraemer's avatar

Yes that's right, but why is it working with successful register and then automated login? Is this login a different script?

And I've tried to adjust all the names of the database columnes in Laravel, i could find.

jekinney's avatar

Look in the config folder auth.php. And in your user model be sure you overide the primary key as it's not "id".

Protected $primarykey='user_id':

alexander_kraemer's avatar

yeah i changed that already... I don't think that there are problems with different columnnames, because otherwise Laravel would throw an error saying that there is no such column as "....". But it just tells me that the password i've given is not the same as in the database. But it's impossible, because I've tried like a hundred times :)

Maybe some problem with the hashing of the password?

lrobi2015's avatar

So you said that you have tried to change the column name from usr_username to username and it still did not work? Yes, double checking the password hash too is an excellent idea :), and remember that Laravel uses bcrypt for password encryption

jekinney's avatar

@lrobi2015 mentioned you might be double hashing the password. I find using setPasswrdAttribute in the model and removing any other hash and/or bycrpt anywhere else helps ensure it doesn't re-hash a hashed password.

thepsion5's avatar

The Authentication-related traits are hardcoded to look for a "password" column, as evidenced here, here, and here. Authentication will never work because it will be able to retrieve your Eloquent model, but will always compare the your submitted password to an empty attribute (since $user->password will always be null). You might be able to get around it by setting a custom getter on your model, but why go through the trouble just so you can prepend your columns with "usr_"?

alexander_kraemer's avatar

Is Laravel using a different login script after registration process than the normal login? sounds weird to me... Where does the password i insert get hashed? i couldn't find it in the postLogin method...

alexander_kraemer's avatar

Thank you all for your help!! I finally figured it out! I just had to overwrite the "getAuthPassword" method.

public function getAuthPassword()
    {
        return $this->usr_password;
    }

Took me a while to figure that out :)

Greets, and again thank you very much!!

Alex

1 like
martinbean's avatar

@alexander_kraemer Because Laravel expects columns and tables to be named a certain way. If you’re trying to go against that whilst also trying to learn the framework, then you’re just making things more difficult for yourself as not only are you going to have to learn the framework, but how to go against it’s conventions at the same time.

alexander_kraemer's avatar

hmmm yeah I think you may be right... maybe I'll continue with the standard-columnnames until i'm good with laravel :P

Thank you :)

Gianfranco90's avatar

Alexander_kraemer can you tell me where i can paste this function?.....i'm noob in laravel 5.1 and i have this problem.....but is very rarely because my login was perfectly and then i decided add one attribute to table then reset migrate and migrate the modified tablet.....then my login down....-.-

cristian9509's avatar

@alexander_kraemer I don't think you should name your columns like that. When you do joins and other stuff you could just use aliases for tables and you won't have any problems.

Example

Select * From users as usr
Left join addresses as addr ON addr.id = usr.address_id

1 like

Please or to participate in this conversation.