Random 400 token_invalid errors with Laravel / jwt-auth and Angular / Satellizer app

I have an Angular application that uses the API that I created in Laravel, and I use jwt-auth to manage the tokens and the front end satellite to send a token with every request.

My live environment (both for the interface and the API), which will be transferred to another server after the application is completed) currently consists of 2 AWS EC2 instances working with nginx with a load balancer. Both servers have the same jwt private key.

However, and at the moment I cannot work out any pattern, I randomly get 400 "token_invalid" errors returned from my api. This is not one specific api route, just like every time you download an application. When I get a 400 error, for example, from my / clients endpoint, other requests will return 200. Next time, everyone will return the 200th. After that, I can get 200 returned for / clients, but 400 errors for / users.

Could this be a problem with me using a load balancer? The jwt secret key, as I said, is the same on both servers - since all the code is in GIT.

I do not use jwt.refresh middleware.

Another thing worth mentioning is that I never get the 400 errors returned when starting the application locally through Homestead, only during production.

EDIT - it seems that it is logging out (which clears both my user object (only basic data) and the token from the local storage, clearing my cache, and then most often leads to an error - is this useful?

Below is an example of one of my api calls.

App.js

.service('ClientsService', function($http, $q, __env) {
    this.index = function () {
        var deferred = $q.defer();
        $http.get(__env.apiUrl + '/clients')
            .then(function successCallback(response) {
                    console.log(response.data);
                    deferred.resolve(response.data);
                },
                function errorCallback(response) {
                    console.log(response);
                });
        return deferred.promise;
    }
})

ClientsController.js

.controller('ClientsController', function(ClientsService, $stateParams, $mdDialog, $mdToast) {
            var vm = this;
            ClientsService.index().then(function(clients) {
                console.log('ClientsCtrl init');
                vm.clients = clients.data;
            });
            // other controller code
})

I'm really trying to debug this, so any help would be greatly appreciated. If you need more information, please let me know.

+4
source share

Source: https://habr.com/ru/post/1652040/


All Articles