Laravel身份验证登录不断提供“这些凭据与我们的记录不匹配”。

时间:2019-08-29 05:20:45

标签: laravel authentication laravel-5.4

我安装了laravel并使用了它的默认身份验证控制器,但我修改了表名及其表结构,因此还更改了RegisterController和LoginController。并且RegisterController可以正常工作并注册一个新用户,但是每当我尝试使用登录表单登录时,它都会给出相同的验证错误,“这些凭据与我们的记录不匹配。”

我已附加了以下文件:LoginController,RegisterController,Admin(Model),Config-> auth

我已经根据我的EmailAddress字段覆盖了用户名字段。

管理员模型

return [
            'name'  => 'required|string|alpha_dash|max:255|min:6',
            'email' => ['required',
                        'string',
                        'email',
                        'max:255',
                        Rule::unique('users')->ignore($this->user->id)],
         ];

登录控制器

<?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;
    }
}

注册控制器

<?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";
    }
}

Config-> auth.php

<?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;
    }
}

1 个答案:

答案 0 :(得分:0)

RegisterController中,在create方法中,而不是 'password' => bcrypt($data['password']),执行

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

为什么会发生错误,因为可能是在注册时使用bcrypt哈希方法来输入密码,但是在登录时却使用了另一种哈希方法。 确保导入类

use Illuminate\Support\Facades\Hash;
在您的RegisterController文件顶部的

。 这里还要注意的另一件事是确保在数据库中插入新的用户记录时,确保默认情况下将小写电子邮件转换为小写,并在登录时确保也将小写电子邮件转换为小写。默认情况下,某些数据库区分大小写。因此,您可能在那里遇到问题。 就像数据库中有一封电子邮件一样, Admin@example.com,并且在登录时输入admin@example.com,在这种情况下将不匹配。 希望这会有所帮助。