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;
}
}
}
?>
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | auto_login.zip | 1.94 KB | Donaldd |
Comments
Comment #1
8ballsteve commentedThis 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
Comment #2
alan d. commentedHi 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
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
Comment #3
8ballsteve commentedThanks so much for your help, works like a charm!
Comment #4
Donaldd commentedThanks, this is exactly what I needed!
I've also added it as a module.
Comment #5
twooten commented@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.
Comment #6
alan d. commentedYou 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!
Comment #7
crifi commentedThanks 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?
Comment #8
Christopher Herberte commentedAgreed, this should be included in email_registration.module as it effects normal workflow. moving to dev will add a fix to next release.
Comment #9
Christopher Herberte commentedCommitted to dev
Comment #10
rjbrown99 commentedOops.. this is closed. Opening a new ticket.