signalpoint / facebook

The Facebook module for DrupalGap.
1 stars 0 forks source link

Connect Existing Account to Facebook #7

Open tennist opened 7 years ago

tennist commented 7 years ago

I found an issue where I wanted to provide a button for users to connect their existing accounts to their facebook account, but their facebook account was already connected to a different drupal account. When you clicked the connect button, it would log the user out and then log them into the existing connected account. This was not a good solution as it did not let the user know the account was being switched. I reworked the code so that it presents the user with an alert telling them that their facebook account is already connected to a different account and to log out of the current account then click the connect with facebook button to login. The updated facebook_connected function is as follows:

//updated function function facebook_connected(token) { try { fboauth_connect(token, { success: function(result) { if (result.message == 'Success') { system_connect(_drupalgap_deviceready_options({ reloadPage: true })); } else if (result.message == 'Your account is now connected to Facebook!') { drupalgap_alert(result.message); drupalgap_goto(drupalgap_path_get(), {reloadPage:true}); } else if (result.message == 'Your account is already connected to Facebook!') { drupalgap_goto(drupalgap_path_get(), {reloadPage:true}); } else { tokenStore = window.localStorage; tokenStore.removeItem('fbAccessToken'); drupalgap_alert(result.message); drupalgap_goto(drupalgap_path_get(), {reloadPage:true}); } }, error: function(xhr, status, message) { if (status == 406 && message[0].indexOf('Already logged in as')) { system_connect(_drupalgap_deviceready_options()); } } }); } catch (error) { console.log('facebook_connected - ' + error); } }

This modification also required some changes to the services_fboauth module on the server side. I will post the updated code to that forum as well. The updated code for the services_fboauth_connect function is as follows:

`//updated function function services_fboauth_connect($data) { global $user; $reponse = new stdClass();;

// Include fboauth functions as required. module_load_include('inc', 'fboauth', 'includes/fboauth.fboauth'); $access_token = $data['access_token'];

// Find Drupal user that corresponds with this Facebook user. $fbuser = fboauth_graph_query('me', $access_token); $uid = fboauth_uid_load($fbuser->id);

if ($data['uid'] > 0) { // User is already logged in. //Check to see if the fbid is already associated with a different drupal account $account = user_load($data['uid']); if($uid) {
if ($uid == $data['uid']) { $response = services_fboauth_login_user($account); $response->message = "Your account is already connected to Facebook!"; return $response; } else { $response = services_fboauth_login_user($account); $response->message = "Your facebook account is already associated with another account on our site. Please logout of this account and then click the 'Login with Facebook Button' to login with your existing account."; return $response; }

//The fbid is not associated with any other accounts
} else {
    // So just associate the two accounts.
   fboauth_save($data['uid'], $fbuser->id);

   $response = services_fboauth_login_user($account);
   $response->message = "Your account is now connected to Facebook!";      
   return $response;
}  

//User is not logged in yet } else { if ($uid && $account = user_load($uid)) { $response = services_fboauth_login_user($account); $response->message = "Success";
return $response; } elseif (!empty($fbuser->email) && ($account = user_load_by_mail($fbuser->email))) { fboauth_save($account->uid, $fbuser->id);
$response = services_fboauth_login_user($account); $response->message = "Success";
return $response; } // Register a new user only if allowed. elseif (variable_get('user_register', 1)) { $account = fboauth_create_user($fbuser); // Load the account fresh just to have a fully-loaded object. $account = user_load($account->uid); if ($account->status == 0) { _user_mail_notify('register_pending_approval', $account); } else { _user_mail_notify('register_no_approval_required', $account); } $response = services_fboauth_login_user($account); $response->message = "Success";
return $response; } else { return services_error('Your Facebook e-mail address does not match any existing accounts. If you have an account, you must first log in before you can connect your account to Facebook. Creation of new accounts on this site is disabled.'); }
}
}`