Redirect after AUTH_TRY_LOGIN

Discussion in 'Customization & add-ons' started by edusites, Jul 20, 2015.

  1. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    I've added some code to the hook AUTH_TRY_LOGIN, which validates the user against a table in a database, all working fine until I try to redirect at the end when I try to redirect but get a '302' resource moved error.

    I tried taking the redirect out and I get the following response in the browser:
    How can I redirect after validating the user?
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,274
    You should not add redirect in this hook. aMember may do other important actions after that hook(for example set long time cookies).
    If you want to change default redirect url use Am_Event::AUTH_GET_OK_REDIRECT hook instead.
  3. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    I need to validate the user against a different database, so is it possible to use the AUTH_GET_OK_REDIRECT hook in this case?
  4. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    Also what do I have to return from the AUTH_TRY_LOGIN function, I have tried returning 'true', and some json, but this is what gets sent to the browser:

    {"ok":false,"error":["The username or password is incorrect"],"code":-6}
  5. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,274
    You should return created user:
    PHP:
            $event->setCreated($user);
  6. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,274
    Yes possible. That hook will be executed in anyway after successful login.
  7. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    I have AUTH_TRY_LOGIN working, it checks a custom table in a database (expressionengine) and allows me to set relevant cookies and session info.

    However when I try to add a function to the AUTH_GET_OK_REDIRECT hook, I get 'The username or password is incorrect' and no redirect happens (although the user is logged in).

    I found that the error thrown is in a function that is called by this hook:
    MAIL_TEMPLATE_BEFORE_PARSE

    Can you tell me please, why is this hook also being called?
  8. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,274
    What exact code do you have?
    Do you create user in amember from AUTH_TRY_LOGIN hook?
    When you create user amember will send several email messages to new user, for example registration email if it is enabled, so that's why MAIL_TEMPLATE_BEFORE_PARSE is being executed as well.
  9. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    I do create a user, thank you that explains it.

    But I seem to have another problem, my code is not always called, I have striped it back to be very basic:

    Am_Di::getInstance()->hook->add(Am_Event::AUTH_TRY_LOGIN, 'test');

    function test(Am_Event $event)
    {
    echo 'here!';
    die;
    }

    Can you tell me why?

    The response is always:
    {"ok":false,"error":["The username or password is incorrect"],"code":-2}
  10. edusites

    edusites aMember Pro Customer

    Joined:
    Aug 9, 2009
    Messages:
    47
    I'm not sure why I am getting the response detailed in the above post, however I know how to avoid it: I need to create a new user, correctly (I'm not sure how to create a correctly encrypted password), in aMember too.

    Can you tell me how to create a user programmatically in aMember please?

    Many thanks for all your help.

Share This Page