From the book (which is getting *very* close to being ready to print):
Is the User Logged In?
We could check the user’s ID or username with one of the methods above, but there is a more efficient way — calling the user object’s hasSessionContext() method. As we mentioned, for any page request, MODX creates a $user object.
The user object’s hasSessionContext() method will return true if the user is logged in (authenticated) and false if the user is not logged in.
If you are only using the default web context (and if you don’t know, then you are), the following code will tell you if the current user is logged in to the front end:
$isAuthenticated = $modx‑>user‑>hasSessionContext('web');
If you are using multiple contexts and want to know if the user is logged in to the current context, you can do this:
$isAuthenticated = $modx‑>user‑>hasSessionContext(
$modx‑>context‑>get('key'));
If you are using multiple contexts and want to know if the user is logged in to any of them, this code will do it:
$isAuthenticated = $modx‑>user‑>hasSessionContext(array(
'context1',
'context2',
'context3'),
);
If you are previewing the site from the MODX Manager, the methods above will still work as described. You will not be authenticated unless you log in using a login form in the front end. Remember, though, that you are still logged in to the Manager, so you may still see unpublished documents and can have manager permissions that a normal front‑end user doesn’t have. To see the site as regular users see it, it’s best to preview the site in another browser where you are not logged in to the Manager.
Note:
The Login package provides a snippet called isLoggedIn that will check the user’s login status for you. If the user is not logged in to the current context, it will automatically redirect the user to the unauthorized page or to a page you specify in the snippet properties.