mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-31 01:35:15 +02:00
[Ivan Diaz] - Add response object and errors class
This commit is contained in:
parent
9b6ad74e6a
commit
4331a14004
@ -13,7 +13,9 @@ $app->group('/user', function () use ($app) {
|
|||||||
'admin' => 0
|
'admin' => 0
|
||||||
));
|
));
|
||||||
$id = $userInstance->store();
|
$id = $userInstance->store();
|
||||||
$app->response()->setBody("{ \"id\": $id }");
|
Response::respondSuccess(array(
|
||||||
|
'id' => $id
|
||||||
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->post('/login', function () use ($app) {
|
$app->post('/login', function () use ($app) {
|
||||||
@ -23,11 +25,12 @@ $app->group('/user', function () use ($app) {
|
|||||||
if ($userInstance = User::getUser($user, 'user')) {
|
if ($userInstance = User::getUser($user, 'user')) {
|
||||||
$pass = $userInstance->password;
|
$pass = $userInstance->password;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($pass === $password) {
|
if ($pass === $password) {
|
||||||
$app->response()->setBody("{ \"response\": \"OK\" }");
|
Response::respondSuccess();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$app->response()->setBody("{ \"response\": \"FAIL\" }");
|
Response::respondError(ERRORS::UNDEFINED_CREDENTIALS);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
4
server/models/ERRORS.php
Normal file
4
server/models/ERRORS.php
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
class ERRORS {
|
||||||
|
const UNDEFINED_CREDENTIALS = 'User or password is not defined';
|
||||||
|
}
|
25
server/models/Response.php
Normal file
25
server/models/Response.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
class Response{
|
||||||
|
private static $errored;
|
||||||
|
|
||||||
|
public static function respondError($errorMsg, $data = null) {
|
||||||
|
$response = array(
|
||||||
|
'status' => 'fail',
|
||||||
|
'message' => $errorMsg,
|
||||||
|
'data' => $data
|
||||||
|
);
|
||||||
|
|
||||||
|
$app = \Slim\Slim::getInstance();
|
||||||
|
$app->response()->setBody(json_encode($response));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function respondSuccess($data = null) {
|
||||||
|
$response = array(
|
||||||
|
'status' => 'success',
|
||||||
|
'data' => $data
|
||||||
|
);
|
||||||
|
|
||||||
|
$app = \Slim\Slim::getInstance();
|
||||||
|
$app->response()->setBody(json_encode($response));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user