Laravel自定义用户表和JWT身份验证

时间:2019-04-01 08:41:08

标签: jwt laravel-5.8

尝试在Laravel JWT身份验证中实现自定义用户类

我正在尝试实现一个使用自定义表获取用户信息的应用程序。

我的桌子_user看起来像这样

'USR_NoRole', 
'USR_CompleteName', 
'USR_UserName', 
'USR_Password', 
'USR_BBlocked', 
'USR_BAssociation', 
'USR_NoPeriode', 
'USR_NoEntite', 
'USR_CreateUser', 
'USR_CreateTimestamp', 
'USR_UpdateUser', 
'USR_UpdateTimestamp'

创建新用户时,一切正常,我取回令牌

public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'noRole' => 'required',
            'completeName' => 'required',
            'name' => 'required|string|max:255',
            'password' => 'required|string|min:6|confirmed',
            'noPeriode' => 'required',
            'noEntite' => 'required'
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors()->toJson(), 400);
        }

        $user = User::create([
            'USR_NoRole' => $request->get('noRole'),
            'USR_CompleteName' => $request->get('completeName'),
            'USR_UserName' => $request->get('name'),
            'USR_Password' => Hash::make($request->get('password')),
            'USR_BBlocked' => $request->get('isBlocked'),
            'USR_BAssociation' => $request->get('isAssociation'),
            'USR_NoPeriode' => $request->get('noPeriode'),
            'USR_NoEntite' => $request->get('noEntite'),
            'USR_CreateUser' => "jschneider",
            'USR_CreateTimestamp' => date("Y-m-d H:i:s"),
            'USR_UpdateUser' => "jschneider",
            'USR_UpdateTimestamp' => date("Y-m-d H:i:s"),
        ]);

        $token = JWTAuth::fromUser($user);

        return response()->json(compact('user', 'token'), 201);
    }

这里是结果

{
    "user": {
        "USR_NoRole": "1",
        "USR_CompleteName": "Complete Name",
        "USR_UserName": "test@test.com",
        "USR_Password": "$2y$10$bli2OTQQfsq7h4/XryZT4Op4p5DCnEGANCR5MYagHbndNU/ULmE3G",
        "USR_BBlocked": null,
        "USR_BAssociation": null,
        "USR_NoPeriode": "1",
        "USR_NoEntite": "1",
        "USR_CreateUser": "test",
        "USR_CreateTimestamp": "2019-04-01 07:02:13",
        "USR_UpdateUser": "test",
        "USR_UpdateTimestamp": "2019-04-01 07:02:13",
        "id": 3
    },
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3RcL2xhcmF2ZWwtand0XC9wdWJsaWNcL2FwaVwvcmVnaXN0ZXIiLCJpYXQiOjE1NTQxMDIxMzQsImV4cCI6MTU1NDEwNTczNCwibmJmIjoxNTU0MTAyMTM0LCJqdGkiOiJ2UGFURmNVbFE2WHVSY251Iiwic3ViIjozLCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.Vi75d8uVSpzR_VddgfhJVGcyaNd-MsvjazPuUy81RXg"
}

但是在那之后,当我尝试使用该新用户登录时,我无法获得令牌。结果始终是:

{
    "message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select * from `_user` where `name` = jschneider@patinfo.ch limit 1)",
}

我如何配置凭据以使字段名称与USR_UserName匹配,并且字段密码与USR_Password匹配?

我的自定义用户类

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = '_user';
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'USR_NoRole', 'USR_CompleteName', 'USR_UserName', 'USR_Password', 'USR_BBlocked', 'USR_BAssociation', 'USR_NoPeriode', 'USR_NoEntite', 'USR_CreateUser', 'USR_CreateTimestamp', 'USR_UpdateUser', 'USR_UpdateTimestamp'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'email_verified_at' => 'datetime',
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

}

config / auth.php文件

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

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

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

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

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

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

谁能解释我如何设置应用程序并基于这两个字段创建令牌?

0 个答案:

没有答案