• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

joomla login script for fabrik custom login

Hello,

I have a stand-alone form form with 2 elements (username/password). Form will not save to database and therefore no db table is set. The Form is an ajax form.

Upon submit, I would like to validate and login joomla user. There is an external script, which looks good, but does not work: https://gist.github.com/AdamMadrzejewski/020c4fa4b1d0e7af78b8

PHP:
define('_JEXEC', 1);

  if (file_exists(__DIR__ . '/defines.php'))
  {
      include_once __DIR__ . '/defines.php';
  }

  if (!defined('_JDEFINES'))
  {
      define('JPATH_BASE', __DIR__);
      require_once JPATH_BASE . '/includes/defines.php';
  }

  require_once JPATH_BASE . '/includes/framework.php';

  // Instantiate the application.
  $app = JFactory::getApplication('site');

  // JFactory
  require_once (JPATH_BASE .'/libraries/joomla/factory.php');


$credentials['username'] = $formModel->_formData['___username'];
$credentials['password'] = $formModel->_formData['___password'];

  /**
   * Code adapted from plugins/authentication/joomla/joomla.php
   *
   * @package     Joomla.Plugin
   * @subpackage  Authentication.joomla
   *
   * @copyright   Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   */

  // Get a database object
  $db    = JFactory::getDbo();
  $query = $db->getQuery(true)
      ->select('id, password')
      ->from('#__users')
      ->where('username=' . $db->quote($credentials['username']));

  $db->setQuery($query);
  $result = $db->loadObject();

  if ($result)
  {
      $match = JUserHelper::verifyPassword($credentials['password'], $result->password, $result->id);

      if ($match === true)
      {
          // Bring this in line with the rest of the system
          $user = JUser::getInstance($result->id);
          echo 'Joomla! Authentication was successful!';
      }
      else
      {
          // Invalid password
          // Prmitive error handling
          die('Invalid password');
      }
  } else {
      // Invalid user
      // Prmitive error handling
      die('Cound not find user in the database');
  }

Placed the code OnAfterProcess with my form. When I submit, I get:

HTML:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I used to have a working script, but I have lost it.
Any help appreciated.

I could use the Joomla login script, but this will redirect to a component page when login failed. I would like to be more flexible with a custom fabrik form and define my own redirects and login behaviour.
 
Good point. Already read and thought about it.

The reason why I am not using JUser is simple: how do I set it up?

I have a register form with JUser and it is working. Not sure how to use with a login validation.
 
Thank you Troester.

I have seen the docs. I will try "Auto Login - if set to yes we will try to log in the user as soon as the form is submitted.".

I wasn't really sure, if JUser will actually act as a password/email validation only with existing users in db.
 
I have tried to use email(field)/password(field) and username(field)/password(field) with JUser and Auto-Login. Actually I want to login an existing user.

Ajax Form that does not save to database and does bypass all joomla stuff.

When I try to login, email/username field are flagged as error.

Not sure yet, what I am doing wrong here.
 
Last edited:
I haven't used much juser plugin, but this option seems only for logging user in after initial registering.

About your code, at least remove the following row as this file does not exist in newer versions of Joomla:
// JFactory
require_once (JPATH_BASE .'/libraries/joomla/factory.php');
 
but this option seems only for logging user in after initial registering
Yup (+ I think JUsers plugin doesn't make much sense without database)

For your code: I would look how Joomla is doing it in components\com_users\controllers\user.php
 
PHP OnBeforeProcess

Module / No Ajax / not saving to database / 2 fields: username/password

PHP:
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

jimport('joomla.plugin.helper');
$app = JFactory::getApplication('site');
$JInput = $app->input;

$username = '{___username}';
$password = '{___password}';

$credentials = array();
$credentials['username'] = $username;
$credentials['password'] = $password;

$db = JFactory::getDbo();
$query = $db->getQuery(true)
  ->select('id, password')
   ->from('#__users')
   ->where('username=' . $db->quote($credentials['username']). ' OR email = '.$db->quote($credentials['username']));
$db->setQuery($query);
$result = $db->loadObject();

if ($result) {

   if (JUserHelper::verifyPassword($credentials['password'], $result->password, $result->id)) {
       //GET USER INSTANCE FROM DATABASE ID
       $user = JUser::getInstance($result->id);
       //LOGIN THE USER
       $app->login($credentials, $options = array());
    JFactory::getApplication()->enqueueMessage(JText::_('FABRIKL_SYSTEM_MESSAGE_LOGIN_SUCCESS'));
   }
   else
   {
       //HANDLE INCORRECT PASSWORD
    JFactory::getApplication()->enqueueMessage(JText::_('FABRIKL_SYSTEM_MESSAGE_LOGIN_ERROR'), 'error');
   }
}
else
{
   //HANDLE INVALID USERNAME
  JFactory::getApplication()->enqueueMessage(JText::_('FABRIKL_SYSTEM_MESSAGE_LOGIN_ERROR'), 'error');
}
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top