Skip to content

Instantly share code, notes, and snippets.

@itskingori
Last active January 12, 2016 07:32
Show Gist options
  • Save itskingori/6586756 to your computer and use it in GitHub Desktop.
Save itskingori/6586756 to your computer and use it in GitHub Desktop.
Log in a user programmatically in MODx, answer (hopefully) to a question posted in the forums here; http://forums.modx.com/thread/86714/is-it-possible-to-login-a-user-programmatically-in-frontend
<?php
$id = 1; // just an example ... use appropriate id
$user = $this -> modx -> getObject('modUser', $id);
if (!$user -> hasSessionContext('web')) {
$user -> addSessionContext('web');
}
?>
@gadgetto
Copy link

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);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment