/** * 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); }
/** * Log the user in */ public function do_login() { $rules = ['username' => 'required', 'password' => 'required']; $validate = Hyfn::validate($rules); if ($validate !== true) { $user = new User(); return Redirect::to('login')->withInput(Input::except('password'))->withErrors($validate->errors()); } $input = array('email' => Input::get('email'), 'username' => Input::get('username'), 'password' => Input::get('password'), 'remember' => Input::get('remember')); // If you wish to only allow login from confirmed users, call logAttempt // with the second parameter as true. // logAttempt will check if the 'email' perhaps is the username. if (Confide::logAttempt($input)) { // If the session 'loginRedirect' is set, then redirect // to that route. Otherwise redirect to '/' $r = Session::get('loginRedirect'); if (!empty($r)) { Session::forget('loginRedirect'); return Redirect::to($r); } return Redirect::to('/'); // change it to '/admin', '/dashboard' or something } else { $user = new User(); // Check if there was too many login attempts if (Confide::isThrottled($input)) { $err_msg = Lang::get('confide::confide.alerts.too_many_attempts'); } elseif ($user->checkUserExists($input) and !$user->isConfirmed($input)) { $err_msg = Lang::get('confide::confide.alerts.not_confirmed'); } else { $err_msg = Lang::get('confide::confide.alerts.wrong_credentials'); } $user->validationErrors->add('login_error', $err_msg); return Redirect::to('admin/login')->withInput(Input::except('password'))->with('error', true)->withErrors($user->errors()); } }
Route::filter('auth.api', function () { // Validate api key $rules = ['api_key' => 'required|api_key']; $validate = Hyfn::validate($rules); // Invalid API key if ($validate !== true) { return Api::error($validate->errors()->getMessages(), 401); } }); /** * 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