Part1: SenchaTouchEmail + FacebookLoginSystemwithPHP_MySQL

Source: Internet
Author: User
Part1: SenchaTouchEmail + FacebookLoginSystemwithPHPamp; amp; MySQLBackend One really common pattern found in mobile applications is a dual email + Facebook log in system. Facebook is a very popular sign in method, but not EverybodyHas an account and not everybody wants to use their Facebook account to sign in to new services. This makes giving the option to users to use either email or their Facebook account to sign up very attractive.

There seems to be very little (that I cocould find at least) information out there on how to set up a log in/authentication system in Sencha Touch. so I'm writing a series of blog posts that will cover how to set up a log in system from scratch with the option to sign up with either email or a Facebook account. the application will have a PHP & MySQL backend and will also use PhoneGap Build.

Topics covered over the coming weeks will include:

  • Setting up the database and API (this post)
  • Creating the application and screens
  • Email sign up system
  • Authentication and generating sessions for users
  • Auto login/remember me for return visits
  • Integrating a Facebook signup system

In this first part, I will walk you through setting up your database and setting up some PHP files on your server that are ready to receive CILS from your application.

Setting up the database

We're re going to have to create a MySQL database first of course so go ahead and do that, calling it whatever you wish. once you 've created the database, add the following table to it:

CREATE TABLE `users` (`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,`fbid` VARCHAR(255),`email` VARCHAR(100) NOT NULL,`password` VARCHAR(255) NOT NULL,`session` VARCHAR(255),PRIMARY KEY (`id`));

This will allow us to store the details the users uses to sign up, as well as any other details we wowould like to track. you coshould go ahead and add some other fields like 'first _ name', 'phone' and so on if you wish. if you're building out a real application, you're likely going to require other tables as well, but this is all we need to actually get the log in system working.

Setting up the API

Throughout the log in and sign up processes, our application will be making cballs to an API hosted on the Internet. this API will be created with PHP files which will interact with our database, and then return data in a JSON format to our application.

Although we are not implementing it just yet, our application will make callto our server using an Ajax proxy. the problem with this is that we can run into some Cross Origin Resource Sharing errors which might look something like the following:

XMLHttpRequest cannot load [URL]. No 'access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost' is therefore not allowed Access.

XMLHttpRequest cannot load [URL]. Origin [URL] is not allowed by Access-Control-Allow-Origin.

Request header field Content-Type is not allowed by Access-Control-Allow-Headers

Essential, we're re running into security problems because we're re making requests to a domain different to the one the application is hosted on. I wrote a blog posts on this recently so if you 'd like more information you cancheck it out here.

What we want to do now though is make sure we don't run into any of these errors by making sure we set our headers (and everything else) correctly from the beginning. now I need you to create a file called 'users. php 'and add the following code to it:

 

What we are doing here is first connecting to the database-you will have to replace these details with your own. next we are grabbing the 'action' which will be passed into the API through the URL. for example: http://www.example.com/api/users.php? Action = something. Eventually we will cycle through this 'action' variable to perform the appropriate action. When we want to log a user in we could make an Ajax request to 'Users. php? Action = login 'or if we wanted to log a user out' users. php? Action = logout' and so on.

Before outputting the result, we are also setting our headers here. these headers are necessary to overwrite the default Cross Origin Resource Sharing options and to prevent the errors above. if you read the CORS article I linked abve you will notice that you can set these headers at the server level instead of directly in your PHP files.

At the end of this file we are outputting '$ result '. if you were to visit this page in your browser you wocould simply see '{'success': false }'. this is a JSON formatted string that is telling us that whatever operation was just already Med on the server was not successful. in later parts of this tutorial series we will of course be outputting different data through this JSON string that will indicate whether a user successfully logged in, what their session key is and so on. this same format can be used to send in large, complex data including any and all details we had stored about a user in the database.

This concludes the first part of this tutorial series. at this point we have our database set up and our API ready to be added to and interacted. stay tuned for the following posts in this series; I'll update with a link to Part 2 here when it is ready! Feel free to sign up to the fortnightly newsletter in the bar on the right for updates on any new blog posts.

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.