“Class ‘Tymon\\JWTAuth\\Middleware\\BaseMiddleware’ not found in Laravel 9.”

huangapple go评论68阅读模式
英文:

Class \"Tymon\\JWTAuth\\Middleware\\BaseMiddleware\" not found in laravel 9

问题

In Laravel 9,我正在使用tymon\jwt-auth包。在composer.json中,我添加了以下内容:

"tymon/jwt-auth": "2.x"

在config/app.php的providers数组中,我添加了以下内容:

Tymon\JWTAuth\Providers\LaravelServiceProvider::class,

以及别名:

'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,

在app\Http\Kernel.php中,我定义了自己的中间件Customer.php,并导入了以下内容:

use Tymon\JWTAuth\Middleware\BaseMiddleware;

但是上面的类未找到。我将use语句替换为:

use Tymon\JWTAuth\Http\Middleware\Authenticate;

但是这个类已经被弃用。请问如何解决这个错误?我已经花了三天时间但没有进展。Http\Middleware中的所有类都已被弃用。

(Note: The above response includes translations of the code and text you provided without additional information or answers to specific questions.)

英文:

In laravel 9 I am using tymon\jwt-auth package.
In composer.json"tymon/jwt-auth": "2.x",

In config/app.php in providers array i added

Tymon\JWTAuth\Providers\LaravelServiceProvider::class,

and aliases

'JWTAuth'=>Tymon\JWTAuth\Facades\JWTAuth::class,  
'JWTFactory' =>Tymon\JWTAuth\Facades\JWTFactory::class,

app\Http\Kernel.php

protected $routeMiddleware = [
    ...
    'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
    'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
   ];

config\auth.php

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

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

I am writing my own middleware named as Customer.php and importing this

use Tymon\JWTAuth\Middleware\BaseMiddleware;

in my middleware, but the above class is not found. I replaced the use statement as

use Tymon\JWTAuth\Http\Middleware\Authenticate;

But this class is depricated.

“Class ‘Tymon\\JWTAuth\\Middleware\\BaseMiddleware’ not found in Laravel 9.”

Kindly help how can I get rid of this error. I have spent three days but no progress yet. All the classes in Http\Middleware are deprecated.

答案1

得分: 1

以下是您要翻译的内容:

"The classes are marked as deprecated in the repository and the maintainer likely has a reason to mark them as such. I have not found any hints about this middleware in the documentation either.

I suggest using the documented authorization method in a custom middleware without relying on the BaseMiddleware:

<?php
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class EnsureUserIsAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (! auth()->validate($credentials)) {
            // credentials are invalid, redirect user to login?
        }
 
        return $next($request);
    }
}

and then use that middleware on your routes with the standard middleware configuration in the Kernel.php

protected $middlewareAliases = [
    'jwt-authenticated' => \App\Http\Middleware\EnsureUserIsAuthenticated::class,
];
英文:

The classes are marked as deprecated in the repository and the maintainer likely has a reason to mark them as such. I have not found any hints about this middleware in the documentation either.

I suggest using the documented authorization method in a custom middleware without relying on the BaseMiddleware:

&lt;?php
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class EnsureUserIsAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (! auth()-&gt;validate($credentials)) {
            // credentials are invalid, redirect user to login?
        }
 
        return $next($request);
    }
}

and then use that middleware on your routes with the standard middleware configuration in the Kernel.php

protected $middlewareAliases = [
    &#39;jwt-authenticated&#39; =&gt; \App\Http\Middleware\EnsureUserIsAuthenticated::class,
];

huangapple
  • 本文由 发表于 2023年4月17日 18:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定