Skip to main content
2 of 3
added 2327 characters in body

Is it possible to login to Drupal with a custom user field, instead of username/pass?

I have two websites - One of them is Drupal 7, and the other is a different site (Site #2). In Site #2, within the user tables, is a field called "customid". Each user on Site #2 has a unique, randomly generated 30 character alphanumeric string within their respective "customid" field.

I can pull the value of the "customid" field for the user on Site #2, and use it as an input value in a form post to the Drupal 7 site.

In the Drupal 7 site, I created a custom user field "customid" as well.

Is it possible to make a form post from Site #2 to Drupal 7 - with the "customid" value as an input, and if it matches the "customid" field in the Drupal user table, log them in, else - redirect to the registration page?

We're essentially bypassing the username/password to log them in - I'm trying to log them in via custom user field.

I searched everywhere, and just couldn't find anything where something like this had been discussed. Thanks anyone for your time and help!

UPDATE:

I've tried implementing W.M.'s solution below, however upon attempting to enable the module, I get the white screen of death. Nothing shows up in the logs for the error.

For my custom module, I have two files:
ra_login.module
ra_login.info

Info file:

name = RA Login
description = Check ID and Login if there is a match, else go to Registration Page.
core = 7.x

; Information added by Drupal.org packaging script on 2013-12-16
version = "7.x-1.0"
core = "7.x"
project = "ra_login"
datestamp = "1387203506"

Module File:

<?php

function ra_login_menu()
{
    $items['authenticate-by'] = array( // or any other custom path you choose
        'title' => 'Login by SSO',
        'access callback' => true, // no point in authenticating at this point in code execution
        'page callback' => 'ra_login_function',
        'type' => MENU_CALLBACK,
        );
    return $items;
}

function ra_login_function($CONTACTID)
{
    // post the data to /authenticate-by as json with a parameter / property named CONTACTID holding the CONTACTID
    $postData = file_get_contents('php://input');

    $Data = json_decode($postData);

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'user')
        ->propertyCondition('uid', 1, '<>')
        // User number 1 should not be able to log in using this method
        ->fieldCondition('field_contactid', 'value', $Data->CONTACTID, '=')
        ->range(0,1);

    $result = $query->execute();

    if (isset($result['user'])) {
        $uid = array_keys($result['user'])[0];
        $account = user_load($uid);
        global $user;
        $user->uid = $uid;
        $user->name = $account->name;
        $user->timezone = $account->timezone;
        user_login_finalize(); // Log user in
        drupal_goto('/user'); // or any other path to redirect logged in users to
    } else {
        drupal_goto('/user/register');
    }

}

Again, for some reason when I try to enable the module, I get a white screen. Thanks for anyone who can help!