1

我是新手,正在学习 laravel 5.7。注册已完成,但在登录期间进行身份验证时出现问题,Auth::attempt 始终返回 false

我尝试使用Hash::check('plain-text','Hashed password)它返回 true 问题Auth::attempt总是返回 false

用户控制器.php:

// Registration code
public function register(Request $request){
       $request->validate([
            'firstName' => 'required',
            'lastName' => 'required',
            'username' => 'required|min:3',
            'password' => 'min:6|required_with:confirm_password|same:confirm_password',
            'confirm_password' => 'min:6'
        ]);

        Users::create([
            'firstName' => request('firstName'),
            'lastName' => request('lastName'),
            'username' => request('username'),
            'password' => Hash::make(request('password'))
        ]);
        notify()->success('Registered Successfully!');
        return redirect('/');

    }

// Login code
public function login(Request $request){

//      This returns true
//        if(Hash::check('123456','$2y$10$BrOg1JtnX7hAX05gbT9p0OZFQB9mFKtcz0m5Ks2rSHIN//B20dODgA.')){
//            return 'OK';
//        }


        $credentials = request([
            'username',
            'password'
        ]);


// Always returns false
        if(Auth::attempt($credentials)){
            notify()->success('Welcome!');
            return back();

        }else{
            notify()->warning('Credentials not found!');
            return back();
        }




    }

用户.php:

//Model
class Users extends Model 
{
    //
    protected $guarded = [];


}

auth.php:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Users::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

迁移:

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

login.blade.php:

<form action="/" method="post" autocomplete="on">
                        @csrf
                        <h1>Log in</h1>
                        <p>
                            <label for="Username"  data-icon="u"> Username</label>
                            <input id="Username" name="username" required="required" type="text" placeholder=""/>
                        </p>
                        <p>
                            <label for="Password"  data-icon="p"> Password </label>
                            <input id="Password" name="password" required="required" type="password" placeholder=""/>
                        </p>
                        <p class="login button">
                            <input type="submit" value="Login"/>
                        </p>
                        <p class="change_link">
                            Not a member yet ?
                            <a href="/register" class="to_register">Join us</a>
                        </p>
                    </form>

笔记

我很肯定我得到了正确的输入

// Returns correct information
{
"username": "yeah",
"password": "123456"
}

4

2 回答 2

0

Laravelemail默认使用授权用户,因此请尝试覆盖您在LoginController添加中使用的字段:

public function username()
{
    return 'username';
}
于 2019-03-24T20:57:54.410 回答
0

你不需要尝试重写登录功能(也不需要注册),因为这些在 Laravel 中是开箱即用的。你需要做的是:

首先在你的login.blade.php

<form action="/login" method="post" autocomplete="on">

然后在你的web.php

Route::post('login', 'Auth\LoginController@login');

现在去你的App\Http\Auth\LoginController

// Add this function to overwrite default email to username
public function username()
{
    return 'username';
}
于 2019-03-24T21:02:06.920 回答