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

    I'd like to login a user programmatically in frontend so that I can use the UpdateProfile snippet from Login add-on without needing the user to enter/know his credentials.

    I've created an extended user table which holds a unique secure id and already have written a method to fetch the user object based on that secure id.

    Is this possible - and how can this be done?
    Can't find a reference in this forum using the search.

    Thanks in advance,
    Martin

    This question has been answered by multiple community members. See the first response.

      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
    • Do you need UpdateProfile or can you just update the object you're fetching?
        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.
        • 10378
        • 375 Posts
        Hi Marc,

        oh boy... as often I lose sight of the wood for the trees...
        For sure I can simply update the user object! Sorry for steeling your time!

        Thanks,
        Martin
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
          • 10378
          • 375 Posts
          Quote from: gadgetto at Sep 16, 2013, 08:35 PM
          For sure I can simply update the user object! Sorry for steeling your time!

          Hmm... but the problem is, I then have to do form field validation, error handling and so on manually. The UpdateProfile snippet would save me a lot of work (as I already use the Register snippet from Login add-on in other parts of my code).
            Freelancer @bitego http://www.bitego.com
            ---
            GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
            More infos here: http://www.bitego.com/extras/goodnews/
          • discuss.answer
            • 14372
            • 49 Posts
            Technically ... this is possible (correct me if I'm wrong) ... the general idea is that a user being logged in boils down to them having a session.

            Step one is to load the user object (I'm assuming in your case you already have a user in the DB) ... once the user is loaded into, let's say ... $user ... do this ...

            if (!$user -> hasSessionContext('web')) {
                $user -> addSessionContext('web');
            }
            


            It just creates a session for that user at the moment ONLY if the user doesn't have a session.

            Feel free to ask question ...

            Update:

            There's a gist on Github with a code snippet here; https://gist.github.com/itsmrwave/6586756 [ed. note: jobkingori last edited this post 10 years, 8 months ago.]
            • discuss.answer
              • 10378
              • 375 Posts
              Quote from: jobkingori at Sep 16, 2013, 09:12 PM
              Technically ... this is possible (correct me if I'm wrong) ... the general idea is that a user being logged in boils down to them having a session.

              Step one is to load the user object (I'm assuming in your case you already have a user in the DB) ... once the user is loaded into, let's say ... $user ... do this ...

              if (!$user -> hasSessionContext('web')) {
                  $user -> addSessionContext('web');
              }
              


              It just creates a session for that user at the moment ONLY if the user doesn't have a session.

              Feel free to ask question ...

              Update:

              There's a gist on Github with a code snippet here; https://gist.github.com/itsmrwave/6586756

              Thanks for your hint!
              For more flexibility I use:

                  $contexts = !empty($scriptProperties['authenticateContexts']) ? $scriptProperties['authenticateContexts'] : $modx->context->get('key');
                  $contexts = explode(',',$contexts);
                  foreach ($contexts as $ctx) {
                      if (!$user->hasSessionContext($ctx)) {
                          $user->addSessionContext($ctx);
                      }
                  }
              
                Freelancer @bitego http://www.bitego.com
                ---
                GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                More infos here: http://www.bitego.com/extras/goodnews/
                • 14372
                • 49 Posts
                @gadgetto ... yes, that would work better for all contexts. Thanks!