JWT的新手,我想在我尝试登录后只是更改我的密码无效。
我的更新密码功能代码:
public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth)
{
$password = Hash::make($request->password);
$user = User::where('email', '=', $request->email)->first();
if(!$user) {
return response()->json([
'message' => "Credential do not match",
'status_code' => 403,
]);
}
if($user) {
$user->password = $password;
$user->save();
}
return response()->json(['message' => 'Your password has been changed successfully','status_code' => 204]);
}
尝试登录后,此函数正常工作,返回$token
null。
我的登录控制器代码:
public function login(LoginRequest $request, JWTAuth $JWTAuth)
{
$credentials = $request->only(['email', 'password']);
try {
$token = Auth::guard()->attempt($credentials);
if(!$token) {
return response()->json([
'message' => "Email and password do not match",
'status_code' => 403,
]);
}
$user = Auth::user();
$user->last_login = Carbon::now();
$user->save();
$user = Auth::user();
$user->UserDeviceData()->firstOrCreate([
'device_id' => $request->device_id
]);
} catch (JWTException $e) {
return response()->json([
'message' => "Internal server error",
'status_code' => 500,
]);
}
return (new UserTransformer)->transform($user);
}
在user
型号上:
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
有什么问题?这是更改密码的正确方法吗?
答案 0 :(得分:0)
重置密码时,您在resetPassword
功能中对密码进行两次哈希处理,在setPasswordAttribute
中对密码进行二次哈希,因此您需要更换
此
$password = Hash::make($request->password)
;
有了这个
$password = $request->password;
在您的resetPassword
函数