Muthu
Muthu

Reputation: 209

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

I have written code for registration and login using JWT authentication. In this code registration function works fine but login function doesn't works. Login function prompts an error as Class 'Illuminate\Foundation\Auth\User' not found

My user model is

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

My UserController is

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);
    }
}

The login function shows the error as

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

Upvotes: 2

Views: 5656

Answers (2)

Muthu
Muthu

Reputation: 209

The problem exists with my user model and I solved it

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

Upvotes: 0

Sletheren
Sletheren

Reputation: 2486

In your controller I guess you forgot to use your model "User" add it below the namespace declaration, Or it's a conflict with Illuminate\Foundation\Auth\User

use\App\User;

And you must run the following:

composer update
composer dump-autoload

Upvotes: 0

Related Questions