【问题标题】:Auth::attempt () in Laravel 5.5 always return falseLaravel 5.5 中的 Auth::attempt () 总是返回 false
【发布时间】:2019-04-14 23:49:42
【问题描述】:

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

这是代码:

我的家庭控制器:

    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。

【问题讨论】:

  • 是否有任何理由将用户名和密码与字符串中的附加引号括起来?
  • 我不明白你的问题@NicoHaase
  • if( Auth::attempt(['mssv' =&gt; '\''.$username.'\'', 'pass' =&gt;'\''.$password.'\''])) 更改为 if( Auth::attempt(['mssv' =&gt; $username, 'pass' =&gt;$password])),因为不需要像 NicoHaase 所说的那样用额外的引号括起用户名和密码
  • 我认为这是我的代码中的疏忽,但是当我将其更改为您的评论时,它也会返回相同的结果
  • 您是否使用 php artisan db:seed 运行播种机?

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


【解决方案1】:

从密码捕获字段中删除 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?

【讨论】:

  • 我认为这是我的代码中的疏忽,但是当我将其更改为您的评论时,它也返回相同的结果,我不知道为什么
  • 没办法。我给了你一个工作的例子。使用该应用程序创建一个用户并尝试登录。工作正常
猜你喜欢
  • 1970-01-01
  • 2016-09-12
  • 2017-05-24
  • 2013-06-11
  • 2015-12-10
  • 2014-03-15
  • 2022-01-22
  • 2012-10-25
相关资源
最近更新 更多