1

I'm developing a facebook login (FB SDK Javascript) for a Joomla site to add an alternative way to register/login.

What I've done is:

  • Create FB app
  • Insert relevant code to my site
  • Get connected stat from FB.login().
  • Got basic information: name, email...etc

I found the following function that I use to login to Joomla, but this way require me to provide username & password.

$result = JFactory::getApplication()->login(array('username' => $username, 'password' => $password), array('remember' => true));

My question is:
After I get the permission from FB, I wish to login to Joomla without enter email and password, so which module/helper/function should I use ?

0

If user's action is recorded somewhere, for example, buy a product, post a comment,... then user's ID or user's username must be available.

In this case you need to create a Joomla account for your user.

Step 1. User logins with Facebook successfully

Step 2.a. If he has already had an account (by checking email, etc...), you get the account's username login (no need for password).

Step 2.b. If he doesn't have an account, you create a dummy account for him. A better way is asking him for providing a username (and password), let him know that he only does this once, this username can be used to log into the site if for some reasons he can't use Facebook login. Use his username to log him in.

Here is the sample code I use for this task in my community component.

        /*
         * Get user info by email address.
         * If user is already registered, we log user in. If we haven't saved
         * this connection yet, we save it from now.
         * If user is not registered yet, we redirect user to register function.
         */
        $db = JFactory::getDbo();
        $query = $db->getQuery(true)
            ->select($db->quoteName(array('id', 'username')))
            ->from($db->quoteName('#__users'))
            ->where($db->quoteName('email') . ' = ' . $db->quote($email));
        $user = $db->setQuery($query)->loadObject();

        if (isset($user->id))
        {
            // Log user in.
            $options = array('action' => 'core.login.site');

            $response = array('username' => $user->username);

            JPluginHelper::importPlugin('user');
            $result = $app->triggerEvent('onUserLogin', array($response, $options));

            $app->redirect(JRoute::_('index.php?Itemid=' . $itemId, false));

            return true;
        }
        else
        {
            $app->redirect(JRoute::_('index.php?option=com_cmcommunity&task=registration.socialRegister&network=' . $this->pluginId, false));

            return false;
        }
0

You have to create an authentication plugin to receive the alternative credentials, and authorize the access.

You can create it based on one of the core authentication plugins. For instance, PlgAuthenticationCookie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.