dongzhengzhong1282 2017-02-07 10:03
浏览 82

Tymon JWT使用数据库提供程序代替eloquent

I'm using https://github.com/tymondesigns/jwt-auth on my lumen application.

Here's my composer.json

"laravel/lumen-framework": "5.3.*",
"tymon/jwt-auth": "^1.0@dev",

I've read a of tutorials on how to install. Some of which are:

I am able to make it work on my local and successfully return the token. But the problem is that instead of using eloquent on the provider that fetches data from database.sqlite, I want to use database as my driver.

With that, I have set

'providers' => [
    'users' => [
        'driver' => 'database',
        'table'  => 'user_table',
        // 'driver' => 'eloquent',
        // 'model' => App\User::class,
    ],
],

on my config/auth.php

Since it is now connection thru a database, it now uses the DatabaseUserProvider.php

I need to modify some codes though.

/**
 * 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'];

    return $this->hasher->check($plain, app('hash')->make($user->getAuthPassword()));
}

Notice that I added a app('hash')->make() when validating the password.

It then inject the retrieved user into the GenericUser object.

/**
 * Get the generic user.
 *
 * @param  mixed  $user
 * @return \Illuminate\Auth\GenericUser|null
 */
protected function getGenericUser($user)
{
    if (! is_null($user)) {
        return new GenericUser((array) $user);
    }
}

Since it is on the GenericUser object, it gives an error of:

Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of Illuminate\Auth\GenericUser given

In order to fix this, I have to "hack" it by removing the JWTSubject injection on every method under the tymon\jwt-auth\src\JWT.php

Is there a better way to clean this up?

  • 写回答

1条回答 默认 最新

  • douxin5953 2017-02-08 10:14
    关注

    You can easily fix it by making your authenticating user implement JWTSubject, which is the most right thing to do.

    class GenericUser implements JWTSubject {
        [...]
    }
    

    But, since you're dealing with a Laravel native implementation, I'd suggest you to extend it and implement JWTSubject, instead of going everywhere in jwt-auth's code and remove the type-hints.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100