gpt4 book ai didi

php - Laravel 5.1 bcrypt 和登录

转载 作者:可可西里 更新时间:2023-10-31 23:46:42 25 4
gpt4 key购买 nike

当我在 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,得到的哈希与数据库中存储的完全不同。

这太令人困惑了..

更新了 Controller 代码,

// 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);

为输入转储数据,

  +request: ParameterBag {#40 ▼
#parameters: array:5 [▼
"_token" => "JQIIuCjiKQmbK0X5zCM6czYD1vIoh4PGjLO4qrFm"
"email" => "testing@gmail.com"
"password" => "thisisnewpass"
"password_confirmation" => "thisisnewpass"
"current_password" => "helloworld"
]
}

最佳答案

此代码更接近于 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();

看起来您也在使用相同的表单来更新用户的电子邮件地址,因此请更新代码以满足您的需要。

关于php - Laravel 5.1 bcrypt 和登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33247029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com