Search code examples
phpapilaraveldingo-api

Unable to authenticate with invalid token error in dingo/api with jwt-auth


I'm using dingo/api (that has built-in support for jwt-auth) to make an API.

Suppose this is my routes :

$api->group(['prefix' => 'auth', 'namespace' => 'Auth'], function ($api) {
            $api->post('checkPhone', 'LoginController@checkPhone');

            //Protected Endpoints
            $api->group(['middleware' => 'api.auth'], function ($api) {
                $api->post('sendCode', 'LoginController@sendCode');
                $api->post('verifyCode', 'LoginController@verifyCode');

            });
        });

checkPhone method that has task of authorize and creating token is like :

public function checkPhone (Request $request)
        {
            $phone_number = $request->get('phone_number');
            if (User::where('phone_number', $phone_number)->exists()) {

                $user = User::where('phone_number', $phone_number)->first();

                $user->injectToken();

                return $this->response->item($user, new UserTransformer);

            } else {
                return $this->response->error('Not Found Phone Number', 404);
            }
        }

And injectToken() method on User Model is :

public function injectToken ()
        {
            $this->token = JWTAuth::fromUser($this);
            return $this;
        } 

Token creation works fine.

But When I send it to a protected Endpoint, always Unable to authenticate with invalid token occures.

The protected Endpoint action method is :

public function verifyCode (Request $request)
        {
            $phone_number = $request->get('phone_number');
            $user_code    = $request->get('user_code');

            $user = User::wherePhoneNumber($phone_number)->first();

            if ($user) {
                $lastCode = $user->codes()->latest()->first();

                if (Carbon::now() > $lastCode->expire_time) {
                    return $this->response->error('Code Is Expired', 500);
                } else {
                    $code = $lastCode->code;

                    if ($user_code == $code) {

                        $user->update(['status' => true]);

                        return ['success' => true];
                    } else {
                        return $this->response->error('Wrong Code', 500);
                    }
                }
            } else {
                return $this->response->error('User Not Found', 404);
            }
        }

I used PostMan as API client and send generated tokens as a header like this :

Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI5ODkxMzk2MTYyNDYiLCJpc3MiOiJodHRwOlwvXC9hcGkucGFycy1hcHAuZGV2XC92MVwvYXV0aFwvY2hlY2tQaG9uZSIsImlhdCI6MTQ3NzEyMTI0MCwiZXhwIjoxNDc3MTI0ODQwLCJuYmYiOjE0NzcxMjEyNDAsImp0aSI6IjNiMjJlMjUxMTk4NzZmMzdjYWE5OThhM2JiZWI2YWM2In0.EEj32BoH0URg2Drwc22_CU8ll--puQT3Q1NNHC0LWW4

I Can not find solution after many search on the web and related repositories.

What is Problem in your opinion?

Update :

I found that not found error is for constructor of loginController that laravel offers :

public function __construct ()
        {
            $this->middleware('guest', ['except' => 'logout']);
        }

because when I commented $this->middleware('guest', ['except' => 'logout']); all things worked. But if I remove this line is correct? How should be this line for APIs?


Solution

  • As I mentioned earlier as an Update note problem was that I used checkPhone and verifyCode in LoginController that has a check for guest in it's constructor.

    And because guest middleware refers to \App\Http\Middleware\RedirectIfAuthenticated::class and that redirects logged in user to a /home directory and I did not created that, so 404 error occured.

    Now just I moved those methods to a UserController without any middleware in it's constructor.