Smart Login/Logout Links

Need to add a login/logout link to your site, but don’t like the Drupal login block?  If you’re using Drupal 7 at all, this page is for you! A simple link is often much more appealing and discrete, and doing it right will ensure that it says “Log in” for unauthenticated users and “Log out” for users who have signed in.

 

We’ll also show how to use tools that make “smart” links, that do their best to return you to the page you were on when you clicked the login/logout button.

 

Basic login/logout links

 

Used to be you had to throw PHP code into the template or a block in order to make a context-aware login/logout link.  This technique is based on adding the links to a Drupal menu.  One of the best places would be as part of a Footer menu, but other locations can work depending on your requirements and design.

 

Simply choose the menu you’d like and add two adjacent links to the menu (Admin > Structure > Menus).  One labeled “Log in” (or whatever you prefer) with the drupal path of “user/login” the other labeled “Log out” (or whatever you prefer) with the drupal path of “user/logout”.

 

 

 

Because we’re using Drupal’s menu system, it does an access check and can only show user/login or user/logout depending on the login state.  Placing the links adjacent to each other ensures they replace each other in position.

 

Smart login/logout links

 

Smart links automatically append “?destination=current/drupal/path” to your links in an attempt to return the user to the same page they’re currently on. 

 

If you are using the UCSF MyAccess Settings Feature for login, this functionality is already activated.  If you are not using MyAccess, you can enable the Better login and logout handling module (better_login).  If you want to use this on another site you could grab it from the UCSF git repository (docroot/sites/all/modules/custom/better_login) [todo:provide download].

 

Once activated, you simply append “?destination=” to your login/logout menu links from before. “Log in” becomes user/login?destination= and “Log out” becomes user/logout?desination= -- the module will do the rest and fill in the current page paths throughout your site! 

 

 

Note: “Destination” behavior is somewhat erratic for logout, you may find it is better to leave off destination and just use a standard user/logout link.

 

Note: automatic “destinations” may not take effect immediately when logged out due to varnish cache. Try checking if logout links work, try infrequently visited pages, flush internal cache, or just wait.