Backendless and Corona: Retrieving user data

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. Check my post here for a more in depth explanation of their encryption.

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

Backendless and Corona: Using API for Adding Users

This post will go over the basics of integrating Backendless services into the Corona framework…

First, create your Backendless account and obtain the “application-id” and “secret key”. Keep those handy, you’ll be using them throughout these tutorials.

At present, there is not a proper, Corona-specific API plugin so we’ll be using the REST API for Backendless. A handy link can be found here for the REST documentation.

Managing users in the Backendless

I originally was excited about Backendless for user management. I was excited at the prospect of NOT having to “roll my own”. I was pleased to see that they provided a great and simple, yet flexible user management system. Let’s not waste any time, let’s cover the basics here first and then, let’s REALLY play with the easily customization aspects!

json = require ("json")
-- Create a table to hold our headers we'll be passing to 
-- backendless. This is how Backendless 
local headers = {}
headers["application-id"] = "ENTER YOUR INFO HERE"
headers["secret-key"] = "ENTER YOUR INFO HERE"
headers["Content-Type"] = "application/json"
headers["application-type"] = "REST"
 
-- This is a "yes" press
-- OK, well write to the file and update it with the user's name
-- Create a table to put our data into
local tempTable = {}
-- Populate our table with some data...you'll need to change the usernames
-- if you plan on testing it multiple times, of course. Check on your backendless
-- dashboard and find the user table:
 
tempTable.username = "fowler"
tempTable.email = "amyfarrahfowler@bigbangtheory.com"
tempTable.password = "whooooo"
 
-- Encode the table into the expectes JSON...
local jsonData = json.encode(tempTable)
-- Debug output for your console window...
print("JSON is "..jsonData)
-- Event handler for our network.request webcall
local function RetrieveUserData( event )
    if ( event.isError ) then
        myText.text = "Network error!"
    else
        print("Response "..event.response)
        local response = json.decode(event.response)
        print("Total users "..response.totalObjects)
    end
end     
 
-- Now, we'll combine both our headers and the body into the single table "params"
-- and send this off to Backendless! 
local params = {}
params.headers = headers
params.body = jsonData
 
-- To create a user using the JSON encoded info we defined above in the 'params' table
network.request( "https://api.backendless.com/v1/users/register", "POST", RetrieveUserData, params)

If you replace headers["application-id"] and headers["secret-key"] with your own data, and run the above code, it should create a new user with the above info. Here’s what the console should come back with something similar to this:

JSON is {"username":"fowler","password":"whooooo","email":"amyfarrahfowler@bigba
ngtheory.com"}
Response {"username":"fowler","updated":null,"created":"06/10/2014 22:57:14 GMT+
0000","email":"amyfarrahfowler@bigbangtheory.com","ownerId":null,"objectId":"BC9
F50E0-6D6B-AC54-FFAD-8B30BAE50C00"}

Yours will differ of course with the unique GUID assigned to your “objectId” and the date/time.

Now, go to your backendless dashboard, and select the data icon and select “users” and you should see the user info listed there. Refresh if it doesn’t appear right away:

Your freshly added user!

It’s that simple. You’ve just created a user programatically using Backendless!