Laravel 5.1 bcrypt和登录

4
当我在Laravel框架中注册新用户时,我目前是这样做的:
// Creating a new user
$user = new User;
$user->firstname = $data['firstname'];
$user->lastname = $data['lastname'];
$user->email = $data['email'];
$user->password = bcrypt($data['password']);
$user->save();

这很好用,我能够登录应用程序。然而,我希望用户在设置页面中有一个更改密码的选项。为了实现这一点,我使用了相同的技术,使用

$newPass = bcrypt($response->new_password);

我更新了用户字段的内容,但是在这之后我不能登录。我正在使用Laravel内置的身份验证服务进行注册/登录。

我做错了什么?应该用另一种方法吗?

我还尝试了将当前密码bcrypt处理,但是得到的哈希值与数据库中存储的完全不同。

这太令人困惑了...

以下是控制器代码:

// Validation
$this->validate($request, [
    'email' => 'email',
    'password' => 'min:8|confirmed',
    'current_password' => 'required',
]);

// Getting the user ID
$userId = Auth::id();

// Dummy hack check, change later.
if(!Auth::attempt(['id' => $userId, 'password' => $request->current_password]))
{
    return redirect('settings')->with('alert','current password is wrong.');
}

// Everything is validated and ok to proceed
if($request->email)
{
    $data['email'] = $request->email;
}

if($request->password)
{
    $data['password'] = bcrypt("helloworld");
}

$user = User::where('id',$userId)->update($data);

dd($data);

转储输入数据,
(Note: 保留了HTML标签)
  +request: ParameterBag {#40 ▼
    #parameters: array:5 [▼
      "_token" => "JQIIuCjiKQmbK0X5zCM6czYD1vIoh4PGjLO4qrFm"
      "email" => "testing@gmail.com"
      "password" => "thisisnewpass"
      "password_confirmation" => "thisisnewpass"
      "current_password" => "helloworld"
    ]
  }

你在设置新密码后保存了用户吗? - BrokenBinary
当用户更改密码时,他们仍然登录,是的。如果我注销自己并尝试再次登录,则会持续显示密码错误,即使我的旧密码也无法使用。 - Sweepy
好的,我尝试对明文进行哈希处理,这个成功了。现在,我不确定为什么从表单输入中获取的密码无法正确哈希处理。然而,当我将密码从明文更改后,也被强制退出登录了。 - Sweepy
你确定你实际上从表单中获取了输入吗?在对其进行哈希处理之前,打印出 $response->new_password 并查看它是否有任何额外的空格。 - BrokenBinary
我已经更新了代码,加入了控制器和转储。 - Sweepy
显示剩余4条评论
3个回答

0

这段代码更接近于Laravel内部处理重置用户密码的方式。试试看吧。

// Getting the User
$user = Auth::user(); // Gets the currently logged in User
$credentials = [
    'id' => $user->id,
    'password' => $request->input('current_password')
];

// Make sure current password is correct
if (!Auth::validate($credentials)) { // Checks the User's credentials
    return redirect('settings')->with('alert','current password is wrong.');
}

// Change the password
if ($request->has('password')) {
    $user->password = bcrypt($request->input('password'));
}

// Save any changes
$user->save();

看起来你正在使用同一个表单来更新用户的电子邮件地址,因此请根据你的需求更新代码。


这与我使用bcrypt完全相同,但是仍然有些问题。 - Sweepy

0

将密码存储在一个新变量中似乎解决了问题(不确定为什么?)然而,这是使一切正常工作的代码,

// Validation
$this->validate($request, [
    'email' => 'email',
    'password' => 'min:8|confirmed',
    'current_password' => 'required',
]);

// Getting the user ID
$userId = Auth::id();
$newPassword = $request->password;

// Dummy hack check, change later.
if(!Auth::attempt(['id' => $userId, 'password' => $request->current_password]))
{
    return redirect('settings')->with('alert','Wrong password.');
}

// Everything is validated and ok to proceed
if($request->email)
{
    $data['email'] = $request->email;
}

if($request->password)
{
    $data['password'] = bcrypt($newPassword);
}

// Getting, and checking if the current password is corrent.
$user = User::where('id',$userId)->update($data);

echo $newPassword . "<br><br>";

dd($data);

如果有我没有注意到的解释,请告诉我原因。不过,现在它已经可以工作了。


如果您将上面代码中的 $request->password 直接复制粘贴到 bcrypt() 调用中,它会再次出错吗? - Jared Farrish

0

对于2017年的Laravel,我们是这样做的:

//create a setter method in your controller

public function setPasswordAttribute( $password ) {
    if ( $password !== null ) {
        if ( is_null(request()->bcrypt) ) {
            $this->attributes['password'] = bcrypt($password);
        } else {
            $this->attributes['password'] = $password;
        }
    }
}

检查一下this链接,他们都在谈论将其放置在模型中,但它在我的控制器内部起作用。


这是糟糕的代码。如果$password参数为null,你不应该默默地什么都不做。 - Marcin Orlowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接