Background
I'm attaching the user register form, to the node create form with hook_form_alter
. that way I create a node and user at once.
In order to validate and register the user, in hook_form_alter
I add the relevant functions to my node form submit and validation calls.
function mymodule_form_alter form_alter(&$form, &$form_state, $form_id) {
....
$form['#validate'][] = 'user_account_form_validate';
$form['#submit'][] = 'user_register_submit';
Problem
The node is created and the user is created but the user isn't logged in.
- Why isn't the user logged in?
The user register and login code seems to be running ok (see below), I can only think somehow the node creation is logging the user out? I have no idea how to continue debugging this.
Debugging
Thanks to Felix, I was able to debug by logging dpm (because it doesn't appear on the page) and the code is running correctly.
user_login_submit
is called from user_register_submit
function user_login_submit($form, &$form_state) {
global $user;
$user = user_load($form_state['uid']);
$form_state['redirect'] = 'user/' . $user->uid;
user_login_finalize($form_state);
}
And then user_login_finalize
is called.
function user_login_finalize(&$edit = array()) {
global $user;
watchdog('user', 'Session opened for %name.', array('%name' => $user->name));
// Update the user table timestamp noting user has logged in.
// This is also used to invalidate one-time login links.
$user->login = REQUEST_TIME;
db_update('users')
->fields(array('login' => $user->login))
->condition('uid', $user->uid)
->execute();
// Regenerate the session ID to prevent against session fixation attacks.
// This is called before hook_user in case one of those functions fails
// or incorrectly does a redirect which would leave the old session in place.
drupal_session_regenerate();
user_module_invoke('login', $edit, $user);
}
I printed out $edit
and $user
and $edit
is $form_state
and $user
is the newly created user. I can't see anything going wrong. The session opening is logged by watchdog, however perhaps it's never assigned?
How else could I continue to debug this? Could drupal_session_regenerate()
be logging me out?
EDIT: I switched the user_register_submit
to logintoboggan_user_register_submit
, which worked on my dev environment but then broke on production with the same problem as above.
It doesn't appear to be a code problem, but something to do with the database or a setting. I dropped my production front end in front of my dev backend and it works fine, so it's some sort of database setting (which could of course effect the front end code).