Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Titanium Mobile

Titanium User Authentication: Part 2

Read Time:9 minsLanguages:
This post is part of a series called Titanium User Authentication.
Titanium User Authentication: Part 1
Titanium User Authentication: Part 3

Welcome to part 2 of our 3 part series on user authentication with Titanium Mobile. Titanium is an open source cross compiler that allows you to write iPhone and Android (soon to be blackberry too!) applications using Javascript. No Objective-C required! We will be using PHP as the server side code and my database will be MySQL. For this example, I am using MAMP to develop locally. I strongly recommend that you go through the first part of this series before continuing if you haven't already. However, you can alternatively download the source from part 1, create the database table, and setup the PHP database connections on your own before skipping to this tutorial if you would like.


In part 1, we setup the database for our app and added a user. We then made our login interface by creating a tab group, a tab, and a window. We then gave our login button some actions. Our PHP file queried our database and, upon successful login, it returned our name and email. If login authentication failed, we returned a string simply stating invalid username and/or password. In part 2, we will create a new tab on the main screen that allows a user to create a new account and then login.

Step 1: Creating the Account Window and Tab

Open up app.js and create the account window and tab underneath our login tab script. Also note that I removed the tabBarHidden property on the login window that we did in part 1. Removing that property allows us to see the tabs on the bottom of the phone. We have also added the accountTab to the tabGroup.

The URL property on the account var tells the compiler to use account.js as our window. If you skip this part, Titanium will throw an ugly red error in the emulator. Upon a successful compile, your screen should look like this:

Traditionally, you would see the tab bar on the bottom have icons. Well, with Titanium, that is super easy too! Simply use the icon property in each tab. For example:

Step 2: Create account.js

Create a new file and name it account.js and save it in your Resources/main_windows folder. This is the same place we saved our login.js file in part 1.

Okay, this mean looking block of code is really super easy to understand, yet it does so much for us. Just by looking at our variable names, it is pretty easy to decipher what is going on here. We created 5 fields:

  • Username
  • Password1
  • Password2
  • Name
  • Email Address

We also made our 'create account' button.

You will also notice the var at the top called scrollView. Adding the objects to a scroll view allows the view to be scrollable so when the keyboard slides up, it isn't overlapping our text fields.

Go ahead and compile. Upon a successful compile, your screen should look like this after switching to the account tab. The create account button doesn't do anything yet, but play around with selecting text fields and see how the scroll view works.

Step 3: Adding the Click Event to Our Button

We now need to create an event listener on our button so when they click 'create account', it sends the data off as well as some validation.

Starting from the top, the checkEmail() method is a simple function that uses Regular Expression to check if the email the user inputs is the correct format. We created a new HTTPClient that will be used to send our data to our PHP file.

In the click event, we first check if any fields are empty. If they are, alert them saying "All fields are required." Our next check is to see if the two password fields are the same. If they aren't, alert them saying "Your passwords do not match." Our final check is to check if the email address is valid. If it isn't, alert them saying "Please enter a valid email."

Once the form is validated, it will alert "Everything looks good so send the data." Go ahead and compile and test submitting the form with no values, non-matching passwords and an invalid email. Upon submitting a valid form, you will see the alert below:

Step 4: Actually Send Some Data

Go ahead and delete the "Everything looks good so send the data" line. We need to replace that with the open() and send() methods.

So, in replacing that line, we disable our 'create account' button and set the opacity to 30%. We then take the HTTPClient we made and call the open() method on it. It is pointing at a PHP file that we will make in the next step. We then create a params object to contain all the form data. Notice I am running an MD5 encryption on the password field. Final step is to call the send() method and pass it our params object.

Step 5: Creating our Register PHP File

This file will be the PHP file our app talks to when hitting the 'create account' button. The name must reflect the name in our createReq.open() method in the previous step. I've named mine post_register.php. Replace my mysql_connect and mysql_select_db settings with your connection settings.

So here we connect to our database and select the database named 'test' (that name will change depending on the name of your database obviously). You can see our $_POST variables reflect the names we set in the params object in our last step. That part is crucial. We then see if the username or email address they entered already exists. If it doesn't, insert the the data into the database. Okay, don't compile just yet! We will next step, I promise.

Step 6: Receiving Data in account.js

Okay back to account.js. Let do some data handling for when our PHP returns something. Place this code under var createReq and above our click event:

So when PHP returns something, if 'this.responseText' is equal to "Insert failed" OR "That username or email already exists," remove the overlay window (so they can re-enter information and submit) and alert them with 'this.responseText'.

Upon successful registration, alert them with "Thanks for registering. You may now login" (defined in our post_register.php file). We also add an event listener to the OK button so when clicking it, it automatically takes us to the login screen.

If the alert coming back is some garbled message about mysql_connect and/or access denied, then you need to check your mysql connection settings in the PHP.


In part 2 of this series, we added in tabbed windows that you can switch between. We then made a new form where a user can input data into and submit it. Upon submitting we did some form validation and then had our PHP return a message based on if the data was in use and, if not, we successfully inserted it. I hope you enjoyed reading this mini series tutorial as much as I enjoyed writing it!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.