Laravel 5.5中的Auth :: attempt()始终返回false

时间:2018-11-12 09:21:38

标签: laravel laravel-5 laravel-5.5 laravel-authentication

我使用Hash来加密种子机中的密码,在我的控制器中,我使用Auth来检查来自客户端的请求,但是它始终返回false,我不知道为什么。

这是代码:

我的HomeController:

    public function login(Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('password');

        if( Auth::attempt(['mssv' =>$username, 'pass' =>$password])) {
                $success = new MessageBag(['successlogin' => 'welcome']);
                return view('welcome')->withErrors($success);
            } else {
                $errors = new MessageBag(['errorlogin' => 'Login fail']);
                return redirect()->back()->withInput()->withErrors($errors);
            }
        }
    } 

我的播种机有:

    DB::table('sinhvien')->insert([
        'hoten' => 'Nguyễn Ngọc Anh Thư',
        'mssv' =>'DH51400668',
        'pass' =>Hash::make('DH51400668'),
        'created_at'=>date("Y-m-d H:i:s"),
        'updated_at'=>date("Y-m-d H:i:s")
    ]);

我的模特用户:

    <?php

    namespace App;

    use Illuminate\Notifications\Notifiable;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;

    class User extends Authenticatable
    {
        use Notifiable;

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $table='sinhvien' ;
        protected $fillable = [
            'mssv', 'hoten', 'pass',
        ];

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'pass', 'remember_token',
        ];
    }

我的路由器具有:

Route::post('/login', 'HomeController@login'); 

但是当我要求提供播种者的信息时,Auth::attempt ()总是返回false。

1 个答案:

答案 0 :(得分:2)

从密码捕获字段中删除Hash::make。当您使用Auth::attempt时,它将默认获取哈希值并搜索数据库。

尝试

$password = $request->input('password'); # remove Hash::make

if( Auth::attempt(['mssv' =>$username, 'pass' => $password])) { # remove quotes

How to prevent SQL injection in Laravel?