首页 文章

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

提问于
浏览
1

我将用户名和密码通过我的视图中的帖子请求传递给控制器 . 负责处理邮件请求的控制器:

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');                            
    }

问题是我一直收到“失败”消息:“用户名或密码无效” .

我查看了phpmyadmin中的表,用户名和密码非常简单(用户名:Admin和密码:12345) .

这是admins表的数据库:

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

update 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!']);       

    }

4 回答

  • 0

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

  • 1

    你总是在Auth :: try中得到错误,因为你的数据库中的密码没有哈希 . 首先在数据库中隐藏密码 . 您可以使用播种机在数据库中播种

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

    希望这会有所帮助 .

  • 1

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

    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 .

    希望这会帮助你 .

  • 0

    Laravel会期望散列密码 . 因此,如果phpmyadmin中的密码可见,则无法在Laravel中进行验证 . 我希望phpmyadmin中的密码看起来像这样 $2y$13$Cn0gwiUfg2mq5Y3/V98aB.NZ4GJqjbwhvKlsSAOYkVNHKlWRmwZ1W

相关问题