1) What is the best way to set the landing page that the user lands on after logging in via Facebook?

I currently see it set to "/#_=_".

2) Plus, how can we set different landing pages for registration and login? LoginToboggan supports this. Setting different landing pages for registration and login allows website owners to improve the user experience as well as track registrations via Google Analytics Goals.

Comments

quicksketch’s picture

Title: Set Landing Page » Set Custom Landing Page URL for Login and Register
Component: Documentation » Code
Category: support » feature

Actually right now you can't set a landing page at all. The code is partially set up to allow redirects though. We have this code already in place:

    $destination = fboauth_action_invoke($action_name, $app_id, $access_token);
    if (empty($destination)) {
      $destination = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : '<front>';
    }
    drupal_goto($destination);

But right now our fboauth_action_connect() function is not actually returning any value, so there isn't any way to specify a redirect URL (and certainly not different redirect URLs based on login or registration).

I currently see it set to "/#_=_".

As an aside, Facebook OAuth module isn't actually setting this value, it's currently being added somehow due to a bug in the Facebook API, there are multiple reports of this around the web:
http://stackoverflow.com/questions/7131909/facebook-callback-appends-to-...
https://developers.facebook.com/blog/post/552/ (lots of posts in the comments)
http://bugs.developers.facebook.net/show_bug.cgi?id=20504

I can confirm that when I first wrote the module, it would just redirect you to the homepage without any extra symbols attached like that. A change in the API affected the module somehow.

In any case, I'm moving this issue to a feature request, as I think options for specifying login and registration paths would be a good option to include.

adamharms’s picture

+1

CydeWeys’s picture

I'm currently making some changes to ensure that the user is redirected to whatever page they came from. So if the user is viewing, for instance, site/node/1450, and they click a "Login with Facebook" button on there, they'll end up back at site/node/1450, instead of the main page.

CydeWeys’s picture

Here's a simple way to create an FB Connect button that takes you back to the page you were last browsing after the login action completes. I've implemented this as a footer for simplicity, but it could easily work as a block, as part of a page template, etc. (Left as an exercise for the reader.)

function yourmodule_footer() {
  $app_id = isset($app_id) ? $app_id : variable_get('fboauth_id', '');
  if ($app_id && !fboauth_fbid_load()) {
    // Get rid of the leading slash on the destination.
    $redirect = substr(drupal_get_path_alias(request_uri()), 1);
    $return = fboauth_action_display('connect', $redirect);
  }
  else {
    $return = '[TESTING: You are already logged in, so there is no link here.]';
  }
  return $return;
}
fenda’s picture

I'm currently making some changes to ensure that the user is redirected to whatever page they came from. So if the user is viewing, for instance, site/node/1450, and they click a "Login with Facebook" button on there, they'll end up back at site/node/1450, instead of the main page.

Was this implemented?

d.holmen@gmail.com’s picture

Thank you!

I added this to my custom module, in my form_alter and added it as a markup:

	// Facebook login button
	switch ($form_id) {
		case 'user_register_form':
		case 'user_login':
		$form['facebook_redirect'] = array (
		'#markup' => yourmodule_fboauth(),
		);
	}
jonmcl’s picture

Incase it helps anyone, I managed to make the connect button come back to the originating page (instead of front page) by creating an overriding theme function:


/**
 * Return a link to initiate a Facebook Connect login or association.
 * Modified theme function to replace theme_fboauth_action__connect().
 *
 * @param $link
 *   An array of properties to be used to generate a login link. Note that all
 *   provided properties are required for the Facebook login to succeed and
 *   must not be changed. If $link is FALSE, Facebook OAuth is not yet
 *   configured.
 * @see fboauth_link_properties()
 */
function theme_fboauth_action__connect($variables) {
  $action = $variables['action'];
  $link = $variables['properties'];

  // modify to return to current path
  $currentDestination = drupal_get_destination();
  $link['query']['redirect_uri'] .= '?destination=' . drupal_encode_path($currentDestination['destination']);

  $url = url($link['href'], array('query' => $link['query']));
  $link['attributes']['class'] = isset($link['attributes']['class']) ? $link['attributes']['class'] : 'facebook-action-connect';
  $attributes = isset($link['attributes']) ? drupal_attributes($link['attributes']) : '';
  $title = isset($link['title']) ? check_plain($link['title']) : '';
  $src = ($GLOBALS['is_https'] ? 'https' : 'http') . '://www.facebook.com/images/fbconnect/login-buttons/connect_light_medium_short.gif';
  return '<a ' . $attributes . ' href="' . $url . '"><img src="' . $src . '" alt="' . $title . '" /></a>';
}

It took me a few minutes to realize the ?destination= parameter needed to go into the redirect_uri parameter and not get added to the end of the original Facebook URL.

I should mention that this is for Drupal 7, but I think for Drupal 6 you just need to replace drupal_encode_path with drupal_urlencode.

You will need to rename function to be placed in your theme's template.php or use hook_theme_registry_alter() to place it in a custom module.

merzikain’s picture

#4 solved my problem. In my specific case I used most of that code in a template function instead of a module.

jphelan’s picture

#4 solved my problem as well. Thanks CydeWeys

milesw’s picture

Nice tip, #4, thanks. Just note that sometimes FBOauth will include ?destination it's part of the page URL, so it could be there already.

You can also do this in a preprocess...

/**
 * Implements hook_preprocess_HOOK().
 */
function modulename_preprocess_fboauth_action__connect(&$vars) {
  if (!substr_count($vars['properties']['query']['redirect_uri'], '?destination')) {
    $current = drupal_get_destination();
    $vars['properties']['query']['redirect_uri'] .= '?destination=' . drupal_encode_path($current['destination']);
  }
}
hachreak’s picture

Hi guys,
I have patched the module to add in the configuration of the block a url where drupal can redirect the user after login.

I added this configuration:
[-] textfield: url where redirect user
[-] checkbox: if I want redirect user in the same page where he made clicks on fbconnect button

It's possible to insert the patch in the module? :)
Someone can review the code if all it's ok?

Best,
hachreak

dmegatool’s picture

#7 works like a charm ! I just implemented this on my D7 install. Can't thank you enough JonMcL :D

Frederic wbase’s picture

@JonMcL

Thank you for you code, just tested it on a D7 install and it works wunderfull!

grts

frederic

rosborn’s picture

Can I ask for the status of this fix? I tried modifying the code of my Drupal 7.x-1.5 version of fboauth.module using the JonMcL version, and it seemed to work well on some pages for a while, but it has now reverted to landing on a random page. I haven't managed to find any systematics to this behavior, so I can't add much to this issue. Also, it's not entirely clear what is being patched, since this is labelled a 6.x issue although it affects Drupal 7 equally.

I think this feature is essential to the module. If it's been fixed, it would be great if it were included in a stable release soon. Is that possible?

timofey’s picture

This would be a great commit!

klucid’s picture

@hachreak, thanks for the patch. Any chance you could write one up for Drupal 7? Thanks in advance!

hachreak’s picture

when I start to use drupal 7.. XD

But I don't understand... the module is still under development? :)

cayerdis’s picture

#7 works great, thank JonMcL!

quicksketch’s picture

But I don't understand... the module is still under development? :)

Sorry I maintain a lot of modules and maintain them in sort of a haphazard manner. Once a module gets enough issues (especially with patches) then I'll go through and clear out the queue all at once. It's tough to find time to watch all of them at once.

Regarding this issue, I think we could take a cue from core's login behavior and always redirect to the same page the user was on when using the user login block. It's completely dumb that you'd be taken to any other page at all when logging in, unless its the first time they've logged in at all. Then you might want to take them to some welcome page or form to complete their registration (though you should probably use http://drupal.org/project/complete_profile for that situation).

So the options that remain are where you would like the user directed when they log in from /user/login (not using the login block). And potentially an option similar to logintoboggan to redirect the user *upon registration*, that will override the normal redirect location.

I don't think these options should live on the block configuration form, instead these should be set on the FBOAuth settings page, since the first option would apply to the normal login form only.

quicksketch’s picture

To make this feature request less of an issue, I filed a patch at #1873750: Login/Connect/Deauthorize links all need $redirect variables set, which solves the immediate problem of the login block redirecting back to the same page, rather than going to the homepage.

charlesjc’s picture

Facebook somewhat document the behaviour of adding #_=_ to the uri at https://developers.facebook.com/blog/post/552/

Change in Session Redirect Behavior
This week, we started adding a fragment #_=_ to the redirect_uri when this field is left blank. Please ensure that your app can handle this behavior.

However, while there are several complaints that the documentation is wrong or inadequate, there has not been a response from FB that I can find.

charlesjc’s picture

Issue summary: View changes

Edited for clarity only.