Last active
February 27, 2026 15:26
-
-
Save matdave/ab71a5a8157323a7be7721a0bae01e91 to your computer and use it in GitHub Desktop.
Magic Link Hook for FormIt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <p> | |
| Please use the following link to log in to your account: <a href="[[+url]]?l=[[+token]]">[[+url]]?l=[[+token]]</a> | |
| </p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // MagicLinkHook for FormIt // | |
| if (isset($_SESSION['modx.user.failedLoginCount']) && $_SESSION['modx.user.failedLoginCount'] >= 5) { | |
| if (!isset($_SESSION['modx.user.failedLoginOn'])) { | |
| $_SESSION['modx.user.failedLoginOn'] = time(); | |
| } | |
| if (time() - $_SESSION['modx.user.failedLoginOn'] > 900) { | |
| unset($_SESSION['modx.user.failedLoginCount'], $_SESSION['modx.user.failedLoginOn']); | |
| } else { | |
| return true; | |
| } | |
| } | |
| $hook = $sp['hook']; | |
| $resource = $modx->resource; | |
| $email = $hook->getValue('email'); | |
| $user = $modx->getObject(modUser::class, ['username' => $email]); | |
| if ($user) { | |
| $context = $modx->context->key; | |
| $_SESSION["modx.{$context}.user.token"] = $user->generateToken($context); | |
| $cacheManager = $modx->getCacheManager(); | |
| $cacheManager->set($_SESSION["modx.{$context}.user.token"], $user->id, 900); | |
| $options = [ | |
| 'token' => substr($_SESSION["modx.{$context}.user.token"], strlen($modx->site_id.'_')), | |
| 'url' => $modx->makeUrl($resource->id, $context, '', 'full'), | |
| ]; | |
| $message = $modx->getChunk('MagicLinkEmailLogin', $options); | |
| $user->sendEmail($message, [ | |
| 'subject' => 'Login to your account' | |
| ]); | |
| } | |
| if (isset($_SESSION['modx.user.failedLoginCount'])) { | |
| $_SESSION['modx.user.failedLoginCount']++; | |
| } else { | |
| $_SESSION['modx.user.failedLoginCount'] = 1; | |
| } | |
| return true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // MagicLinkPrehook for FormIt // | |
| if (isset($_GET['l'])) { | |
| if (isset($_SESSION['modx.user.failedLoginCount']) && $_SESSION['modx.user.failedLoginCount'] >= 5) { | |
| if (!isset($_SESSION['modx.user.failedLoginOn'])) { | |
| $_SESSION['modx.user.failedLoginOn'] = time(); | |
| } | |
| if (time() - $_SESSION['modx.user.failedLoginOn'] > 900) { | |
| unset($_SESSION['modx.user.failedLoginCount'], $_SESSION['modx.user.failedLoginOn']); | |
| } else { | |
| $modx->sendUnauthorizedPage(); | |
| } | |
| } | |
| $token = $_GET['l']; | |
| $cacheManager = $modx->getCacheManager(); | |
| $userId = $cacheManager->get($modx->site_id.'_'.$token); | |
| if (!empty($userId)) { | |
| $modx->user = $modx->getObjectGraph( | |
| modUser::class, | |
| '{"Profile":{},"UserSettings":{}}', | |
| ['modUser.id' => $userId] | |
| ); | |
| $modx->user->addSessionContext('web'); | |
| $modx->user->loadAttributes(array(), 'web', false); | |
| $modx->user->set('lastlogin', date('Y-m-d H:i:s')); | |
| $modx->user->save(); | |
| if (isset($_SESSION['modx.user.failedLoginCount'])) { | |
| unset($_SESSION['modx.user.failedLoginCount']); | |
| } | |
| $redirect = $modx->getOption('loginResourceId', $scriptProperties, $modx->getOption('site_start')); | |
| $modx->sendRedirect($modx->makeUrl($redirect)); | |
| } else { | |
| if (isset($_SESSION['modx.user.failedLoginCount'])) { | |
| $_SESSION['modx.user.failedLoginCount']++; | |
| } else { | |
| $_SESSION['modx.user.failedLoginCount'] = 1; | |
| } | |
| $modx->sendUnauthorizedPage(); | |
| } | |
| } | |
| if (isset($_GET['logout'])) { | |
| $modx->user->removeSessionContext($modx->context->key); | |
| $modx->user = null; | |
| $modx->sendRedirect($modx->makeUrl($modx->resource->id)); | |
| } | |
| if ($modx->user->isAuthenticated('web')) { | |
| $redirect = $modx->getOption('loginResourceId', $scriptProperties, $modx->getOption('site_start')); | |
| $modx->sendRedirect($modx->makeUrl($redirect)); | |
| } | |
| return true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment