Everytime I open app, I need to login again. Is there any way to auto login without entering user name and password again as drupal websites?
Thanks

Comments

tyler.frankenstein’s picture

Status: Active » Postponed

I believe OAuth is the solution for this. I haven't tried it yet though, and won't have time until someone steps forth to contribute/sponsor its DrupalGap implementation.

coolman7’s picture

I have no knowledge about OAuth, so I made a workaround until better and more secure way is implemented. This works for me. But I don't think it is secure to store password in localstorage, but I can't find any other way. If someone knows more secure way, please share here, so I can change the code.

I added some code to user.js:

// added by coolman7
function user_auto_login() {
  try {
    if (Drupal.user.uid != 0)
      user_login(localStorage.account_name, localStorage.account_pass, {
        success: function(result) {
          drupalgap_goto(drupalgap.settings.front);
        }
      });
  }
  catch (error) { console.log('user_auto_login - ' + error); }
}

function user_login_form_submit(form, form_state) {
  try {
    user_login(form_state.values.name, form_state.values.pass, {
      success: function(result) {
        localStorage.account_name = form_state.values.name; // added by coolman7
        localStorage.account_pass = form_state.values.pass; // added by coolman7
        drupalgap_goto(drupalgap.settings.front);
      }
    });
  }
  catch (error) { console.log('user_login_form_submit - ' + error); }
}

function user_logout_callback() {
  localStorage.account_name = ''; // added by coolman7
  localStorage.account_pass = ''; // added by coolman7
  return '<p>Logging out...</p>';
}

And one line to drupalgap.js:

function drupalgap_bootstrap() {
  try {
    // Load up any contrib and/or custom modules (the DG core moodules have
    // already been loaded at this point), load the theme and all blocks. Then
    // build the menu router, load the menus, and build the theme registry.
    drupalgap_load_modules();
    drupalgap_load_theme();
    drupalgap_load_blocks();
    menu_router_build();
    drupalgap_menus_load();
    drupalgap_theme_registry_build();

user_auto_login(); // added by coolman7

    // Attach device back button handler (Android).
    document.addEventListener('backbutton', drupalgap_back, false);
  }
  catch (error) { console.log('drupalgap_bootstrap - ' + error); }
}
tyler.frankenstein’s picture

I wouldn't recommend hacking DrupalGap core to do this, instead this could be accomplished with custom submit handlers and hook_services_*, and probably hook_deviceready() too. Even then, I'm going to go ahead and say that storing someone's password plain text in local storage isn't a good idea, aka security concern.