【问题标题】:Change Password using API Laravel使用 API Laravel 更改密码
【发布时间】:2020-01-21 03:36:31
【问题描述】:

我试图使用 API Laravel 更改密码。但它没有用。我是 API Laravel 的新手。

有什么建议可以解决我的问题吗?

public function __construct()
{
    $this->middleware('auth');
}

public function store(ChangePasswordValidation $request)
{
    if(Auth::check($data['current_password'], $user->password))
    {
        $user = User::find(Auth::user()->id)->update(["password"=> bcrypt($request->password)]);
        $success['token'] =  $user->createToken('newToken')->accessToken;
        return response()->json(['success' => $success], 200);
    }
    else
    {
        return response()->json(['error'=>'Unauthorised'], 401); 
    }
}

Postman 中显示的结果是: { “消息”:“未经身份验证。” }

【问题讨论】:

  • 你在 web.php 或 api.php 中的路由写在哪里?
  • in api.php -> Route::post('/change-password', 'Auth\ChangePasswordController@store');

标签: laravel api


【解决方案1】:

您需要在使用 API 时使用防护 所以改变

Auth::user()

Auth::user('api')

还有

$this->middleware('auth');

$this->middleware('auth:api');

【讨论】:

  • 还是不行。它显示相同的结果 { "message": "Unauthenticated." } 当我使用 Postman 测试它时
  • 您是否在请求的标头中发送不记名令牌?
  • 我不确定我是否理解您的问题,但我认为是否定的。对不起。我对 API 很陌生。
  • 确保您没有为路由或控制器使用任何身份验证中间件
【解决方案2】:

编辑这一行

if(Auth::check($data['current_password'], $user->password))

进入

if(Auth::check($request['current_password'], $user->password))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2017-12-07
    • 2017-01-27
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多