I'm stuck with this for about 3 days. Basically, i'm trying to generate a JWT token in laravel using Tymon. This is my controller file.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use JWTAuth;
use JWT;
use Tymon\JWTAuthExceptions\JWTException;
use Tymon\JWTAuth\Contracts\JWTSubject as JWTSubject;
class AuthenticateController extends Controller
{
public function index()
{
//
}
public function authenticate(Request $request)
{
$user = User::where('email', $request->only('email'))->first();
dd($user); //This does show some output
$token = JWTAuth::fromUser($user); //returns error message
return ["error" => NULL, "token" => $token];
}
}
I tested this api using Chrome postman, but it is reporting this error:
ErrorException in JWT.php line 73: Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in /Users/shankerm/mccadmin/laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 217 and defined
Pls give me some advice. Im new to Laravel and struggling with this for a long time. Thanks.
You are using the newer version of the package. This requires that the User Model
implements this contract. Solve it by doing this in your model:
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements JWTSubject {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With