Learned Something About: Mobile Apps

iPhone

1 in 4 American teens owns a smartphone. Now couple that with the fact that an Android user spends almost two-thirds of his/her mobile time using apps. Now you see why DoSomething.org sees the mobile app as an essential to communicate with teens. As DoSomething.org's mobile developer, I've been focused for months on releasing our mobile app (which comes out this Sunday).

The challenge: we needed the app to work with our website's content management system (CMS). Like most not-for-profits, we use Drupal. The CMS offers comprehensive user management and easy content creation (both powerful tools that could be used by the app). Unfortunately, Drupal core doesn't provide support for external applications to access these features. So how do we bring this same functionality to a mobile app? Our solution is the Services module.

The Services module provides Drupal a web service API. Through defined URL endpoints, we can expose access to user registration and login, and content creation and retrieval. We used Services 7.x-3.1, and while it accomplishes a lot on its own, there were a few more additions we made for it to really get us all the way. Our changes went into a custom dosomething_services module. Here are several ways we extended Services to fulfill our needs.

  1. Email Logins. Services out-of-the-box supports logins via username, but users on our site can create an account and login using only their email addresses. For the app to support the same login paradigm for our existing members, we needed to also allow logins via an email address.
    In our dosomething_services.module, within the hook_services_resources_alter() implementation, we overrode the user login callback to point to our custom callback instead.

    $resources['user']['actions']['login']['callback'] = '_dosomething_services_user_resource_login';

    From there, we can check if the original function would have succeeded. If not, then we attempt to login with an email address.

    // Check if normal login process would've succeeded
    $return = _user_resource_login($username, $password);
    ...
    // Otherwise, process username as an email address
    $email = $username;
    $account = user_load_by_mail($email);
    if (count($account) > 0 && $account->name) {
       $return = _user_resource_login($account->name, $password);

     
  2. Facebook Logins. Supporting Facebook logins was a new feature added to our site when we did the redesign a couple months ago. This was something we definitely needed for the app as well. An entire Learned Something post could be dedicated to how we did this, but a primary module to look into to get started with is the Facebook OAuth (fboauth) module. We created a new Services resource in hook_services_resources() that accepts a user access token provided by Facebook.

    ...
    'user' => array(
        'actions' => array(
           'fblogin' => array(
              'callback' => '_dosomething_services_facebook_login',
                 'args' => array(
                    array(
                        'name' => 'access_token',
                        'type' => 'string',
    ...


    The callback then uses the access token with fboauth functions to query facebook user information, login the user, or create a new Drupal user if no existing one is found.
     
  3. Use the Webform Service module. Webforms drive the sign up and report backs used by almost all the campaigns on our site. They're a main avenue for us to hear back from our members. To open up this same avenue through Services, there was another contrib module we brought in called Webform Service. While it has not been recently maintained, the module did accomplish most of what we needed. With the module enabled, exposing its available resources is done with just a couple clicks similar to the rest of the Services module.



    We only use it to create webform submissions from the app, but retrieve, update and delete functions are also available.

Try the Mobile App

The DoSomething.org app is now available. Take a break from Temple Run, and give it a try.

Get it on Google Play

- Jon Uy is a Mobile Developer (and expert juggler) at DoSomething.org.