2016-01-03 05:48:37 +05:30
|
|
|
<?php
|
2016-01-15 14:51:27 +05:30
|
|
|
namespace api\controllers;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
use api\models\LoginForm;
|
2016-01-03 05:48:37 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\filters\AccessControl;
|
2016-02-24 03:45:04 +05:30
|
|
|
use yii\helpers\ArrayHelper;
|
2016-01-03 05:48:37 +05:30
|
|
|
|
|
|
|
class AuthenticationController extends Controller {
|
|
|
|
|
|
|
|
public function behaviors() {
|
2016-02-24 03:45:04 +05:30
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'authenticator' => [
|
|
|
|
'except' => ['login'],
|
|
|
|
],
|
2016-01-03 05:48:37 +05:30
|
|
|
'access' => [
|
2016-01-21 02:44:29 +05:30
|
|
|
'class' => AccessControl::class,
|
2016-01-03 05:48:37 +05:30
|
|
|
'rules' => [
|
|
|
|
[
|
2016-01-21 02:44:29 +05:30
|
|
|
'actions' => ['login'],
|
2016-01-03 05:48:37 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['?'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs() {
|
|
|
|
return [
|
2016-01-21 02:44:29 +05:30
|
|
|
'login' => ['POST'],
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
public function actionLogin() {
|
|
|
|
$model = new LoginForm();
|
2016-01-03 05:48:37 +05:30
|
|
|
$model->load(Yii::$app->request->post());
|
2016-02-23 03:52:04 +05:30
|
|
|
if (($jwt = $model->login()) === false) {
|
2016-03-14 00:16:22 +05:30
|
|
|
$data = [
|
2016-01-03 05:48:37 +05:30
|
|
|
'success' => false,
|
2016-01-15 14:51:27 +05:30
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
2016-03-14 00:16:22 +05:30
|
|
|
|
|
|
|
if (ArrayHelper::getValue($data['errors'], 'login') === 'error.account_not_activated') {
|
|
|
|
$data['data']['email'] = $model->getAccount()->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2016-01-03 05:48:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
2016-02-23 03:52:04 +05:30
|
|
|
'jwt' => $jwt,
|
2016-01-03 05:48:37 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|