samedi 2 septembre 2017

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire