Skip to content

Instantly share code, notes, and snippets.

@amitpatil321
Created February 26, 2016 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitpatil321/4391b9ad6ba4b054ac47 to your computer and use it in GitHub Desktop.
Save amitpatil321/4391b9ad6ba4b054ac47 to your computer and use it in GitHub Desktop.
Modx login with code
if(trim($username) != "" and trim($password) != ""){
// Load lexicons to show proper error messages
if (!isset($modx->lexicon) || !is_object($modx->lexicon)) {
$modx->getService('lexicon','modLexicon');
}
$modx->lexicon->load('login');
$loginContext= isset ($scriptProperties['login_context']) ? $scriptProperties['login_context'] :
$modx->context->get('key');
$addContexts= isset ($scriptProperties['add_contexts']) && !empty($scriptProperties['add_contexts']) ? explode(',', $scriptProperties['add_contexts']) : array();
$mgrEvents = ($loginContext == 'mgr');
$givenPassword = $password;
/** @var $user modUser */
$user= $modx->getObjectGraph('modUser', '{"Profile":{},"UserSettings":{}}', array ('modUser.username' => $username));
if (!$user) {
$ru = $modx->invokeEvent("OnUserNotFound", array(
'user' => &$user,
'username' => $username,
'password' => $password,
'attributes' => array(
'loginContext' => $loginContext,
)
));
if (!empty($ru)) {
foreach ($ru as $obj) {
if (is_object($obj) && $obj instanceof modUser) {
$user = $obj;
break;
}
}
}
if (!is_object($user) || !($user instanceof modUser)) {
echo json_encode(array("error" => "1", "code" => "5", "msg" => "Cannot find user"));
exit;
}
}
if (!$user->get('active')) {
echo json_encode(array("error" => "1", "code" => "4", "msg" => "Account not activated"));
exit;
}
if (!$user->passwordMatches($givenPassword)) {
if (!array_key_exists('login_failed', $_SESSION)) {
$_SESSION['login_failed'] = 0;
}
if ($_SESSION['login_failed'] == 0) {
$flc = ((integer) $user->Profile->get('failedlogincount')) + 1;
$user->Profile->set('failedlogincount', $flc);
$user->Profile->save();
$_SESSION['login_failed']++;
} else {
$_SESSION['login_failed'] = 0;
}
$app->response->setStatus(200);
$app->response()->headers->set('Content-Type', 'applic ation/json');
echo json_encode(array("error" => "1", "code" => "2", "msg" => "User not found"));
exit;
}
//$info['id'] = $row['id'];
$info['id'] = $user->Profile->get('id');
$info['fullname'] = $user->Profile->get('fullname');
$info['email'] = $user->Profile->get('email');
echo json_encode(array("error" => "0", "code" => "3", "msg" => json_encode($info) ));
}else{
echo '{"error":"0","code" => "1", "msg":"Insufficiant data"}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment