Is it possible to redirect to a specific Drupal page after using Facebook Connect button?

gallamine - October 28, 2009 - 02:06
Project:Drupal for Facebook
Version:6.x-2.x-dev
Component:Facebook Connect
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

I need my users to choose a unique and memorable username. I also want them to use Facebook Connect. I've disabled "Create Local Account: " but I want anyone that logs in via Facebook Connect, that doesn't have a Drupal site account, to be redirected to the registration page - so they can enter an email address and username.

Is it possible to redirect a user to the registration page after they successfully logged in via Facebook Connect?

#1

deltab - November 14, 2009 - 08:31

+1

#2

Dave Cohen - November 16, 2009 - 20:53

Two ways to accomplish this (maybe more). With javascript, or PHP. I'm more comfortable with PHP. The first thing I'd try is implementing hook_fb, something like the following (untested code)...

<?php
function custom_fb($op, $data, &$return) {
  if (
$op == FB_OP_APP_IS_AUTHORIZED) {
   
// This will be called on every page where user has authorized the app.
   
if (!$GLOBALS['user']->uid) {
     
// user has not registered.
     
if (arg(0) != 'user' || arg(1) != 'register') {
       
drupal_goto('user/register');
      }
    }
  }
}
?>

That code has not been tested. Let me know if it works for you. It's also heavy-handed. You might add some logic to redirect the user only once per session, or allow them to visit more pages than just user/register.

#3

pyrello - November 16, 2009 - 20:55

I am trying to achieve something similar to this, except that I am trying to embed the facebook connect button on the user registration page. Right now when the user clicks the connect button, they are logged in and then shown access denied because authenticated users cannot see the registration page. It would be nice to maybe use the value of drupal_get_destination() somehow...

When I look into the javascript, I see that the connect button triggers the following function:

function fb_connect_login_onclick() {
    // This will execute before the user fills out the login form.
    // More important is fb_connect_on_connected, above.
}

which references the function below in its comments but appears to do nothing than register itself as event:

function fb_connect_on_connected(fbu) {
    //alert("fb_connect_on_connected " + fbu + " settings fbu is " + Drupal.settings.fb_connect.fbu + " fb_connect_fbu is " + fb_connect_fbu);
    if (fb_connect_fbu === 0 || Drupal.settings.fb_connect.fbu != fbu) {
// We've gone from not connected to connected.
window.location.reload();
    }
    fb_connect_fbu = fbu;
}

This function is apparently where the page reload happens, but it looks like having a destination would require a Drupal.settings.fb_connect.destination or something like that to not require too much modification to the code.

For your purposes, gallamine, I think that you can either change this window.location.reload() to a window.location.href = 'user/register' (if you don't mind a hack that could be overwritten with a module update) or writing a custom module which adds a javascript file that overwrites this function (to avoid conflicts with module updates later).

I don't know if this helps or if I am just rambling, but I am also interested in the outcome of this thread, so at least I am subscribing to it.

Sean

#4

Dave Cohen - November 17, 2009 - 00:22

The javascript you're quoting is out of date. I recently made some changes (to better support benharper.com which is using this).

The function your talking about (now called FB_Connect.on_connected, but more imporantly there's an event, 'fb_connect_status' which is triggered) is called very frequenly. On every page load, maybe more, not just when the user clicks through the login box. That's why the crazy logic deciding whether to reload the page or not. It's trying to reload so that drupal is aware of who the current user is. This code will get executed after a user clicks through the login box, and it will also get called if the user logs out of facebook then logs back in and visits your site.

If you want to do this in javascript...

Try changing the XFBML of the login button markup. Have it call your javascript function instead of FB_Connect.login_onclick().
In your javascript behaviors, bind to the 'fb_connect_status' event some other function.
In your login_onclick() function set some global so you know later the user is logging in.
In your fb_connect_status handler, check whether your global is set an whether the user is logged in. If both, then redirect to the registration page.

#5

Dave Cohen - November 17, 2009 - 00:28

And yet another approach...

Take a look at fb/contrib/fb_permission.module. See fb_permission_fb(), $op == FB_APP_EVENT_POST_AUTHORIZE.

This is code that is called only after the user has authorized the app for the very first time. So if you want to redirect only once, say to the user/edit page if you've allowed DFF to create the user for you, then you can add some javascript to do the redirect. See how fb_permission_fb() calls fb_connect_init_js().

(It's complicated this way because the FB_APP_EVENT_POST_AUTHORIZE is called by facebook, not the user's browser. So the javascript has to be added to a subsequent page load.

I don't think there's any one technique that applies to all sites. But you all should let me know what you end up doing to solve this. If one solution seems really workable I'll add it to modules/fb or modules/fb/contrib.

 
 

Drupal is a registered trademark of Dries Buytaert.