简体   繁体   English

Laravel Auth::attempt 总是返回 false

[英]Laravel Auth::attempt always return false

I've tried searching Google and tried several methods.我试过搜索谷歌并尝试了几种方法。 None of them are working though: the auth::attempt still returns false.他们都没有工作: auth::attempt仍然返回 false。 My registration code is below;我的注册码如下; in my AuthController I have:在我的AuthController我有:

protected function create(array $data)
{
    $datetime = new \DateTime();
    $datetime->format('Y-m-d H:i:s');
    $datetime->getTimestamp();

    return User::create([
        'country_id' => 1, // default to malaysia
        'user_name' => $data['user_name'],
        'facebook_id' => null,
        'steam_link' => null,
        'password' => Hash::make($data['password']),
        'extra_information' => null,
        'remember_token' => null,
        'email' => $data['email'],
        'status_id' => 1, // active
        'is_verified' => false, // false
        'user_type_id' => 2, // normal user
        'account_status_id' => 1, // pending
        'confirmation_code' =>  str_random(30),
        'created_at' => $datetime
    ]);
}

 public function postRegister(Request $request)
 {
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    $user = $this->create($request->all());

    Mail::send('emails.registration_verification', ['user' => $user], function ($m) use ($user)
    {
        $m->from('no-reply@myapps.com', 'www.myapps.com');
        $m->to($user->email, $user->user_name)->subject('Verify your myappsaccount.');
    });

    return view('auth.registration_success')->with('user_name',$user->user_name)->with('email', $user->email);
}

public function emailVerification($confirmation_code)
{
    if (!$confirmation_code)
    {
        return Redirect::route('/errors/500');
    }

    $user = User::whereConfirmationCode($confirmation_code)->first();

    if (!$user)
    {
        return Redirect::route('/errors/500');
    }

    $user->account_status_id = 2; // active
    $user->confirmation_code = null;
    $user->save();

    return view('auth.verification_success')->with('user',$user);
}

public function postLogin(Request $request)
{
    $userdata = array(
        'email' => $request->email,
        'Password' => $request->password
    );

    if (Auth::attempt($userdata,true)) {
        echo 'test';
    }
    else {
        var_dump($userdata);
    }
}

I've hashed my password accordingly, I've also changed the password column from 60 to 255, but still the login fails.我相应地对密码进行了哈希处理,我还将密码列从 60 更改为 255,但登录仍然失败。

The expected workflow is the user registers, the system sends an email to them for verification.预期的工作流程是用户注册,系统向他们发送电子邮件进行验证。 The user clicks the link in the email which will trigger the emailVerification method.用户单击电子邮件中的链接将触发emailVerification方法。 Once verification is complete, the user should be able to login.验证完成后,用户应该能够登录。

Am I missing anything?我错过了什么吗?

I'm pretty sure your error is here:我很确定你的错误在这里:

$userdata = array(
        'email' => $request->email,
        'Password' => $request->password
);

The password field in your database is lowercase and you're checking for one that starts with an uppercase P .数据库中的password字段是小写的,您正在检查以大写P开头的password字段。 Change 'Password' => $request->password to 'password' => $request->password and it should work.'Password' => $request->password'password' => $request->password ,它应该可以工作。

my bad, included the follow code in user model help me solve the problem.我不好,在用户模型中包含以下代码帮助我解决问题。

public function getAuthPassword()
    {
        return $this->password;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM