简体   繁体   中英

Laravel authentication login keeps giving "These credentials do not match our records."

I have setup laravel and used it's default authentication controller but I modified the table name and it's table structure and accordingly I also changed the RegisterController and LoginController. And the RegisterController is working fine and registering a new user but when ever I try to login using the login form it gives the same validation error of "These credentials do not match our records."

I have attached the following files: LoginController, RegisterController, Admin(Model), Config->auth

I have overridden the username field according to my EmailAddress field.

Admin Model

<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class admin extends Authenticatable
{
    use Notifiable;
    public $table = 'admin';
    public $timestamps = false;
    protected $primaryKey = 'AdminId';
    protected $fillable = ['FirstName', 'LastName', 'FirstName_ar','LastName_ar','EmailAddress','Password','IsActive','remember_token'];

    protected $hidden = ['Password', 'remember_token'];

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

Login Controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username()
    {
        return 'EmailAddress';
    }

    public function getRememberTokenName()
    {
        return "remember_token";
    }
}

Register Controller

<?php

namespace App\Http\Controllers\Auth;

use App\admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'firstname' => 'required|string|max:255',
            'lastname' => 'required|string|max:255',
            'firstname_ar' => 'required|string|max:255',
            'lastname_ar' => 'required|string|max:255',
            'email' => 'required|string|email|max:255',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return admin::create([
            'FirstName' => $data['firstname'],
            'LastName' => $data['lastname'],
            'FirstName_ar' => $data['firstname_ar'],
            'LastName_ar' => $data['lastname_ar'],
            'EmailAddress' => $data['email'],
            'Password' => bcrypt($data['password']),
            'IsActive' => 1,
            'remember_token' => str_random(10)
        ]);
    }

    public function getRememberTokenName()
    {
        return $this->remember_token;
    }
}

Config->auth.php

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'admin',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],


    'providers' => [
        // 'users' => [
        //     'driver' => 'eloquent',
        //     'model' => App\User::class,
        // ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => App\admin::class,
        ],
    ],

    'passwords' => [
        // 'users' => [
        //     'provider' => 'users',
        //     'table' => 'password_resets',
        //     'expire' => 60,
        // ],
        'admin' => [
            'provider' => 'admin',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

In the RegisterController , in the create method, instead of 'password' => bcrypt($data['password']) , do this

'password' => Hash::make($data['password'])

Probably why your error is happening because maybe when you're registering you're using the bcrypt hashing method for the password but when you're logging, it's using a different hashing method. Make sure to import the class

use Illuminate\Support\Facades\Hash;

at the top of your RegisterController file. One more thing to take care of here is to make sure when you're inserting the new user record in the database, make sure to lowercase the email by default and when logging, make sure to lowercase the email too. Some databases are case sensitive by default. So you may have a problem there. Like you have an email in the database, Admin@example.com and when logging, you give admin@example.com, it will not match in that case. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM