<Observable<HttpResponse<Country[]>>
<Observable<Country[]>
public function actionRegistration()
{
$registrationData = Yii::createObject(RegistrationData::class);
$registrationData->load($this->getRequest()->post(), '');
$user = $this->accountService->registrationUser($registrationData);
if ($user) {
return $this->accountService->generateToken($user)->token;
} else {
throw new BadRequestHttpException(implode('; ', $registrationData->getFirstErrors()));
}
}
public function registrationUser(RegistrationData $data): ?User
{
if (!$data->validate()) {
return null;
}
$user = $this->userRepository->create($data->getAttributes());
$user->setPassword($data->password);
if ($this->userRepository->save($user)) {
return $user;
}
return null;
}
public function generateToken(User $user): UserToken
{
$userToken = $this->userTokenRepository->create([
'user_id' => $user->id,
'token' => md5(mktime() . uniqid()),
'ip' => $this->getRequest()->userIP ?? '127.0.0.1'
]);
if (!$this->userTokenRepository->save($userToken)) {
throw new Exception('Can\'t save user token');
}
return $userToken;
}