Development

Updated on

How to create a Joomla 5 login with email plugin

Alejandro Schmeichler

Joomla 5 only allows users to log in using their username. However, many users prefer to log in using their email address for convenience. This tutorial will explain how to create a Joomla 5 plugin that allows users to log in using their email address instead of their username.

Creating the Plugin

Create a new plugin by creating a new file and saving it in the plugins/system directory. Name the file loginwithemail.php.

Use the following code:

<?php
 defined('_JEXEC') or die;
 
 use Joomla\CMS\Plugin\CMSPlugin;
 
class plgSystemLoginwithemail extends CMSPlugin
{
   protected $app;
   protected $db;
   
  // plugin code
}

The code above creates a new plugin called loginwithemail that extends the CMSPlugin class. Including the $app and $db class properties allow us to access the Joomla application and database.

Implement the onAfterRoute() function:

The onAfterRoute() function is called after the Joomla application has finished routing the user's request.


public function onAfterRoute()
{
    if (
        $this->app->input->getMethod() !== 'POST'
        ||
        $this->app->input->get('option') !== 'com_users'
        ||
        $this->app->input->get('task') !== 'user.login'
    ) {
        return;
    }
 
    $input = $this->app->input->getInputForRequestMethod();
 
    $email = $input->getEmail('username');
 
    if (strpos($email, '@') === false) {
        return;
    }
 
    $db = $this->db;
 
    $query = $db->getQuery(true)
        ->select($db->quoteName('username'))
        ->from($db->quoteName('#__users'))
        ->where($db->quoteName('email') . ' = :email')
        ->bind(':email', $email);
 
    $db->setQuery($query);
         
    $username = $db->loadResult();
 
    if (! $username) {
        return;
    }
         
    $input->set('username', $username);
}

This code checks if the request method is POST, the option is com_users, and the task is user.login. If these conditions are met, the function gets the user's entered email address and checks if it contains an '@' symbol. If the email address is valid, the function queries the Joomla database for the corresponding username and sets the username input field to the retrieved username. If a matching email is not found in the database, the plugin lets Joomla process the login request as it would normally do.

Creating the manifest file

Before you can install and use the plugin, you'll need to create a plugin manifest file. This file provides information about your plugin, including its name, type, version, and other important details.

To create the manifest file, you'll need to create a new file in your plugin folder and name it loginwithemail.xml. In this file, you'll need to include XML tags that specify the information about your plugin, such as:

  • <name>: The name of your plugin
  • <version>: The version number of your plugin
  • <description>: A brief description of your plugin
  • <type>: The type of your plugin (system, user, authentication, etc.)
  • <files>: A list of files that your plugin includes

For more information on creating the plugin manifest file, you can refer to the official Joomla documentation.

Conclusion

In this tutorial, we have learned how to create a Joomla 5 plugin that allows users to log in using their email address. By default, Joomla 5 only allows users to log in using their username, but with this plugin, we have extended the login functionality to allow users to log in using their email address.

The plugin works by checking if the user has entered an email address instead of a username during the login process. If the user has entered an email address, the plugin queries the Joomla database for the corresponding username and replaces the user's email address with their username, allowing them to complete the login process.

We hope this tutorial has been helpful for beginner programmers looking to create their own Joomla plugins.

You can download the plugin and view the entire code in our GitHub repository, and we encourage you to modify the plugin to suit your needs and enhance its functionality.

Save 20% on everything with code HOLIDAY through Jan 7 | Excludes renewals & upgrades