Laravel JWT Auth中间件(自定义用户表)始终返回401

时间:2018-12-22 06:33:22

标签: php laravel jwt laravel-5.7 jwt-auth

我已经通过具有自定义用户模型以及字段的laravel应用程序成功登录了用户。

但是问题是如何执行注销以及其他操作。这是路线列表

+--------+----------+--------------+------+----------------------------------------------+-----------------+
| Domain | Method   | URI          | Name | Action                                       | Middleware      |
+--------+----------+--------------+------+----------------------------------------------+-----------------+
|        | GET|HEAD | /            |      | Closure                                      | web             |
|        | POST     | api/login    |      | App\Http\Controllers\AuthController@login    | api             |
|        | GET|HEAD | api/logout   |      | App\Http\Controllers\AuthController@logout   | api,jwt.auth    |
|        | POST     | api/me       |      | App\Http\Controllers\AuthController@me       | api,jwt.auth    |
|        | POST     | api/refresh  |      | App\Http\Controllers\AuthController@refresh  | api,jwt.refresh |
|        | POST     | api/register |      | App\Http\Controllers\AuthController@register | api             |
|        | GET|HEAD | login        |      | Closure                                      | web             |
+--------+----------+--------------+------+----------------------------------------------+-----------------+

这是所有路线的安排方式

Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');

Route::middleware('jwt.auth')->group(function () {
    Route::get('logout', 'AuthController@logout');
    Route::post('me', 'AuthController@me');
});

Route::middleware('jwt.refresh')->group(function () {
    Route::post('refresh', 'AuthController@refresh');
});

问题出在jwt.authjwt.refresh上。两者都将返回典型的401。我的意思是laravel 401响应。

为您提供信息,我没有使用常规的电子邮件和密码来验证用户身份,而是使用其他名称来验证凭据。因此,我创建了另一个服务提供商,并如下重写了validateCredential

/**
 * Validate a user against the given credentials.
 *
 * @param \Illuminate\Contracts\Auth\Authenticatable $user
 * @param array                                      $credentials
 *
 * @return bool
 */
public function validateCredentials(UserContract $user, array $credentials)
{
    $plain = $credentials['password']; // will depend on the name of the input on the login form
    $hashedValue = $user->getAuthPassword();

    if ($this->hasher->needsRehash($hashedValue) && $hashedValue === md5($plain)) {
        $user->us_password = bcrypt($plain);
        $user->save();
    }

    return $this->hasher->check($plain, $user->getAuthPassword());
}

并对AuthServiceProvider

进行一些修改
class AuthServiceProvider extends ServiceProvider
{
    ...

    /**
     * Register any authentication / authorization services.
     */
    public function boot()
    {
        $this->registerPolicies();

        $this->app['auth']->provider('custom', function ($app, array $config) {
            $model = $app['config']['auth.providers.users.model'];

            return new CustomUserProvider($app['hash'], $model);
        });
    }
}

我开始怀疑此软件包与Laravel 5.7不兼容。因为我使用了建议的版本("tymon/jwt-auth": "dev-develop"

0 个答案:

没有答案