The way that Drupal works means that while the module is correctly using the user hooks, you loss the ability to log in when the user name is changed as the auto login step uses the $form_state name value, and not the generated value.

This is a catch 22 situation, as you need to change this early to generate the emails, et al, but this blocks the login process.

My workaround was to add another user_register submit handler to do the authentication step after the hook_user register that is called by user_register_submit. It would be easy to include this feature in the module, but using this code you get full control of the message and redirect. (A string override is fairly easy to do via settings.php for the message itself, but the redirect would be a great feature if you add this to your module.)

<?php

function HOOK_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_register':
      $form['#submit'][] = 'custom_email_registration_name_submit';
      break;
  }
}

function custom_email_registration_name_submit($form, &$form_state) {
  if (!isset($form_state['user'])) {
    return;
  }

  $admin = user_access('administer users');
  $account = $form_state['user'];

  if (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
    // No e-mail verification is required, create new user account, and login
    // user immediately.
    $auth = array(
      'pass' => $form_state['values']['pass'],
      'name' => $account->name,
    );
    if (user_authenticate($auth)) {
      // Authenticated, add a message and go to the users account
      // Since the standard workflow doesn't work, no other messages should appear.
      drupal_set_message(t('Registration successful. You are now logged in.'));
      $form_state['redirect'] = 'user/'. $account->uid;
    }
  }
}

?>
CommentFileSizeAuthor
#4 auto_login.zip1.94 KBDonaldd

Comments

8ballsteve’s picture

This would be a very useful function for me.

Alan, am unsure where to place the code snippet to ensure immediate login. Your help would be greatly appreciated - am a bit of a noob when it comes to adjusting modules.

Thanks

alan d.’s picture

Hi steve, full details can be taken from the handbook - http://drupal.org/node/231276

Basically, create a folder HOOK, a file HOOK.info and HOOK.module. The info format can be taken from any other module, name and core are about all that is really required. The dependency line makes sure that the module email_registration & all of its functions must exist before you can enable it.

file HOOK.install

name = "Example module"
description = "Gives an example of a module."
core = 6.x
dependencies[] = email_registration

And the code above in HOOK.module. Just pointing out the obvious, replace HOOK with the module name. This does not require changing the original module, it builds on top of the existing code. Place all in sites/all/modules

8ballsteve’s picture

Thanks so much for your help, works like a charm!

Donaldd’s picture

StatusFileSize
new1.94 KB

Thanks, this is exactly what I needed!
I've also added it as a module.

twooten’s picture

@Alan D. and @Donalddonckers - perfect! exactly what I have been looking for. Installed the module on Drupal 6.15 and it works great, even redirection.

alan d.’s picture

You should add the LICENSE.txt into the zip, it covers your butt if something goes wrong. I'd consider releasing this if the maintainers are not interested!

crifi’s picture

Component: Documentation » Code
Category: feature » bug
Status: Active » Needs work

Thanks for adding the issue and a workaround! It took hours for me to find the problem in this module.
I think this bug should be patcht inside the email_registration module, because it breaks a drupal core feature (autologin after user register). What do you think?

Christopher Herberte’s picture

Version: 6.x-1.2 » 6.x-1.x-dev

Agreed, this should be included in email_registration.module as it effects normal workflow. moving to dev will add a fix to next release.

Christopher Herberte’s picture

Status: Needs work » Closed (fixed)

Committed to dev

rjbrown99’s picture

Oops.. this is closed. Opening a new ticket.