Forgive the potentially simple question, but I'm nearing the end of a project deathmarch and may not running on all cylinders.

Problem: I want to redirect a user after they login for the first time to a special welcome page. All subsequent logins should go to a different user home page.

I've been using LoginTobaggin to control registration redirects, but they all only seem to work if you use the one-time email link. We've opted not to use this due to security requirements that are out of my control and simply want the user to manually login using their username and system generated password that they receive in email. When they do this for the first time, they should go to the welcome page and be prompted to change their password or some such.

I've tried to use LoginDestination to try to determine whether or not the user has logged in before, but can't find a reliable piece of data to query on. "access" in the user table seems to get set after the login but before LoginDestination kicks in. Using the accesslog or watchdog is not viable since those tables will be purged when the system goes live on a regular basis as part of an archive regimen.

I'm hoping there's something simple that I'm over-looking as this seems like a common feature.

Once again, apologizes for the potentially simple question.

Comments

fubarthepanda’s picture

Not sure this is the best way to skin this problem, but I have been to get around it with minimum effort by:

a. Adding a hidden field to Profile for "verification".

b. Adding a PHP snippet to LoginDestination which checks for the presence of "verification" and, if not found, adds the field and redirects the user to the first time login page. If it is found, then redirects the user to the regular home page.

takinola’s picture

The solution to your problem requires the use of the Workflow-NG module. Just go through the following steps and you'll have a one time redirection set up

1. Create a role 'First Timers' for your first time users. Users should not have the ability to add themselves to this role.
2. Using Workflow-NG, set up a rule initiated by the event "User Registered" that assigns the user to the role "First Timers"
3. Set up another rule, initiated by the event "User Log In" that redirects the user to the required destination AND removes the user from the role "First Timers"

That's it! All users get set to be First Timers when they register but after login they are redirected and stripped of their First Timer status so they follow normal log in procedures later on.

nrackleff’s picture

I need to do this same thing, but I am using Drupal 6 and it appears the workflow ng module is only for Drupal 5. Is there another way to accomplish this in Drupal 6?

davidseth’s picture

Use the Rules module - http://drupal.org/project/rules. It is the successor to Workflow-ng. You can use the exact same steps as Workflow-ng though...

Michsk’s picture

your post made my day

electblake’s picture

Just so any passerby is having problems (as I was to my dismay) is that even if your running a new/small drupal build and havent installed http://drupal.org/project/views_bulk_operations then many actions (user role ones for instance) won't exist yet.

Also, if you haven't installed http://drupal.org/project/views_bulk_operations then you will have a really hard time finding a module that DOES open up user role actions..

just saying ... http://drupal.org/project/views_bulk_operations

moxwai’s picture

I can get the trigger to work on to add users to a role on login or registration, but i can redirect to edit password screen or any other page... i think i have content profile module enabled.. could that be breaking it? Am i supposed to check 'load user account' as the action?!

Michsk’s picture

you have to refer them to a custom url and from there it's easy just create the profile/edit url

boran’s picture

Rules can be used in D6 as noted by takinola, using an initial role as aflage to track new users
However, I've found that this interferes with the normal procedure of setting a new password for first time users.
i.e. a) have added rules + page direct to a "first login" page,

1. User registers and is send a one-time login via email.
2. User visits link and sees a page like:
Reset password
This is a one-time login for XXX and will expire on 16. July 2011 - 9:40.
Click on this button to login to the site and change your password.
This login can be used only once
3. After clicking on Login, the user is redirected to /user/XXX/edit, where he/she can set a password.
4. User is logged in an operational.

When the rule is active, step 4 sis skipped, i.e. the user is not presented with a change to set his password.

Some related posts: #1062646: First time login rule with verification email.
I'll follow up on #480584: Rule to redirect on login, except on one-time login and password reset, seems more appropriate for fixing the above.

Brackish Lake’s picture

If you're developing a module - Could you not see if $account->created == $account->access on hook_user $op = 'login'?

UPDATE: The answer is no. They're not set equal on first time log in.

tarik916’s picture

I've created video tutorials for two ways of achieving this. One is based on a user profile field value, and the other is based on the number of logins or posts.

http://www.slurpitup.com/drupal/redirect-users-on-first-login-using-rules-and-user-stats

http://www.slurpitup.com/drupal/redirect-user-using-rules-based-on-a-profile-field-value

Ivanhoe123’s picture

Do not go to the links above. Avast reported a Trojan horse on those pages.

afox’s picture

For future reference, I created a module to handle future logins with the context-module. http://drupal.org/project/first_login creates a context condition plugin and views handlers as well as couple useful set/get functions for custom handling.

For redirections within context, try using it with http://drupal.org/project/context_redirect.
Otherwise, I'm considering triggers for use with rules. Currently D6 is available, D7 version is coming soon.

Anonymous’s picture

This is my solution:

if ($form->access == $form->login) {
    // ... do your stuff here ...
}
alesr’s picture

Here is a snippet for Drupal 7.
Executed only on user's first time login using hook_user_login().

<?php
/**
 * Implements hook_user_login().
 */
function MODULENAME_user_login(&$edit, $account) {
  // If this is user's first time log-in.
  if ($account->access == 0) {
    // Execute your code.
  }
}
?>
khaled_webdev’s picture

Hi,
I have used the same solution.

densolis’s picture

All,

This is actually very simple in D7 using the Rules module. All you have to do is see if the value of the user's Last Access field is empty. When an account is created, the initial value of the Last Access is empty.

The value of this field is not set until sometime AFTER the rules have been run. So, all you have to do is setup a conditions in rules that check:

  • Data value is empty
  • site:current-user:last-access

Here is an export of my rule and it has been working fine. If anyone finds any issues, please let me know. You should be able to import this rule into your site.

{ "rules_first_time_login" : {
"LABEL" : "first time login",
"PLUGIN" : "reaction rule",
"WEIGHT" : "1",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "user_login" : [] },
"IF" : [
{ "data_is_empty" : { "data" : [ "site:current-user:last-access" ] } },
{ "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "16" : "16" } } } }
],
"DO" : [
{ "drupal_message" : { "message" : "Welcome to Drupal Camp Florida. Please submit your session proposal below." } },
{ "redirect" : { "url" : "node\/add\/sess?destination=\/admin\/dashboard" } }
]
}
}

The role with a value of "16" is a role of "Speaker".

Dennis