Laravel:更改API

时间:2018-04-25 06:36:44

标签: laravel laravel-5.5

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

有什么问题?这是更改密码的正确方法吗?

1 个答案:

答案 0 :(得分:0)

重置密码时,您在resetPassword功能中对密码进行两次哈希处理,在setPasswordAttribute中对密码进行二次哈希,因此您需要更换

此  $password = Hash::make($request->password);

有了这个  $password = $request->password;

在您的resetPassword函数