1

I'm trying to dismiss the login if my $completed var is false. I can make it work with die() but I wan't to know how to do this correct, so that the user gets returned to the login page with a error message.

I've tried with return NULL;, return false; and just return;

I can't make it work or find the solution anywhere. I really hope you can help me out.

This is my code:

defined('_JEXEC') or die;

class plgAuthenticationTestlogin extends JPlugin{
    function onUserAuthenticate( $credentials, $options, &$response ){
        $captcha_plugin = JFactory::getConfig()->get('captcha');
        $captcha = JCaptcha::getInstance($captcha_plugin);
        $completed = $captcha->CheckAnswer('captcha');
        if(!$completed){
            $response->status = JAuthentication::STATUS_FAILURE;
            $response->error_message = 'captcha not correct';
            die("not valid reCaptcha");
        }
    }
}

It is placed inside a plugin made. placed inside the folder: plugins > authentication > mypluginFolder > myplugin.php This plugin works fine as described above - The login is aborted if I use die("not valid reCaptcha");

3
  • 1
    I was going to recommend this to you (I don't know if you saw my ->redirect() comment before I deleted it). Inspired by, stackoverflow.com/q/24556095/2943403 Unfortunately, your updated question is now seeking a "Yes/No" response. Perhaps you should post your update as an answer, then if someone offers a better answer, you can upvote/accept the other. Until then, it looks like you have the answer that you can be happy with. Oct 23, 2018 at 21:54
  • Can anyone help me to "consult the libraries/joomla/user/authentication.php file" as advised by: docs.joomla.org/… ? I don't see a user directory in libraries/joomla. Perhaps the docs need to be updated. Oct 23, 2018 at 23:11
  • 1
    on mickmackusa's question - After Joomla 3.8 a big part of Joomla libraries were moved to the libraries/src directory, that's where you can find the User or the Authentication library. If that's what you were asking for.
    – Zollie
    Oct 25, 2018 at 8:21

2 Answers 2

1

This is not a correct way of doing this as you should not use 'die'

The proper way to do this will be using this method in the Authentication plugin

function onUserAuthenticate($credentials, $options, &$response)
{
    // check your condition and stop login here
    $response->status        = JAuthentication::STATUS_FAILURE;
    $response->type          = 'Test';
    $response->error_message = JText::sprintf('JGLOBAL_AUTH_FAILED', JText::_('NOT_LOGGED_IN'));
    return false;
}

You cannot set redirect here, to set redirect make sure to set it in the parameter(variable in JInput)

If the method is not triggered make sure to set the ordering in the correct sequence.

4
  • Thanks!!... Is it not possible to do in a plugin? Do I then have to override original Authentication ? Oct 25, 2018 at 8:24
  • This is the correct way, you must be missing something, make sure to check if your plugin is triggered and some other plugin may not be interrupting your flow.
    – Pratyush
    Oct 25, 2018 at 8:24
  • hmmm. Maybe because I've missed $response->type ? ....I will try it out tonight! Thanks for replying Oct 25, 2018 at 8:26
  • but I think your issue is in the sequence of plugins being triggered if any plugin responses success before your plugin is triggered then your plugin will not work.
    – Pratyush
    Oct 25, 2018 at 8:34
0

Looks like it's working this way. But is that the correct approach?

if(!$completed){
    $app = JFactory::getApplication();
    $app->enqueueMessage('ReCaptcha not validated', 'Fail');
    $app->redirect(JRoute::_('index.php'));
    die();
}

I feel like it's wrong and I should be using JAuthentication::STATUS_FAILURE;

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.