/** * Api Login * @return array */ public function login() { $rules = ['email' => 'required_without:username|email', 'username' => 'required_without:email', 'password' => 'required']; $validate = Hyfn::validate($rules); if ($validate !== true) { return Api::error($validate->errors()->getMessages()); } $input = array('email' => Input::get('email'), 'username' => Input::get('username'), 'password' => Input::get('password')); $user = new User(); $login = $user->login($input); if ($login !== true) { return Api::error($login); } $token = $user->token; $user = User::getFromToken($token); $user = $user->toArray(); $user['token'] = $token; return Api::response($user); }
<?php require "include/connect.php"; // Соединение с БД /** @section Бизнес-логика */ /** @section Обработка запросов */ $response = array(); $method = strtolower(_::str('method')); switch ($method) { /** @subsection Обработка запросов к Api */ /** @subsection Обработка ошибочного запроса */ default: Api::error(0, $method . ' : Неверный запрос к Api'); // Все прочие запросы игнорируются } Api::out($response);
} }); /** * Authenticate valid auth token key */ Route::filter('auth.token', function () { // Validate api key $rules = ['token' => 'required']; $validate = Hyfn::validate($rules); // Invalid API key if ($validate !== true) { return Api::error($validate->errors()->getMessages(), 401); } $validToken = User::isValidToken(Input::get('token')); if ($validToken !== true) { return Api::error(Lang::get('errors.invalid_token'), 401); } }); Route::filter('auth.basic', function () { return Auth::basic(); }); /* |-------------------------------------------------------------------------- | Guest Filter |-------------------------------------------------------------------------- | | The "guest" filter is the counterpart of the authentication filters as | it simply checks that the current user is not logged in. A redirect | response will be issued if they are, which you may freely change. | */
return Api::error(Lang::get('errors.404'), 404); } return Response::view('errors.404', array(), 404); }); /* |-------------------------------------------------------------------------- | Maintenance Mode Handler |-------------------------------------------------------------------------- | | The "down" Artisan command gives you the ability to put an application | into maintenance mode. Here, you will define what is displayed back | to the user if maintenace mode is in effect for this application. | */ App::down(function () { if (Request::is('api/*')) { return Api::error(Lang::get('errors.503'), 503); } return Response::view('errors.maint', array(), 503); }); /* |-------------------------------------------------------------------------- | Require The Filters File |-------------------------------------------------------------------------- | | Next we will load the filters file for the application. This gives us | a nice separate location to store our route and application filter | definitions instead of putting them all in the main routes file. | */ require app_path() . '/filters.php';