Back

Changing default columns and table in Laravel Auth 5.2


Ok I am having this problem for a week now. I am following the naming convention of our company in creating Schema for Tables and Columns. This means that I need to modify the default columns and tables use in Laravel Auth 5.2.

I followed the instruction found online. I didn't change the config/auth.php

Our database is MS SQL

My migration script:

     Schema::create('Admins', function (Blueprint $table) {
                $table->increments('Id');
                $table->string('Email')->unique();
                $table->string('Password');
                $table->string('RememberToken')->nullable();
                $table->timestamp('CreatedAt');
                $table->timestamp('UpdatedAt');
            });

My routes:

    Route::auth ();
    
    Route::get ('/', [email protected]');
    Route::get ('/home', [email protected]');

Overriding the defaults in User Model

  1. Specify the table in User model

    ....public $table = 'Admins';

  2. Using my own timestamp column so I disable the default

    ....public $timestamps = false;

  3. Specify primary key use in my table

    ....protected $primaryKey = 'Id';

  4. Specify fillable and hidden fields

   protected $fillable = [
                'Email', 'Password', 'CreatedAt', 'UpdatedAt'
        ];
    
   protected $hidden = [
                'Password', 'RememberToken',
        ];
  1. Overriding more
     // Override required, otherwise existing Authentication system will not match credentials
        public function getAuthPassword ()
        {
            return $this->Password;
    
        }
    
        // Override required, otherwise existing Authentication system will not match credentials
        public function getRememberToken ()
        {
            return $this->RememberToken;
    
        }

Overriding the defaults in User Model

  1. Login method
 public function postLogin (Request $request)
    {
        $this->validate ($request, [
                'Email' => 'required|email', 'Password' => 'required',
        ]);

        $credentials = $request->only ('Email', 'Password');

        if ($this->auth->attempt ($credentials, $request->has ('remember')))
        {
            return redirect ()->intended ($this->redirectPath ());
        }

        return redirect ($this->loginPath ())
                        ->withInput ($request->only ('Email', 'remember'))
                        ->withErrors ([
                                'Email' => $this->getFailedLoginMessage (),
        ]);

    }

Registration is fine. The problem is when I am trying to login. It gives me this error "These credentials do not match our records". I am pretty sure the login credentials is right because the password is just "123456".

What am I missing? Please help

mirbatworks replied 4 months ago

You need to check in database how is stored your password. Is it stored as 123456 or is crypted something like: $2y$10$UYwtjlZXTeMFSJuWfz7fZeR7e39Y4h7CIgj3f3iWEo4hkI4YNCtI2

When you are type your password in login page Laravel crypt your password in something like code above than compare crypted password and email with crypted password and email stored in database.

If your password is not crypted in database you need to crypt it before you store it. Check your logic in users registration and login code.

flashery replied 4 months ago

My password is encrpyted in the database. I use bcrypt to do it.

What I want to know is how this function "Auth->attempt" compare the credentials from the database to my input request.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us