We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36734
    • 14 Posts
    Hello,

    here is what I'm trying to achieve. I want a website with 2 contexts: one public (basically a login page), the other one for members-only (basically, the rest of the website).

    I want user to request membership by filling a form, then get an email to activate their account. Everything works so far: user receives email, then can follow the link and get their account activated.

    Now, I want two things:
    - authenticate the user who just activated their account
    - redirect those user to the 'members only' section as they just became members.

    The problem I encounter every time is that, from ConfirmRegister, it seems that the URL can not be generated properly if it points to a resource in an another context.

    I ran an experiment to try to understand why it behaves like that and I am now stuck. Here is what I did.

    On the public landing page (1) of the website, I have :

    [[!Login?
      &loginTpl=`lgnLoginTpl`
      &logoutTpl=`lgnLogoutTpl`
      &errTpl=`lgnErrTpl`
      &contexts=`members,web`
      &logoutResourceId=`0`
      &logoutKey=`logout`
    ]]
    
    [[!RedirectIfAuthenticated]]
    


    Here is the code for RedirectIfAuthenticated (which was a dirty hack attempt to circumvent the more general problem):

    if($modx->user->isAuthenticated('web')){
        echo 'User Authenticated (web)';
    } else {
        echo 'Not Authenticated (web)';
    }
    
    if($modx->user->isAuthenticated('members')){
        $url = $modx->makeUrl(3, 'members');
        echo 'User Authenticated (members)';
        echo 'URL: '.$url;
    //    $modx->sendRedirect($url);
        return;
    } else {
        echo 'Not Authenticated (members)';
    }


    Finally, I have the confirmRegister page with the following:
    [[!ConfirmRegister?
        &redirectTo=`1`
        &authenticate=`1`
        &authenticateContexts=`members,web`
        &errorPage=`98`
    ]]


    Scenario 1: user creates an account, get the email, follow the link.
    The user is properly redirect back to the first resource, the public page. RedirectIfAuthenticated indicates that the user is properly logged in to both web and members context. However, the url generated is empty.

    Scenario 2: user logs out, then log back in using the Login snippet:
    first, the user is properly logged out from both web and members context (great !) then properly logged back in to both context. This time, RedirectIfAuthenticated also provides a valid URL for the target page in the other context (and I can access it -- everything works).

    Am I doing something wrong ? How come makeUrl can not generate the URL in one case but can in the other, even though the user seems to be genuinely authenticated both time ?

    Miscellaneous information:
    MODX Revolution 2.2.0-pl2 (traditional)
    Login 1.7.3.

    Thanks !
      • 36734
      • 14 Posts
      Ah !

      This is a apparently a bug, there is a pull request, and it works beautifully with the fix (no more need for a dirty hack !).

      https://github.com/splittingred/Login/pull/53
        • 28945
        • 12 Posts
        I had this bug (logging in user as "anonymous") also, therefore I tried the fix but I wonder if this behavior ic correctly.
        After the user clicks now within the e-mail the activation link he is immediately logged in at my site.
        But only when he log out and login again he is redirected to the member only page like it should..Is this normal ?
        I`m still testing.
          • 3749
          • 24,544 Posts
          Did you clear the cache after doing the fix? You may need to manually delete all files in the core/cache directory and clear your browser cache and cookies.


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting