Skip to content

Commit

Permalink
api response code, login with api
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroniker committed Jan 20, 2019
1 parent 7e9d91d commit eabe081
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 46 deletions.
51 changes: 26 additions & 25 deletions gear/classes/api.php
Expand Up @@ -46,21 +46,19 @@ public function __call($method, $params) {

$route = $params[0];
$callback = $params[1];
$errorCallback = isset($params[2]) ? $params[2] : false;

$this->addRoute($route, $method, $callback, $errorCallback);
$this->addRoute($route, $method, $callback);

return;

}

protected function addRoute($route, $method, $callback, $errorCallback) {
protected function addRoute($route, $method, $callback) {

$data = [
'route' => str_replace('//', '/', $this->base.$route),
'method' => strtoupper($method),
'callback' => $callback,
'errorCallback' => $errorCallback
'callback' => $callback
];

array_push($this->routes, $data);
Expand All @@ -70,16 +68,28 @@ protected function addRoute($route, $method, $callback, $errorCallback) {
}

private function runCallback($function, $params = []) {

$array[] = $this->app;
echo call_user_func_array($function, array_merge($array, $params));

echo $this->response(call_user_func_array($function, array_merge($array, $params)));

}

private function response($return) {

$responseCode = (isset($return['code'])) ? $return['code'] : 200;

http_response_code($responseCode);

return $return['data'];

}

public function getAllRoutes() {
foreach($this->app->modules->all() as $module) {
if(isset($module->options['api']) && is_array($module->options['api']) && count($module->options['api'])) {
foreach($module->options['api'] as $route => $array) {
$errorCallback = (isset($array['errorCallback'])) ? $array['errorCallback'] : null;
$this->addRoute($route, $array['method'], $array['callback'], $errorCallback);
$this->addRoute($route, $array['method'], $array['callback']);
}
}
}
Expand All @@ -101,9 +111,6 @@ public function run() {
foreach($this->routes as $data) {
if(self::validMethod($data['method'], $method) && ($data['route'] === $this->app->route->route)) {
$foundRoute = true;
if($data['errorCallback'] && is_callable($data['errorCallback'])) {
$this->errorCallback = $data['errorCallback'];
}
$this->runCallback($data['callback']);
break;
}
Expand All @@ -129,9 +136,6 @@ public function run() {
}
}
$matched = $newMatched;
if($data['errorCallback'] && is_callable($data['errorCallback'])) {
$this->errorCallback = $data['errorCallback'];
}
$this->runCallback($data['callback'], $matched);
break;
}
Expand All @@ -140,24 +144,21 @@ public function run() {
}

if($foundRoute === false) {
if(!$this->errorCallback) {
$this->errorCallback = function() {
header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");
return __('URL <strong>%s</strong> not found', [$this->app->route->route]);
};
}
echo call_user_func($this->errorCallback);
$this->errorCallback = function() {
return [
'code' => 404,
'data' => __('URL <strong>%s</strong> not found', [$this->app->route->route])
];
};
echo $this->response(call_user_func($this->errorCallback));
}

}

public static function getPost($key = false) {
$json = json_decode(file_get_contents('php://input'), true);
$data = (isset($json) && is_array($json)) ? $json : [];
if($key) {
return $data[$key];
}
return $data;
return ($key) ? $data[$key] : $data;
}

protected static function isAjax() {
Expand Down
8 changes: 6 additions & 2 deletions gear/classes/message.php
Expand Up @@ -11,7 +11,9 @@ function __construct($app) {
$this->app = $app;

$this->app->api->get('/messages', function() {
return json_encode($this->getAll());
return [
'data' => json_encode($this->getAll())
];
});

$this->app->api->post('/addMessage', function() {
Expand All @@ -21,7 +23,9 @@ function __construct($app) {

$this->app->api->delete('/message/{i}', function($app, $index) {
$this->delete($index);
return json_encode($this->getAll());
return [
'data' => json_encode($this->getAll())
];
});

}
Expand Down
16 changes: 12 additions & 4 deletions gear/modules/auth/classes/auth.php
Expand Up @@ -27,31 +27,39 @@ public function login($email, $password, $remember = 0) {

if($block = $this->attempt->isBlocked()) {
$this->app->message->add(__('Login blocked for %s min %s sec', [$block['minutes'], $block['seconds']]), 'error');
return false;
return [
'code' => 401
];
}

$validateEmail = $this->validateEmail($email);

if($validateEmail['error']) {
$this->attempt->add();
$this->app->message->add($validateEmail['message'], 'error');
return false;
return [
'code' => 401
];
}

$userID = $this->app->user->getID(strtolower($email));

if(!$userID) {
$this->attempt->add();
$this->app->message->add('Email not found', 'error');
return false;
return [
'code' => 401
];
}

$user = $this->app->user->getUser($userID);

if(!$this->passwordRehash($password, $user['password'], $userID)) {
$this->attempt->add();
$this->app->message->add('Incorrect password', 'error');
return false;
return [
'code' => 401
];
}

$session = $this->addSession($user['id'], $remember);
Expand Down
10 changes: 10 additions & 0 deletions gear/modules/auth/index.php
Expand Up @@ -24,6 +24,16 @@
]
],

'api' => [
'/auth/login' => [
'method' => 'POST',
'callback' => function($app) {
$user = api::getPost('data');
return $app->auth->login($user['email'], $user['password'], $user['remember']);
}
]
],

'action' => [
'application.boot-5' => function($app) {

Expand Down
12 changes: 0 additions & 12 deletions gear/system/modules/user/index.php
Expand Up @@ -45,18 +45,6 @@
'table' => 'users'
],

'action' => [
'application.boot-6' => function($app) {

if(type::post('action') == 'login') {
if($app->auth->login(type::post('email'), type::post('password'), type::post('remember'))) {
$app->route->redirect('/dashboard');
}
}

}
],

'required' => [
'auth'
],
Expand Down
3 changes: 2 additions & 1 deletion gear/system/modules/user/views/login.php
Expand Up @@ -2,7 +2,8 @@

$form = new form();

$form->addHidden('action', 'login');
$form->addProp('route', '/auth/login');
$form->addProp('redirect', '/dashboard');

$form->addText('email', null, [
'placeholder' => __('Email')
Expand Down
3 changes: 1 addition & 2 deletions gear/system/scripts/form/GForm.vue
Expand Up @@ -49,8 +49,7 @@ export default {
self.$api.post(self.route, {
data: self.data
}).then(function(response) {
console.log(self.redirect);
if(self.redirect) {
if(response.status == 200 && self.redirect) {
window.location.replace(self.$gear.url + self.$gear.adminURL + self.redirect);
}
});
Expand Down

0 comments on commit eabe081

Please sign in to comment.