【问题标题】:Class 'Illuminate\Foundation\Auth\User' not found JWT Auth Laravel未找到“Illuminate\Foundation\Auth\User”类 JWT Auth Laravel
【发布时间】:2018-02-11 07:49:50
【问题描述】:

我已经编写了使用 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

【问题讨论】:

    标签: php laravel laravel-5.1 jwt json-web-token


    【解决方案1】:

    在你的控制器中我猜你忘记使用你的模型“用户”将它添加到命名空间声明下面,或者它与 Illuminate\Foundation\Auth\User 冲突

    use\App\User;
    

    您必须运行以下命令:

    composer update
    composer dump-autoload
    

    【讨论】:

    • 是的,我使用了用户模型。但错误存在
    • 你更新你的作曲家依赖了吗?检查编辑的答案
    【解决方案2】:

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

    <?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'];
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-03
      • 1970-01-01
      • 2020-07-16
      • 2017-04-30
      • 2014-08-07
      • 2020-12-30
      • 2020-08-16
      • 2021-12-08
      • 2018-06-20
      相关资源
      最近更新 更多