Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to generate token using tymon jwt in laravel

Tags:

php

laravel

api

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.

like image 603
schenker Avatar asked Jan 08 '17 06:01

schenker


1 Answers

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 {
like image 139
Ohgodwhy Avatar answered Sep 22 '22 07:09

Ohgodwhy