MTA
MTA

Reputation: 1073

Laravel 5.4: JWTAuth, ErrorException in EloquentUserProvider.php

I am a newbie of laravel, so it might be my mistake. Using laravel with tymondesigns/jwt-auth to verify user. I am watching this tutorial Complete JWT-AUTH api with Laravel and followed every step, the tymon package installation and logging in user. But i am getting this error. I posted code below, tell me if you need more code from any other file.

ErrorException in EloquentUserProvider.php line 120: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given

This is my user model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

    class User extends Model
    {
        protected $hidden = ["password"];
        protected $fillable = [
                                "id",
                                "name",
                                "password",
                                "mobile_number",
                                "gender",
                                "age",
                                "company_name",
                                "profile_image",
                                "email"
                            ];
    }
?>

This is my ApiAuthController.php

use JWTAuth;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;

class ApiAuthController extends Controller
{
    public function authenticate(){
        $credentaials = request()->only('email', 'password');
        print_r($credentaials);

        try {
            $token = JWTAuth::attempt($credentaials);
            if(!$token){
                return response()->json(['error'=>'invalid credentaials'], 401);
            }
        } catch (JWTException $e) {
            return response()->json(['error'=>'something went wrong'], 500);
        }
        return response()->json(['token'=>$token], 200);
    }
}

User store function in my UsersController:

public function store(Request $request)
    {
        $payload = json_decode($request->payload, true);

        $validator = Validator::make($payload, $this->rules);

        if ($validator->passes()) {

            $user = (new User())->fill($payload);
            if($user->save()){

                $response = [
                    "msg" => "User created",
                    "link" => "/api/users/" . $user->id
                ];
                return response()->json($response, 200);
            }

            $response = [
                "msg" => "An error occured"
            ];

            return response()->json($response, 404);

        } 
        else {
            return response()->json($validator->messages(), 404);
        }        
    }

In storing user request, payload is key and value is json object, the small sample object is given below:

payload={
  "name": "Alexa",
  "email": "[email protected]",
"password":"12345",
  "gender": "Male",
  "age": 24
}

Upvotes: 0

Views: 408

Answers (1)

Sandeesh
Sandeesh

Reputation: 11906

Add this to your model

use Illuminate\Foundation\Auth\User as Authenticatable;

and change this line

class User extends Authenticatable

Edit : Looks like you're storing passwords in plaintext. Add this to your user model.

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value);
}

Upvotes: 1

Related Questions