简体   繁体   English

我无法在 Laravel 中获得 JWT 令牌

[英]I can't get JWT token in Laravel

I am creating an API in Laravel 8 (PHP 7.4) and I wanted to secure it.我正在 Laravel 8 (PHP 7.4) 中创建一个 API,我想保护它。 The choice fell on tymon / jwt-auth.选择落在了 tymon / jwt-auth 上。 I follow the documentation from the website: https://jwt-auth.readthedocs.io/en/develop/quick-start/我遵循网站上的文档: https://jwt-auth.readthedocs.io/en/develop/quick-start/

Everything works fine here.这里一切正常。 However, later when I create a route to login and want to get the token using auth()->attempt() , I have the following error:但是,稍后当我创建登录路由并希望使用auth()->attempt()获取令牌时,出现以下错误:

Undefined method 'attempt'.未定义的方法“尝试”。

My Route code:我的路线代码:

Route::get('/login', function () {
    $loginData = request()->only(['email', 'password']);

    $token = auth()->attempt($loginData);

    return $token;
});

auth.php: auth.php:

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

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

What's wrong?怎么了?

You can try this line你可以试试这条线

 $token = auth('api')->attempt($loginData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM