guild/frontend/modules/api/controllers/UserController.php

79 lines
2.2 KiB
PHP
Raw Normal View History

2021-07-28 18:15:38 +03:00
<?php
namespace frontend\modules\api\controllers;
2021-08-02 18:37:12 +03:00
use common\behaviors\GsCors;
use common\classes\Debug;
2021-07-28 18:15:38 +03:00
use common\models\User;
use frontend\modules\api\models\LoginForm;
use Yii;
2021-08-05 18:52:07 +03:00
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
2021-07-28 18:15:38 +03:00
use yii\filters\ContentNegotiator;
use yii\rest\ActiveController;
use yii\helpers\ArrayHelper;
use yii\filters\auth\QueryParamAuth;
use yii\web\BadRequestHttpException;
use yii\web\Response;
class UserController extends ActiveController
{
public $modelClass = User::class;
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), [
[
'class' => ContentNegotiator::class,
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
2021-08-09 12:12:15 +03:00
// 'authenticator' => [
// 'class' => CompositeAuth::class,
// 'authMethods' => [
// HttpBearerAuth::class,
// ],
// ],
2021-11-29 18:21:50 +03:00
'corsFilter' => [
'class' => GsCors::class,
'cors' => [
'Origin' => ['*'],
//'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Headers' => [
'Content-Type',
'Access-Control-Allow-Headers',
'Authorization',
'X-Requested-With'
],
]
2022-03-03 17:24:45 +03:00
],
2021-07-28 18:15:38 +03:00
]);
}
public function actions()
{
$action = parent::actions(); // TODO: Change the autogenerated stub
unset($action['index']);
unset($action['create']);
unset($action['update']);
unset($action['delete']);
}
public function actionLogin()
{
$model = new LoginForm();
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '') && $model->login()) {
return [
2022-01-07 15:14:46 +03:00
'access_token' => $model->login(),
'access_token_expired_at' => $model->getUser()->getTokenExpiredAt(),
'id' => $model->getUser()->id,
2021-07-28 18:15:38 +03:00
];
} else {
throw new BadRequestHttpException(json_encode($model->errors));
}
}
}