Laravel Passport Auth :: attempt false,password_verify true

时间:2019-03-28 11:24:17

标签: php laravel authentication laravel-passport

我正在使用通行证来注册并使用api保护程序登录用户,但是在注册过程正常进行的同时,我无法使用我的代码登录。我已经在可以正常工作的另一台VM上对其进行了测试。

返回的JSON是

{
    "message": "Unauthorized",
    "request": {
        "username": "lloyd.thomas.admin",
        "password": "mypassowrd"
    }
}

我已使用本机password_verify来测试passowrd,它显示它是有效的。 Authcontroller.php

public function login(Request $request) {
    $request->validate([
        'username' => 'required|string',
        'password' => 'required|string',
        'remember_me' => 'boolean'
    ]);

    $credentials = request(['username', 'password']);

    if (!Auth::attempt($credentials)){
        return response()->json([
                    'message' => 'Unauthorized',
                    'request' => request(['username', 'password']),
                        ], 401);
    }

    $user = $request->user();

    $tokenResult = $user->createToken('Personal Access Token');
    $token = $tokenResult->token;

    if ($request->remember_me){
        $token->expires_at = Carbon::now()->addWeeks(1);
    }

    $token->save();
    return response()->json([
                'access_token' => $tokenResult->accessToken,
                'token_type' => 'Bearer',
                'expires_at' => Carbon::parse(
                        $tokenResult->token->expires_at
                )->toDateTimeString()
    ]);
}

在App /模型/用户中

public function setPasswordAttribute($password) {
        $this->attributes['password'] = bcrypt($password);
    }

在路线api.php中

Route::group([
    'prefix' => 'auth'
], function () {
    Route::post('login', 'ApiControllers\AuthController@login');
    Route::post('signup', 'ApiControllers\AuthController@signup');

    Route::group([
      'middleware' => 'auth:api'
    ], function() {
        Route::get('logout', 'ApiControllers\AuthController@logout');
        Route::get('user', 'ApiControllers\AuthController@user');
    });
});

使用Web Guard而不使用api时,用户名和密码通过身份验证

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],

0 个答案:

没有答案