Blog

Backendless and Corona: Retrieving user data

by on June 19, 2014

Back for round #2, eh? (if you missed the first post which was about registering users, read it here) Well, let’s dive in. This one should be pretty quick.

The following code is pretty well commented, ping me if you have any questions.

json = require ("json")
-- Create a table to hold our headers well be passing to
-- backendless. This is how Backendless
local headers = {}
headers["application-id"] = "Your Info Here"
headers["secret-key"] = "Your Info Here"
headers["Content-Type"] = "application/json"
headers["application-type"] = "REST"
-- Event handler for our asynchronous webcall
local function RetrieveUserData( event )
    if ( event.isError ) then
        myText.text = "Network error!"
    else
        -- Decode the contents of the remote table of users
        -- into a table for Corona to use...
        local response = json.decode(event.response)
        print("Total users "..response.totalObjects)
        -- Remember, 'response' is just a table to hold info.
        -- The user info youre looking is in the sub table key/pair
        -- where key=="data". Check it:
        for key,value in pairs(response) do
            -- HERE'S where the magic happens...
            -- Let's drill down...
            if(key=="data")then
                -- Now in HERE is each of the users and their
                -- info contained in table format. Check it:
                for users,info in pairs(value) do
                    -- And to cycle through all fields of data
                    for fieldKey,fieldValue in pairs(info) do
                        print(fieldKey,fieldValue)
                    end
                end
            end
        end
    end
end
local params = {}
params.headers = headers
-- To retrieve list of users
network.request( "https://api.backendless.com/v1/data/users", "GET", RetrieveUserData,params)

This should bring back something similar to:

Total users 1
created 06/18/2014 02:56:47 GMT+0000
userStatus      ENABLED
username        fowler
objectId        15653DFD-8D12-D416-FF09-8457A6F0DE00
email   amyfarrahfowler@bigbangtheory.com

Notice the Backendless specific things, we didn’t create those fields in our previous post when we added this user to the database.

  • objectId
  • userStatus

Don’t panic, Backendless adds these automatically. Here’s our expected fields we created in the first post:

  • username
  • email
  • password…..wait, what the?!?! Where’s the password?!

I was expecting to be able to see the password when we query the user info, however, for security Backendless encrypts the password info in a one-way method that even they (or you as the admin of your user table) can’t retrieve. If a user forgets their password, you’ll have to build in the functionality for them to reset it.

I don’t have all the answers, so I hit up their great community and got a response.

How do you get the data to compare what the user is entering VS what’s stored on the backend? That’s where the login functionality comes in to play. We’ll hit that up in the next post.

My mission is to keep these posts nice and bite-sized. I don’t want to overwhelm you with tons of functionality. I’m mirroring Backendless’s docs for the REST API.

See you soon!

Mario

Leave a Reply