and12345
and12345

Reputation: 29

Calling API with angularjs with JWT

i'm new in programming so bear with me. Need some help on calling API using angularjs here, and I think this have some problem with JSON Web Tokens; when trying to calling the API i get:

Login Object {success: true, data: Object}

Object {success: false, error: "User not logged in

        $scope.login = function () {
            var url = "";
            url = 'http://localhost.net/api/login';
            $.post(url, {email: "admin", password: "admin"}, function (data)
            {
                console.log("Login", data);

                url = 'http://localhost.net/api/device/get';
                $.post(url, {}, function (data)
                {
                    console.log("Devices", data);
                }).fail(function (data) {
                    console.log("Devices", "login_failed");
                });

                url = 'http://localhost.net/api/async';
                $.post(url, {}, function (data)
                {
                    console.log("Live Data", data);
                }).fail(function (data) {
                    console.log("Live Data", "login_failed");
                });

                url = 'http://localhost.net/api/command/send';
                $.post(url, {deviceId: 2, type: "Stop"}, function (data)
                {
                    console.log("Command", data);
                }).fail(function (data) {
                    console.log("Command", "login_failed");
                });

            }).fail(function (data) {
                console.log("Login", "login_failed");
            });
        };

Upvotes: 0

Views: 74

Answers (2)

FesterCluck
FesterCluck

Reputation: 320

The provided code shows no implementation of JWT, and uses the jQuery Ajax system to make it's requests. It's confusing to know where the "JWT Problem" is. Are you using angular-jwt? If so automatic token handling is provided through $http, not $.ajax, which may be your problem. More information about the implementation of that library can be found here

If this isn't the case, I recommend reviewing the implementation described here .

Upvotes: 0

B. McCartney
B. McCartney

Reputation: 162

According to this documentation, you're not sending a token along with your JWT request:

https://scontent-ord1-1.xx.fbcdn.net/hphotos-xat1/v/t1.0-9/12109126_10203610003569176_8949255308934231993_n.jpg?oh=778476213b57b2815aadee006fe7cf79&oe=56C3F0FB

Upvotes: 1

Related Questions