Login & postHooks

Hello,

I’m trying to use &postHooks for [[!Login]] snippet, but found a weird behavior.
The logged in user is available when logging out, not when logging in.

$modx = $hook->modx;
$user = $modx->getUser();
if ($user) {
    $modx->log(modX::LOG_LEVEL_ERROR, '$user->toArray() ' . print_r($user->toArray(), 1), '', __METHOD__, __FILE__, __LINE__);

}

return true;

Is this intended by design?

The documentation has this line: “However, note that getUser() will not necessarily return the active user object!”

The login extra uses the stardard Modx processors security/login and security/logout, so this is probably standard behaviour. Maybe the changes of the login/logout process only take effect on the next request.

You might try this:

$modx = $hook->modx;
$modx->reloadContext();
$user = $modx->getUser();

I’m not sure if reloadContext() reloads the user info but it’s worth a try.

Another approach would be a plugin attached to OnManagerLogin or OnWebLogin.

I ended up by capturing the request, with assumption that the login attempt is succeeded since the postHooks’s function is called when the login processor returns true.

$modx = $hook->modx;
$user = $modx->getObject("modUser", array(
    'username' => $modx->sanitizeString($_REQUEST["username"])
));
if ($user) {
//    $modx->log(modX::LOG_LEVEL_ERROR, '$user->toArray() ' . print_r($user->toArray(), 1), '', __METHOD__, __FILE__, __LINE__);
}

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.