Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
haqqi committed Feb 6, 2017
2 parents 90723e1 + 3b432a9 commit d8517a7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
8 changes: 8 additions & 0 deletions api/components/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace api\components;

class ErrorHandler extends \common\components\ErrorHandler
{
public $errorAction = 'site/error';
}
2 changes: 1 addition & 1 deletion api/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
],
],
'errorHandler' => [
'class' => \common\components\ErrorHandler::className(),
'class' => api\components\ErrorHandler::className(),
],
'urlManager' => [
// to improve the security
Expand Down
34 changes: 34 additions & 0 deletions api/controllers/SiteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace api\controllers;

use yii\rest\Controller;

/**
* Site controller.
*/
class SiteController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();

$behaviors['authenticator']['except'] = [
'error',
];

return $behaviors;
}

public function actionError()
{
$exception = \Yii::$app->errorHandler->exception;

if ($exception !== null) {
return [
'name' => $exception->getName(),
'status' => $exception->statusCode,
];
}
}
}
2 changes: 1 addition & 1 deletion api/forms/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public function login()
}
return false;
}
}
}
13 changes: 1 addition & 12 deletions api/modules/v1/controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ public function actionLogin()
throw new HttpException(400, $e->getMessage(), ApiCode::DEVICE_IDENTIFIER_NOT_FOUND);
}

$loginByEmail = \Yii::$app->params['loginByEmail'];

if ($loginByEmail) {
$loginForm->setScenario(LoginForm::SCENARIO_SUBMIT_LOGIN_EMAIL);
} else {
$loginForm->setScenario(LoginForm::SCENARIO_SUBMIT_LOGIN_USERNAME);
}

$loginForm->load(\Yii::$app->request->post(), 'User');

/*
Expand All @@ -117,11 +109,9 @@ public function actionLogin()
*/
$user = \Yii::$app->user->getIdentity();

$msgLoginBy = $loginByEmail ? 'email ' . $user->email : 'username ' . $user->username;

return [
'name' => 'Success',
'message' => 'Login by ' . $msgLoginBy . ' success.',
'message' => 'Login by ' . $loginForm->loginColumn . ' success.',
'code' => ApiCode::LOGIN_SUCCESS,
'status' => 200,
'data' => $user->toArray([
Expand Down Expand Up @@ -226,7 +216,6 @@ public function actionResetPassword($resetPasswordToken)

// this will catch POST request
if (\Yii::$app->request->isPost) {

$passwordForm->load(\Yii::$app->request->post(), 'User');

if ($passwordForm->validate() && $passwordForm->resetPassword()) {
Expand Down
2 changes: 0 additions & 2 deletions common/config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
'supportEmail' => 'support@example.com',
/** set password reset token expired 60 sec * 60 Min */
'user.passwordResetTokenExpire' => 3600,
/** set login params. If value is true, then should login by email. else, login by username. */
'loginByEmail' => false,
/**
* Is user must be use strong password?
* there is only 5 choices : simple | normal | fair | medium | strong
Expand Down
5 changes: 1 addition & 4 deletions common/forms/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,7 @@ protected function getUser()
$loginColumn = $this->getLoginColumn();

$this->_user = $class::findOne([$loginColumn => $this->username]);

if (!($this->_user instanceof IdentityInterface)) {
throw new InvalidConfigException('User class must implement IdentityInterface');
}

}

return $this->_user;
Expand Down

0 comments on commit d8517a7

Please sign in to comment.