0

I'm trying to migrate an old PHP/MySQL to Laravel project. For so many reasons I'm should not modify the database table schema. I need to modify the Laravel framework classes. For example, I want to force Laravel Auth to check the users.USER capitalize column to return the user, instead of users.user. I need to know which part of the Laravel framework check the users database table or user column. How to modify without break the default Auth functionality.

More specific: I want to know how Laravel retrieve data from database in /Vendor/laravel/framework/src/illuminate/Auth/EloquentUserProvider.php line 137 validateCredentials().

5
  • Why don't you write migration scripts? e.g You can migrate users.USER to users.user column in your migration script. Commented Feb 25, 2019 at 19:28
  • stackoverflow.com/a/33889923/823549 Commented Feb 25, 2019 at 19:29
  • You can also use custom model for authentication, i.e. a table other than users. See this post for more details. Commented Feb 25, 2019 at 19:32
  • if your current table schema has the main columns i.e name, password, email; then in the Users table you can just specify the table property by passing the table name there and laravel will automatically pick that Commented Feb 25, 2019 at 19:37
  • @john My goal is to force Laravel Auth to use my users.USER column instead of checking the users.user table. I don't want to change my table schema. Commented Feb 25, 2019 at 19:49

3 Answers 3

5

Database considerations

If you want to use your custom database on your laravel application, you just have to make a Model for each of the tables of your database and set up the table name, primary key and fillable attributes.

More info here Defining Models

Authentication with custom user and pass fields

If you executed the command `php artisan make:auth` but you want to use another table or fields instead of the default user and pass without breaking the created Auth functionality, you have configure Laravel for it, following the steps bellow:

First: Create a Model for the table that you want to use for authentication. To do it, run the command php artisan make:model FooUserTable, where 'FooUser' is the name of your table.

Next you have to set on the model your table name, primary key and fillable fields, like bellow:

class FooUser extends Authenticatable{

    protected $table = 'FooUser';
    protected $primaryKey = 'foo_id_user'; // Place here the name of the id of your table
    protected $fillable = [ // Insert here all the fillable fields of your users table
        'field1',
        'field2',
        ...
    ];

}

Note that the model now extends from Authenticatable instead of model, that is an alias for the class Illuminate\Foundation\Auth\User,so just use an alias for the User class: use Illuminate\Foundation\Auth\User as Authenticatable.

This is important!:

In order to use another field name for the password, add the function getAuthPassword into your newly created model, like bellow:

public function getAuthPassword(){
    return $this->myPasswordField; // myPasswordField is the field on your users table for password
}

Now you have all of your model ready for the next step.

Second: On the file config > auth.php you have to find the providers array, and change the model property value to your model class name, for example:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\FooUser::class, // Place here the name of your model
    ],
],

All ready for the next step.

Third: Now you have to find the controller for the default Laravel Auth, located into app > http > controllers > auth > LoginController.php, and into the class, you only have to add a function called username that returns the username to be used for the auth action, like bellow:

class LoginController extends Controller{

    ..... // don't change other functions

    public function username(){ // add the function username
        return 'YourUsernameField';
    }

}

Blade login template note: In your login blade template, you should change the username input name to the new one, and the password field name must be still password, thats because laravel use it for validation, but internally it's going to use your custom password field name.

And that's all. I hope this helps you.

Note: Never edit the files into the vendor folder!

For more information, read the documentation on the section Manually Authenticating Users

All of this was tested on Laravel 5.4 and 5.7 versions.

Sign up to request clarification or add additional context in comments.

1 Comment

I personally think it's overkill to what he's trying to achieve. But it's a very well structured and detailed answer. It's also more an answer to the question of how to extend properly.
1

You will have to create all models for the table and relations. On the models you can specify a table name, you don't have to rename the tables if you don't want to.

for example.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class User extends Model{
    protected $table = 'users.USER';
}

Comments

1

Database Related Question/Answer:

For extracting Models out of a Database Scheme you can use the following package:

Reliese/Laravel

This package makes it possible to automatically generate the Eloquent Models you need to start working with your database in Laravel.

It has a bunch of easy to use commands to be able to make models from your Database Scheme.

Usage

Let's scaffold some of your models from your default connection.

php artisan code:models

You can scaffold a specific table like this:

php artisan code:models --table=users

You can also specify the connection:

php artisan code:models --connection=mysql

If you are using a MySQL database, you can specify which schema you want to scaffold:

php artisan code:models --schema=shop

Extending/Editing Auth Functionality

Take a look at the official documentation about manually authenticating.

If you place the following snippet in your Controller:

public function authenticate(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}

If you need to specify which properties to use you can use the following:

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
  // The user is active, not suspended, and exists.
}

You could also just do your own check and then login in your code directly with the following code:

Auth::login($user);

// Login and "remember" the given user...
Auth::login($user, true);

Everything I listed here is in the link above about authenticating users.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.