1
votes

I have an Azure Mobile Services table in which the read permission is set to "Only Authenticated Users".

I followed the example here https://azure.microsoft.com/en-us/documentation/articles/mobile-services-html-get-started-users/ and am authenticating users using the following code:

    function logIn() {
    client.login("microsoftaccount").done(function (results) {
        console.log(results);
        //save auth data to local storage
        sessionStorage.loggedInUser = JSON.stringify(client.currentUser);

        //route to the next page based on the type of user that is logged  in
        getUserByUserID();
    }, function (err) {
        alert("Error: " + err);
    });
}

I store the client.currentUser information in sessionStorage on the local machine once the user is authenticated. I then navigate to another web page where I create a new instance of the MobileServiceClient and update the client variable's currentUser variable from the sessionStorage mentioned previously. I use the following code to do that:

//load the session data into the client object
if (sessionStorage.loggedInUser) {
    client.currentUser = JSON.parse(sessionStorage.loggedInUser);
    console.log("getting stored user: " + JSON.parse(sessionStorage.loggedInUser));
}

However, I get a "401: Unauthorized error" when trying to read from the table using the code below:

var query = client.getTable("towingProfiles").read().done(function (results) {
    console.log(JSON.stringify(results));
    _.each(results, function(towingProfile){
        towingProfiles.add(towingProfile);
    })
}, function (err) {
    alert("Error: " + err);
});

I'm not sure why this isn't working, and would appreciate any help.

1

1 Answers

0
votes

The problem was that I wasn't setting the correct application key when making the MobileServiceClient.