I need to login mobile user. I'm getting the access_token from the app. What are the next steps to create local user (if needed) and then login him?

Comments

eugen80’s picture

Version: 6.x-3.1 » 7.x-3.x-dev
Component: I am lazy and not paying attention » Code
Dave Cohen’s picture

Status: Active » Fixed

This documentation should help: http://drupal.org/node/932690

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

eugen80’s picture

Status: Closed (fixed) » Active

I've checked the documentation, there are no API examples how to link facebook account with drupal account

eugen80’s picture

once again, the problem:
There is iphone app (frontend) and drupal (backend) with services module. User want to login using Facebook. So,
drupal will get access_token from the mobile app. I need to proccess it and do some tasks:
- get user data from facebook
- create local drupal account if needed
- login user using drupal account

The question is, how "Drupal for Facebook" can assist me with these tasks?

eugen80’s picture

at least it would be helpful to know which functions may help me

Dave Cohen’s picture

If the pages we've already linked to (i.e. http://drupal.org/node/933772) don't help you, try looking at fb_user.module and maybe fb_example.module for examples of what to do.

It sounds like you're using drupal as a service, not a web site. So I doubt all the normal facebook cookies and so on will be present. You might end up explicitly passing the access token to drupal, then using fb_graph(), taking care to always pass in an appropriate token.

eugen80’s picture

Thank you, this problem is solved.. Another problem is: how can logged in user (signed in using FB) change it's profile data, I'm checking the password before changing any profile data. Bun noone knows password for a user signed in using facebook. Password was hashed on creation and saved into the DB.

Is it security issue, if I would NOT use user_check_password if user wants to update it's profile and the user was created using FB?

Dave Cohen’s picture

Status: Active » Fixed

Another problem needs to be in another thread. It's hard enough to maintain this issue queue as it is.

Unclear from the description above whether you're doing something special (user_check_password) or whether that is D7 default behavior. Please specify when you submit another issue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mottolini’s picture

Title: How to login user? » How to login user from a mobile app (PhoneGap/Cordova, etc.)?

I have the same problem. I found a solution hacking the function _fb_user_process_authorized_user in fb_user.module.
_fb_user_process_authorized_user calls (not directly) the function getUser() in the facebook php api. getUser doesn't seem to be able to recognize a facebook user if the call comes from a web service instead of a traditional web page load.
So, what I did is to copy _fb_user_process_authorized_user in my module and have my web service receive a facebook user id from my mobile app. The workflow for my mobile app is:

  1. Connect to facebook on the mobile device
  2. If connection is OK, call a web service on drupal, passing the facebook user id (fbu)
  3. My web service does everything _fb_user_process_authorized_user does except getting the fbu from the facebook php api and calling user_external_login at the and instead of _fb_user_external_login

here is my web service function (fragment - based on 6.x-3.1):

  $fbu = [SOME VALUE COMING FROM WEB SERVICE CALL];
  $mapped = FALSE;

  if ($fbu &&
      (!variable_get(FB_USER_VAR_CHECK_SESSION, FALSE) || _fb_user_check_session($fbu))) {
    $fb_app = $GLOBALS['_fb_app'];
    // First check if map already exists.
    $account = fb_user_get_local_user($fbu, $fb_app);
    $config = _fb_user_get_config($fb_app);

    if (!$account) { 

      if ($GLOBALS['user']->uid > 0 &&
          $config['map_account'][FB_USER_OPTION_MAP_ALWAYS]) {
        // Create map for logged in user.
        $mapped = _fb_user_create_map();
       }
      if (!$mapped &&
          $config['map_account'][FB_USER_OPTION_MAP_EMAIL]) {
        // Create map if email matches.
        $mapped = _fb_user_create_map_by_email();
      }

      if (!$mapped &&
          $config['create_account'] == FB_USER_OPTION_CREATE_LOGIN) {
        // Create new local account with map.
        $mapped = _fb_user_create_local_account();
      }

      if ($mapped) {
        $account = fb_user_get_local_user($fbu, $fb_app);
      }
    }

    if ($account) {
      // Ensure the user has any roles associated with this app.
      $rid = $config['new_user_rid'];
      if ($account && $rid &&
          (!isset($account->roles[$rid]) || !$account->roles[$rid])) { 

        // there should be an API for this...
        db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)',
                 $account->uid, $rid);
        watchdog('fb_user', "Added role %role to existing user !username for application %app", array(
                   '!username' => theme('username', $account),
                   '%app' => $fb_app->label,
                   '%role' => $rid));
      }
 
      // Login as drupal user, if not already.
      user_external_login($account);
    }
  }

I don't like this approach because I would prefer to rely on well tested APIs rather than create a new version that will fail sooner or later and because maybe I expose my app/site to some security problem.
I would prefer to send, along my web service call, some facebook cookie to let _fb_user_process_authorized_user works as it should.
Any hint is appreciated.

Gregg Duncan’s picture

Eugen80, Could you please post how you solved this issue. There are others of us that are trying to figure this out as well.

It seems to me that in this point the evolution of websites and mobile apps that this should be a standard practice by now.

  1. Mobile app
  2. Drupal Website that also delivers Mobile app content
  3. Facebook logins to both the website and mobile app

Cordova has created the cordova facebook plugin to work with the facebook js sdk. I can login to facebook. But I have no way of logging that user into drupal and connecting the facebook user to the drupal user.

Services should be core to drupal. And the ability to do a facebook login should be a standard built into Services.

Thanks,