5

I'm trying to do a very basic login function through my component, but I'm always getting back a TRUE response from the auth, whether I use valid credentials or not. The docs seem to indicate that it would only return true on success. Here's the controller task I'm using, along with the docs for the login method.

/**
 * Login authentication function.
 *
 * Username and encoded password are passed the onUserLogin event which
 * is responsible for the user validation. A successful validation updates
 * the current session record with the user's details.
 *
 * Username and encoded password are sent as credentials (along with other
 * possibilities) to each observer (authentication plugin) for user
 * validation.  Successful validation will update the current session with
 * the user details.
 *
 * @param   array  $credentials  Array('username' => string, 'password' => string)
 * @param   array  $options      Array('remember' => boolean)
 *
 * @return  boolean  True on success.
 *
 * @since   3.2
 */
public function cartlogin() {
    $app = JFactory::getApplication();
    $username = $app->input->get('username', '', 'STRING');
    $password = $app->input->get('password', '', 'STRING');

    $result = $app->login(array('username' => $username, 'password' => $password));        
    if ($result == TRUE) {
        $app->enqueueMessage(JText::_('COM_CUSTOM_LOGIN_SUCCESS'), 'message');
        $this->setRedirect(JRoute::_('index.php?option=com_custom&view=cart', false));
    } else {
        $app->enqueueMessage(JText::_('COM_CUSTOM_LOGIN_FAILED'), 'error');
        $this->setRedirect(JRoute::_('index.php?option=com_custom&view=cart', false));
    }
}

EDIT

Here's the login form. And before you say it, I know - I didn't integrate the 'remember me' function yet. But that doesn't have anything to do with actual authentication.

<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<div class=" col-lg-6 col-md-6 col-sm-6 col-xs-6">
    <h2><span>Sign in to place your order!</span></h2>
    <form id="login-form" method="post" action="<?php echo JROUTE::_('index.php') ?>">
        <fieldset class="userdata">
            <div class="form-group" id="form-login-username">
                <div class="input-group">
                    <span class="input-group-addon">
                        <i title="Username" class="fa fa-user tip"></i>
                    </span>
                    <input type="text" placeholder="Username" size="18" tabindex="0" class="input form-control" name="username">
                </div>
            </div>
            <div class="form-group" id="form-login-password">
                <div class="input-group">
                    <span class="input-group-addon">
                        <i title="Password" class="fa fa-lock tip"></i>
                    </span>
                    <input type="password" placeholder="Password" size="18" tabindex="0" class="input form-control" name="password">
                </div>
            </div>

            <div class="form-group" id="form-login-remember">
                <div class="checkbox">
                    <label for="modlgn-remember">
                        <input type="checkbox" value="yes" class="input" name="remember"> Remember Me</label>
                </div>
            </div>
            <div class="control-group">
                <input type="submit" value="Log in" class="btn btn-primary" name="Submit">
            </div>

            <ul class="unstyled">
                <li>
                    <a href="<?php echo JROUTE::_('index.php?option=com_users&view=registration'); ?>">
                        Create an account <span class="fa fa-arrow-right"></span></a>
                </li>
                <li>
                    <a href="<?php echo JROUTE::_('index.php?option=com_users&view=remind'); ?>">
                        Forgot your username?</a>
                </li>
                <li>
                    <a href="<?php echo JROUTE::_('index.php?option=com_users&view=reset'); ?>">Forgot your password?</a>
                </li>
            </ul>

            <input type="hidden" value="com_custom" name="option">
            <input type="hidden" value="cartlogin" name="task">
            <?php echo JHtml::_( 'form.token' ); ?> 
        </fieldset>
    </form>
</div>

EDIT (2) I suppose I could do something like this, but it doesn't seem like it should be the best practice:

public function cartlogin() {
    $app = JFactory::getApplication();
    $username = $app->input->get('username', '', 'STRING');
    $password = $app->input->get('password', '', 'STRING');
    $remember = $app->input->get('remember', '', 'BOOLEAN');

    $credentials = array(
        'username' => $username, 
        'password' => $password
    );
    $option = array(
        'remember' => $remember
    );
    $result = $app->login($credentials,$option);

    if ($result) {
        $user = JFactory::getUser();
        if ($user->id != 0) {
            $app->enqueueMessage(JText::_('COM_CUSTOM_LOGIN_SUCCESS'), 'message');
            $this->setRedirect(JRoute::_('index.php?option=com_custom&view=cart', false));
        } else {
            $app->enqueueMessage(JText::_('COM_CUSTOM_LOGIN_FAILED'), 'error');
            $this->setRedirect(JRoute::_('index.php?option=com_custom&view=cart', false));
        }
    }
}
6
  • 1
    which joomla version you are using? I checked it with j3.3.6 and it is working properly
    – Nick
    Nov 26, 2014 at 11:19
  • I'm also on 3.3.6
    – James
    Nov 26, 2014 at 17:38
  • Just to clarify - the response is (boolean) TRUE, but the user is not actually authenticated.
    – James
    Nov 26, 2014 at 17:50
  • How did you construct the form which is posting the login request? Nov 26, 2014 at 20:40
  • I just grabbed the login module form and modified it a bit for my component view. I'll edit the question and show the form.
    – James
    Nov 26, 2014 at 21:33

1 Answer 1

1

Nevermind, this does work correctly...I have a different issue happening. I've got a custom authentication plugin installed, and if I disable it then the login works correctly. I'll have to figure out what's wrong with the plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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