Class 'Illuminate\Foundation\Auth\User' 未找到 JWT 验证 Laravel

Class 'Illuminate\Foundation\Auth\User' not found JWT Auth Laravel

我写了使用JWT认证注册登录的代码。在此代码中,注册功能工作正常,但登录功能不起作用。登录功能提示错误 Class 'Illuminate\Foundation\Auth\User' not found

我的用户模型是

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    protected $table = 'users';
    public $timestamps = false;
    protected $primaryKey = 'user_name';
    protected $fillable = ['user_name','password'];
}

我的用户控制器是

class UsersController extends Controller
{

    public function login()
    {
        $credentials = request()->only('user_name','password');
        try{
            $token = JWTAuth::attempt($credentials);
            if($token){
                return response()->json(['error'=>'invalid_credentials'],401);
            }
        }
        catch(JWTException $e){
            return response()->json(['error'=>'something went wrong'],500);
        }
        return response()->json(['token'=>$token],200);
    }

    public function register()
    {
        $user_name = request()->user_name;
        $password = request()->password;
        $user = User::create([
            'user_name'=>$user_name,
            'password'=>bcrypt($password)
        ]);

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

        return response()->json(['token'=>$token],200);
    }
}

登录函数显示错误为

Class 'Illuminate\Foundation\Auth\User' not found

在你的控制器中我猜你忘了使用你的模型 "User" 将它添加到命名空间声明下方,或者它与 Illuminate\Foundation\Auth\User

冲突
use\App\User;

并且您必须运行满足以下条件:

composer update
composer dump-autoload

我的用户模型存在问题,我已解决

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{use Authenticatable, CanResetPassword;

    //
    protected $table = 'users';
    public $timestamps = false;
    protected $primaryKey = 'user_name';
    protected $fillable = ['user_name','c_name','accessibility_level','password','role','contact_number','address'];
}