我已经使用JWT身份验证编写了注册和登录代码。在这个代码注册功能工作正常,但登录功能不起作用。登录功能提示错误为Class' Illuminate \ Foundation \ Auth \ User'找不到
我的用户模型是
<?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'];
}
我的UserController是
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
答案 0 :(得分:0)
在你的控制器中我猜你忘了使用你的模型&#34;用户&#34;将其添加到命名空间声明下方,或者它与Illuminate\Foundation\Auth\User
use\App\User;
您必须执行以下操作:
composer update
composer dump-autoload
答案 1 :(得分:0)
我的用户模型存在问题,我解决了它
<?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'];
}