1

How can you edit the Services' json response of successful register.json to include the user's OR system's auto-generated password, plus any token?

I've tried a variety of combinations of the commented code below..

function logintoboggan_process_login($account, &$edit, $redirect = array()){
  global $user;

  $user = user_load($account->uid);
  watchdog('login debug', json_encode($edit)); // AT THIS POINT I CAN VERIFY WE HAVE THE PLAIN-TEXT PASSWORD
  //user_login_submit(array(), array('uid' => $account->uid));
  user_login_finalize($edit);  
//  $user = user_load($account->uid);
//  $user->token = drupal_get_token('services'); //WE HAVE A TOKEN ALTHOUGH I DOUBT THIS WOULD WORK.
//  user_login_finalize($edit);
//  module_invoke_all('hook_user_login');
//  module_invoke_all('tripchi_user_login');
//  module_invoke_all('logintoboggan_user_login');

and even also tried hacking the drupal core on line 3888 of "/modules/user/user.module":

elseif (!$admin && !variable_get('user_email_verification', TRUE) && $account->status) {
      global $user;    
      $user = user_load($account->uid);
      watchdog('login debug', json_encode($edit));  // AT THIS POINT I CAN VERIFY WE HAVE THE PLAIN-TEXT PASSWORD AND TOKEN AVAILBABLE!
      //user_login_submit(array(), array('uid' => $account->uid));
      user_login_finalize($edit);  
    //  $user = user_load($account->uid);
    //  $user->token = drupal_get_token('services'); //WE HAVE A TOKEN ALTHOUGH I DOUBT THIS WOULD WORK IN TERMS OF SESSION PERSISTANCE
    //  user_login_finalize($edit);
    _user_mail_notify('register_no_approval_required', $account);
    $form_state['uid'] = $account->uid;
    user_login_submit(array(), $form_state);
    drupal_set_message(t('Registration successful. You are now logged in.'));
    $form_state['redirect'] = '';
  }

if one of my test lines don't break the call altogether, on success the call just replies:

{
    "uid": "499",
    "uri": "http://localdrupal.tripchi.com/drupalgap/user/499"
}
  • 1
    Does the register resource usually return the full account object? If so you can use hook_services_account_object_alter() to decorate it before return – Clive Dec 17 '14 at 16:14
  • @Clive, using the basic "notesource" module given in a popular Services tutorial, I added: function noteresource_services_account_object_alter(&$edit, $account, $category) { die(__FUNCTION__); $edit->token = drupal_get_token('services'); $edit->password = 'temppasswordwhenotprovided'; $account->token = drupal_get_token('services'); $account->password = 'temppasswordwhenotprovided'; } but get nothing and it doesn't seem to execute at all. – E.A.T Dec 18 '14 at 8:32
  • @E.A.T are you able to alter register response? – DRUPWAY Jan 13 '16 at 13:22
  • @Smalution, NO. not within logintoboggan. I ended up adding a hook_services_resource endpoint that overwrites the response altogether. – E.A.T Jan 15 '16 at 8:00
  • @E.A.T thanks for reply. i mean you have created custom services for registration right? if yes and you have some docs or reference please share it. – DRUPWAY Jan 15 '16 at 8:17
2

I was never able to get back the raw text password without hacking on the logintoboggan / user modules themselves (similar to what is shown in my question).

However, I solved my same purpose of providing immediate-login-upon-registration for Services/API calls using the services_token_access module. This code only hooks onto a success registration then ads the services_token to the response which is now the only thing my client now needs for validation.

function HOOK_services_request_postprocess_alter($controller, $args, &$result) {

if ( gettype($result) == 'array' && isset($result['uri']) && isset($result['uid'])) { // THIS CONDITION MATCHES THE DEFAULT RESPONSE OF A SUCCESSFUL REGISTRATION. 
        $user = (object)$result;
        $user = user_load($user->uid);
        $user = HOOK_services_account_object_alter($user);
        $result = array('token'=>$user->services_token, 'user'=>$user);
    }
    return $result;
}


function HOOK_services_account_object_alter(&$user) {
    $user->services_token = services_token_access_update_token($user->uid, FALSE); // essential for logging in later
    $user->specialstuff = 'whatever else you need';
    return $user;
}

(The services_token_access module is not particularly popular and barely supported, but worked for me.)

  • Definitely i will implement it and come back to you. Thank you very much. – DRUPWAY Jan 15 '16 at 11:20
  • you are great. It solved my requirement related to registration API. Thank you so much. – DRUPWAY Jan 25 '16 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.