jfurnas's avatar

Custom 'Auth' controller

Hello there. I have been interested in learning Laravel, and I finally jumped into it. I have been reading over the documentation trying to find out how Laravel handles Authentication, and I am a bit confused. The documentation doesn't seem to provide much information regarding this.

I have an already populated database that uses a custom table structure for storing login credentials. I am looking to write (or extend) the default Auth controller to authorize users based on the structure of this table, not the default one that Laravel uses.

Would I be better suited writing my own Authentication controller, and not using Laravel's, or would I be better off just extending Laravel's authentication controller to suite my needs?

Also, is there any better documentation on this process in particular? I am not much of a read and learn type learner, I learn most effectively by jumping in and doing stuff hands-on, so any guides that focus on doing instead of reading would be really helpful.

I did come across guides similar to this: http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver but this appears to modify the driver, without actually handling the validation and processing of the authentication.

Thank you!

0 likes
6 replies
sutherland's avatar

One of my Laravel sites came over from a WordPress site with many users, so I created a custom UserProvider that extends the default one and only overrides the validateCredentials() method to check if the password saved for the user was hashed using WordPress so it can check if the password is correct using that algorithm instead.

The controllers in app/Http/Controller/Auth/ are there for you to modify. I wouldn't write something totally new but instead look at the traits they use and override anything you need.

What specific default Laravel behavior isn't compatible with the data you have?

jfurnas's avatar

Keep in mind I am not very experienced with Laravel, at all, this is just going from what I seem to be understanding from reading the documentation and looking at other laravel installs.

It appears that laravel uses it's own custom table, stores username, password, session id, etc in this table, and also writes to it during registration. Although the table its looking up could easily be modified, the writing to the table from laravel would need to be changed completely, as there are a lot more columns that would need to be written during registration than what laravel provides, and the session handling is vastly different.

For example, my system uses an email address and a specialized password hash for authentication, not like the one Laravel uses, as well as checking the values of some more data in the table, too. I am just not sure how to extend it enough to allow for complete workability with the current table structure.

sutherland's avatar
Level 28

For checking your existing password hashes, writing a custom user provider should do the trick.

As for writing extra columns during registration, the included app/Http/Controllers/Auth/RegisterController.php has a validator and create method you can just modify in place to add the extra fields you need and to hash the password using your own method.

I don't think you'll have too much trouble. The only thing I don't have experience with is customizing the session driver, but once getting a feel of how Laravel does things I'm sure you can get it going.

1 like
jfurnas's avatar

I appreciate it. I think the biggest thing, is nothing outside of specific parameters can be written to the account table. (For example, a new column or columns for handling session IDs, etc). I am writing a web interface for a game that a friend of mine is developing, and the system will break if the columns dont match what it's expecting.

The solution to that, is going to have to be an independent session table that stores the session information along with the user ID (Which Laravel may already do)

jfurnas's avatar

@sutherland Thank you for the advise. In going through, I enabled Auth using php artisan make:Auth, then set the provider to 'database', and using the default system it appears to work.

Now I just need to find out how to get the password hash to use the one that the table has stored, and not bcrypt, and everything will work nicely!

Please or to participate in this conversation.