4
votes

I am trying to develop a custom module for my d6 site that lets users register and login to my site just as a regular user would using the existing drupal login and registration system.

I looked at fbconnect module (which has not been updated to use the latest facebook php sdk [ver 3]), and drupal for facebook which maps a facebook login to a existing userid if a drupal user with the same email exists -- which is not what I am looking for.

What I am looking to do:

  • User comes to my site. Clicks on login with facebook.
  • He is shown the fb login popup. User grants permissions.
  • At this point I register him to the site, and log him in.
  • He now has access to all the permissions set for an authenticated user to create posts etc.
  • He logs out. Comes back again and logs in, and this time all the content he'd created the first time is available to him just as it would a regular authenticated user.

I am having trouble visualizing the module structure. How would I achieve this? I know I need to use server side authentication as mentioned here.

I am well versed with creating Drupal modules, just having trouble thinking of the architecture of this. I would greatly appreciate a broad picture if anyone has some experience with this. I would be on top of the world, if someone has detailed steps to offer.

1
Did you try the Facebook OAuth module? - Pierre Buyle
I would suggest you to make your question more scoped; as it is, it is a too generic question about how to write Drupal modules, as you are asking about the module structure. - apaderno
@PierreBuyle Thanks for the great pointer. The module you mention looks very promising. I am going to try it and report back. - Capstone

1 Answers

0
votes

I'm registering users automatically with their Facebook ID, without pass so they only can login if facebook approves them and recognize them as Facebook users.

I'm using the PHP SDK for Facebook. Also remember that you need a signed request and the user you are trying to register have to accept your APP before trying registration (to get his/her user ID).

You can obtain a signed request with the registration plugin OR in a tab of a Facebook page...

Here's my code, it's not fully tested but it works, and probably will help you find a way.

// Registro automático en Drupal con datos de Facebook
function fbconex_registro() {

global $facebook;
global $user;

$SR = $facebook->getSignedRequest();

if ($SR['registration']) {

    $datos = $SR['registration'];
    $usuario_nuevo['status'] = 1;
    $usuario_nuevo['mail'] = $datos['email'];
    $usuario_nuevo['name'] = 'fb-' . $facebook->getUser();

    // Comprobar que el usuario no existe
    $usuario_nuevo = user_save(null, $usuario_nuevo);

    if (!$usuario_nuevo) {
        $html .= 'Se detectaron problemas. No se pudo registrar.';
    };
} else {
    $html .=
            'Para iniciar sesión o registrarse visite 
  <a href="/concursocial/sesion">mi sesión</a><br/>';

    if ($user->uid != 0)
        drupal_goto('user');
}

if ($usuario_nuevo) {
    $user = $usuario_nuevo;
    drupal_goto('user');
}

return $html;
}