无法验证 Laravel 中的用户:'invalid username or password'

Unable to authenticate a user in Laravel: 'invalid username or password'

我通过 post 请求将用户名和密码从我的视图传递到控制器。负责处理post请求的控制器:

public function postLogin(Request $request) {
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required'
        ]);

        if (!Auth::attempt([
            'username' => $request['username'],                    
            'password' => $request['password'] ])) { 
            return redirect()->back()->with(['fail' => 'invalid username or password']);      
        }

        return redirect()->route('auth.dashboard');                            
    }

问题是我不断收到 'fail' 消息:'invalid username or password'。

我查看了phpmyadmin里面的table,用户名和密码很简单(用户名:Admin,密码:12345)。

这是管理员的数据库 table :

class CreateAdminsTable extends Migration
{

    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('username')->unique();
            $table->string('password');
            $table->rememberToken();
        });
    }

    public function down()
    {
        Schema::drop('admins');
    }
} 

作为参考,我正在使用 Laravel 5.2

更新1:用户是通过注册控制器创建的,注册控制器将用户名和密码存储在数据库中。这是控制器:

public function postRegister(Request $request) {

        $admin = new Admin();

        $this->validate($request, [
            'username' => 'required|unique:admins|max:30|min:3',
            'password' => 'required|min:5',
            'password_confirm' => 'required'           
        ]);

        $password = $request['password'];
        $passwordConfirm = $request['password_confirm'];

        if ($password !== $passwordConfirm) {
            return redirect()->back()->with(['fail' => 'password fields do not match!']);
        }     
        $hashedPassword = password_hash($password, PASSWORD_BCRYPT);

        $admin->username = $request['username'];
        $admin->password = $hashedPassword;
        $admin->save();

        return redirect()->route('index')->with(['success' => 'Successfully created account!']);       

    }

请使用以下代码更改登录功能。

public function postLogin(Request $request) {
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required'
        ]);

        if (Auth::attempt([
            'username' => $request['username'],                    
            'password' => $request['password'] ],$remember)) { 
            return redirect()->route('auth.dashboard');            
        }
        return redirect()->back()->with(['fail' => 'invalid username or password']);

    }

注意:这里的 $remember 应该是 1/0 或 TRUE/FALSE。

希望对您有所帮助。

你总是在 Auth::attempt 中得到错误,因为你数据库中的密码不是散列。首先在数据库中散列密码。您可以使用播种机在数据库中播种

class UserSeeder extends Seeder
{
    public function run()
    {
       //your admin model
       Admin::create([
            'username' => 'admin',
            'password' => Hash::make(123)
       ]);
     }
}

希望这会有所帮助。

Laravel 需要哈希密码。 因此,如果 phpmyadmin 中的密码可见,则无法在 Laravel 中验证。我希望 phpmyadmin 中的密码看起来像这样 y$Cn0gwiUfg2mq5Y3/V98aB.NZ4GJqjbwhvKlsSAOYkVNHKlWRmwZ1W

我更喜欢使用 Hash::make 而不是 password_hash。因为 Hash::make 中有一个附加选项已传递给 password_hash。这就是哈希不匹配的原因