Skip to content

Commit 0bddea6

Browse files
committed
JSON responses for the Login controller/snippet
Merge remote-tracking branch 'upstream/pr/121' * upstream/pr/121: Fixed success variable names. Added JSON output options for logout and added success response for login. Needs a lot of testing. Added JSON error output response in /controllers/web/login.php for prehooks,posthooks and login. Todo: success response.
2 parents bbb1c24 + 99dfbf8 commit 0bddea6

1 file changed

Lines changed: 96 additions & 10 deletions

File tree

  • core/components/login/controllers/web

core/components/login/controllers/web/Login.php

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function initialize() {
5050
'rememberMeKey' => 'rememberme',
5151
'loginContext' => $this->modx->context->get('key'),
5252
'contexts' => '',
53+
'jsonResponse' => false,
5354
));
5455

5556
if (!empty($_REQUEST['login_context'])) {
@@ -195,23 +196,41 @@ public function login() {
195196

196197
if ($this->runPreLoginHooks()) {
197198
$response = $this->runLoginProcessor();
198-
199199
/* if we've got a good response, proceed */
200200
if (!empty($response) && !$response->isError()) {
201201
$this->runPostLoginHooks($response);
202202

203203
/* process posthooks for login */
204204
if ($this->postHooks->hasErrors()) {
205-
$errorPrefix = $this->getProperty('errorPrefix','error');
206-
$this->modx->toPlaceholders($this->postHooks->getErrors(),$errorPrefix);
207-
205+
$errors = $this->postHooks->getErrors();
208206
$errorMsg = $this->postHooks->getErrorMessage();
207+
// Return JSON posthook errors if requested.
208+
if ($this->getProperty('jsonResponse')) {
209+
$jsonErrorOutput = array(
210+
'success' => false,
211+
'message' => $errorMsg,
212+
'errors' => $errors
213+
);
214+
header('Content-Type: application/json;charset=utf-8');
215+
exit($this->modx->toJSON($jsonErrorOutput));
216+
}
217+
$errorPrefix = $this->getProperty('errorPrefix','error');
218+
$this->modx->toPlaceholders($errors,$errorPrefix);
209219
$this->modx->toPlaceholder('message',$errorMsg,$errorPrefix);
210220
} else {
221+
// Return JSON success response if requested.
222+
if ($this->getProperty('jsonResponse')) {
223+
$jsonSuccessOutput = array(
224+
'success' => true,
225+
'message' => $this->getProperty('loginMsg')
226+
);
227+
header('Content-Type: application/json;charset=utf-8');
228+
exit($this->modx->toJSON($jsonSuccessOutput));
229+
}
211230
$this->redirectAfterLogin($response);
212231
}
213232

214-
/* logout failed, output error */
233+
/* login failed, output error */
215234
} else {
216235
$this->checkForRedirectOnFailedAuth($response);
217236
$errorOutput = $this->prepareFailureMessage($response,$this->modx->lexicon('login.login_err'));
@@ -236,7 +255,17 @@ public function runPreLoginHooks() {
236255

237256
/* process prehooks */
238257
if ($this->preHooks->hasErrors()) {
239-
$this->modx->toPlaceholders($this->preHooks->getErrors(),$this->getProperty('errorPrefix','error'));
258+
$errors = $this->preHooks->getErrors();
259+
// Return JSON prehook errors if requested.
260+
if ($this->getProperty('jsonResponse')) {
261+
$jsonErrorOutput = array(
262+
'success' => false,
263+
'errors' => $errors
264+
);
265+
header('Content-Type: application/json;charset=utf-8');
266+
exit($this->modx->toJSON($jsonErrorOutput));
267+
}
268+
$this->modx->toPlaceholders($errors,$this->getProperty('errorPrefix','error'));
240269

241270
$errorMsg = $this->preHooks->getErrorMessage();
242271
$errorOutput = $this->modx->getChunk($this->getProperty('errTpl'), array('msg' => $errorMsg));
@@ -281,12 +310,40 @@ public function prepareFailureMessage(modProcessorResponse $response,$defaultErr
281310
$errors = $response->getFieldErrors();
282311
$message = $response->getMessage();
283312
if (!empty($errors)) {
313+
// Return JSON login errors if requested.
314+
if ($this->getProperty('jsonResponse')) {
315+
$jsonErrorOutput = array(
316+
'success' => false,
317+
'message' => $message,
318+
'errors' => $errors
319+
);
320+
header('Content-Type: application/json;charset=utf-8');
321+
exit($this->modx->toJSON($jsonErrorOutput));
322+
}
284323
foreach ($errors as $error) {
285324
$errorOutput .= $this->modx->getChunk($errTpl, $error);
286325
}
287326
} elseif (!empty($message)) {
327+
// Return JSON error message in response if requested.
328+
if ($this->getProperty('jsonResponse')) {
329+
$jsonErrorOutput = array(
330+
'success' => false,
331+
'message' => $message
332+
);
333+
header('Content-Type: application/json;charset=utf-8');
334+
exit($this->modx->toJSON($jsonErrorOutput));
335+
}
288336
$errorOutput = $this->modx->getChunk($errTpl, array('msg' => $message));
289337
} else {
338+
// Return JSON default error if requested.
339+
if ($this->getProperty('jsonResponse')) {
340+
$jsonErrorOutput = array(
341+
'success' => false,
342+
'message' => $defaultErrorMessage
343+
);
344+
header('Content-Type: application/json;charset=utf-8');
345+
exit($this->modx->toJSON($jsonErrorOutput));
346+
}
290347
$errorOutput = $this->modx->getChunk($errTpl, array('msg' => $defaultErrorMessage));
291348
}
292349
return $errorOutput;
@@ -380,6 +437,15 @@ public function logout() {
380437
/* if successful logout */
381438
if (!empty($response) && !$response->isError()) {
382439
$this->runPostLogoutHooks($response);
440+
// Return JSON logout success
441+
if ($this->getProperty('jsonResponse')) {
442+
$jsonSuccessOutput = array(
443+
'success' => true,
444+
'message' => $this->getProperty('logoutMsg')
445+
);
446+
header('Content-Type: application/json;charset=utf-8');
447+
exit($this->modx->toJSON($jsonSuccessOutput));
448+
}
383449
$this->redirectAfterLogout($response);
384450

385451
/* logout failed, output error */
@@ -401,9 +467,19 @@ public function runPreLogoutHooks() {
401467
));
402468

403469
if ($this->preHooks->hasErrors()) {
404-
$this->modx->toPlaceholders($this->preHooks->getErrors(),$this->getProperty('errorPrefix','error'));
405-
470+
$errors = $this->preHooks->getErrors();
406471
$errorMsg = $this->preHooks->getErrorMessage();
472+
// Return JSON login errors if requested.
473+
if ($this->getProperty('jsonResponse')) {
474+
$jsonErrorOutput = array(
475+
'success' => false,
476+
'message' => $errorMsg,
477+
'errors' => $errors
478+
);
479+
header('Content-Type: application/json;charset=utf-8');
480+
exit($this->modx->toJSON($jsonErrorOutput));
481+
}
482+
$this->modx->toPlaceholders($errors,$this->getProperty('errorPrefix','error'));
407483
$errorOutput = $this->modx->getChunk($this->getProperty('errTpl'), array('msg' => $errorMsg));
408484
$this->modx->setPlaceholder('errors',$errorOutput);
409485
$success = false;
@@ -434,13 +510,23 @@ public function runPostLogoutHooks(modProcessorResponse $response) {
434510

435511
/* log posthooks errors */
436512
if ($this->postHooks->hasErrors()) {
437-
$this->modx->log(modX::LOG_LEVEL_ERROR,'[Login] Post-Hook errors: '.print_r($this->postHooks->getErrors(),true));
438-
513+
$errors = $this->postHooks->getErrors();
514+
$this->modx->log(modX::LOG_LEVEL_ERROR,'[Login] Post-Hook errors: '.print_r($errors,true));
439515
$errorMsg = $this->postHooks->getErrorMessage();
440516
if (!empty($errorMsg)) {
441517
$this->modx->log(modX::LOG_LEVEL_ERROR,'[Login] Post-Hook error: '.$errorMsg);
442518
}
443519
$success = false;
520+
// Return JSON posthook logout errors if requested.
521+
if ($this->getProperty('jsonResponse')) {
522+
$jsonErrorOutput = array(
523+
'success' => false,
524+
'message' => $errorMsg,
525+
'errors' => $errors
526+
);
527+
header('Content-Type: application/json;charset=utf-8');
528+
exit($this->modx->toJSON($jsonErrorOutput));
529+
}
444530
}
445531
return $success;
446532
}

0 commit comments

Comments
 (0)