We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40735
    • 119 Posts
    Hi!
    I'm building an AJAX login and I'm wondering if the Login snippet has hooks for being used as a service. Perhaps like, [[~idOfPageWithLoginSnippet]]?service=login&user=username&pass=password or something similar? Is this possible or will I need to write my own snippet to do this?
    Thanks!
    • Have you tried your own suggestion? Without testing, I reckon that would work, tho you'll need to refresh the origin page after that to see the effects.

      If it doesnt work, use n ajax post instead of get; might help.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 3749
        • 24,544 Posts
        I think that should work. I couldn't find an example of it for login, but this will log a user out:

        <a href="[[~[[$loginPageId]]]]&service=logout">


        so I'm guessing it would be something like:

        <a href="[[~[[$loginPageId]]]]&service=login;&username='username';&password='password'">



        It might be easier to just log the user in directly if you have the user object with:

        $modx->user =& $user;
        $modx->getUser();
        $user->addSessionContext('web');


        I'm not sure which would be more secure.
          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
          • 38878
          • 255 Posts
          The &service=logout redirects the user to the logut page (not quite the "ajaxy" feel of retaining the current page w/o reload). I have not tried to pass the current resource ID as the &redirectTo argument in the call though it could work on login and logout service calls but it would reload the existing resource I imagine. Again sort of defeats the purpose of an Ajax call.

          I would be interested in the outcome if anyone tries it though.

          I have done login with an ajax login resource passing the data back and forth via json (which I didn't really need to do) and that works fine, but takes a bit more setup. IIRC, Splittingred posted something a while back on how to set up ajax resources but I couldn't find it.
            • 3749
            • 24,544 Posts
            You can log the user out and stay on the same page with this:

            $modx->user->removeSessionContext('web');
              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
              • 38878
              • 255 Posts
              I'm curious what that would do to a personalize snippet on the same page.

              Quote from: BobRay at Jan 26, 2013, 05:56 AM
              You can log the user out and stay on the same page with this:

              $modx->user->removeSessionContext('web');
              • If the personalize snippet appears before the logout snippet in the page's code, I would expect it to see the user as still being logged in, since the user doesn't get "logged out" until the logout snippet has run.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org