【问题标题】:How to fix Auth driver [jwt] for guard [api] is not defined如何修复保护 [api] 的身份验证驱动程序 [jwt] 未定义
【发布时间】:2021-09-08 12:32:31
【问题描述】:

我在我的项目中设置 laravel cors 时遇到问题,但是我已经修复了 cors 问题,我使用这个包 https://github.com/fruitcake/laravel-cors 解决了它。现在这里的主要问题是,当我登录时,我的回复显示未定义保护 [api] 的身份验证驱动程序 [jwt]。我使用 laravel 作为我的 api 后端和 React Js 作为前端和我使用 JWT 的令牌。

我的问题:如何修复未定义保护 [api] 的身份验证驱动程序 [jwt]?

Laravel 版本:6.20.29

在我的 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,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

Cors.php

'paths' => ['api/*'],

【问题讨论】:

  • 你能怎么称呼吗?我的意思是身份验证尝试
  • 什么意思?
  • 你如何为用户创建令牌。你能显示该代码
  • JWT 文档里面的内容和我用的一样

标签: reactjs laravel


【解决方案1】:

尝试将默认保护设置为 web 并在 auth 中尝试将保护名称作为 api 传递。

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

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

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

在身份验证中

auth('api')->attempt

参考:https://github.com/tymondesigns/jwt-auth/issues/2015

【讨论】:

    猜你喜欢
    • 2019-05-11
    • 2017-07-25
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2018-09-11
    • 2020-08-23
    • 2017-03-13
    相关资源
    最近更新 更多