2017-09-02 48 views
0

我已經編寫了使用JWT驗證進行註冊和登錄的代碼。在這個代碼註冊功能工作正常,但登錄功能不起作用。登錄功能會提示一個錯誤類「照亮\基金會\驗證\用戶」未找到未找到Class'Illuminate Foundation Auth User'JWT驗證Laravel

我的用戶模型是

<?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

與我的用戶模型中存在的問題,我解決了它

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

在你的控制器我猜你忘了使用你的模型「用戶」添加它的命名空間聲明下面,或者它與Illuminate\Foundation\Auth\User

use\App\User; 

衝突,你必須運行以下命令:

composer update 
composer dump-autoload 
+0

是的,我已經使用了用戶模型。但是存在錯誤 – Muthu

+0

是否更新了你的作曲家依賴關係?檢查編輯答案 – Sletheren

+0

仍然無法正常工作 – Muthu