registration and get user info

This commit is contained in:
Kavalar 2023-10-11 23:19:30 +03:00
parent 75aae8e4df
commit 003c671156
4 changed files with 76 additions and 27 deletions

View File

@ -68,6 +68,14 @@ class ProfileService
return $searchModel->byParams(); return $searchModel->byParams();
} }
public static function getProfileById($id): ?array
{
$searchModel = new ProfileSearchForm();
$searchModel->id = $id;
return $searchModel->byId();
}
/** /**
* @throws ServerErrorHttpException * @throws ServerErrorHttpException
*/ */

View File

@ -21,7 +21,7 @@ class RegisterController extends ApiController
* @OA\Post(path="/register/sign-up", * @OA\Post(path="/register/sign-up",
* summary="Регистрация", * summary="Регистрация",
* description="Метод для регистрации", * description="Метод для регистрации",
* * tags={"Registration"},
* @OA\RequestBody( * @OA\RequestBody(
* @OA\MediaType( * @OA\MediaType(
* mediaType="multipart/form-data", * mediaType="multipart/form-data",

View File

@ -4,6 +4,7 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\behaviors\GsCors; use common\behaviors\GsCors;
use common\classes\Debug;
use common\models\User; use common\models\User;
use frontend\modules\api\models\LoginForm; use frontend\modules\api\models\LoginForm;
use Yii; use Yii;
@ -13,35 +14,35 @@ use yii\rest\ActiveController;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\Response; use yii\web\Response;
class UserController extends ActiveController class UserController extends ApiController
{ {
public $modelClass = User::class; public $modelClass = User::class;
public function behaviors() // public function behaviors()
{ // {
return ArrayHelper::merge(parent::behaviors(), [ // return ArrayHelper::merge(parent::behaviors(), [
[ // [
'class' => ContentNegotiator::class, // 'class' => ContentNegotiator::class,
'formats' => [ // 'formats' => [
'application/json' => Response::FORMAT_JSON, // 'application/json' => Response::FORMAT_JSON,
], // ],
], // ],
'corsFilter' => [ // 'corsFilter' => [
'class' => GsCors::class, // 'class' => GsCors::class,
'cors' => [ // 'cors' => [
'Origin' => ['*'], // 'Origin' => ['*'],
//'Access-Control-Allow-Credentials' => true, // //'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Headers' => [ // 'Access-Control-Allow-Headers' => [
'Access-Control-Allow-Origin', // 'Access-Control-Allow-Origin',
'Content-Type', // 'Content-Type',
'Access-Control-Allow-Headers', // 'Access-Control-Allow-Headers',
'Authorization', // 'Authorization',
'X-Requested-With' // 'X-Requested-With'
], // ],
] // ]
], // ],
]); // ]);
} // }
public function actions() public function actions()
{ {
@ -75,4 +76,18 @@ class UserController extends ActiveController
throw new BadRequestHttpException(json_encode($model->errors)); throw new BadRequestHttpException(json_encode($model->errors));
} }
} }
/**
* @return \frontend\modules\api\models\User
* @throws BadRequestHttpException
*/
public function actionMe(): \frontend\modules\api\models\User
{
$user = \frontend\modules\api\models\User::findOne(Yii::$app->user->id);
if (!$user){
throw new BadRequestHttpException("User not found");
}
return $user;
}
} }

View File

@ -0,0 +1,26 @@
<?php
namespace frontend\modules\api\models;
use backend\modules\card\models\UserCardSearch;
use common\services\ProfileService;
class User extends \common\models\User
{
/**
* @return string[]
*/
public function fields(): array
{
return [
'email',
'username',
'userCard' => function () {
$userCard = new UserCardSearch();
return ProfileService::getProfileById($this->userCard->id);
}
];
}
}