You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

7 years ago
<?php
7 years ago
namespace frontend\controllers\auth;
use core\services\auth\SignupService;
3 years ago
use DomainException;
7 years ago
use Yii;
3 years ago
use yii\base\Exception;
7 years ago
use yii\filters\AccessControl;
use core\forms\auth\SignupForm;
use yii\web\Controller;
3 years ago
use yii\web\Response;
7 years ago
class SignupController extends Controller
7 years ago
{
public $layout = 'auth';
3 years ago
private SignupService $service;
7 years ago
public function __construct($id, $module, SignupService $service, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
7 years ago
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
7 years ago
'rules' => [
[
'actions' => ['request', 'confirm'],
'allow' => true,
'roles' => ['?'],
7 years ago
],
],
],
];
}
/**
3 years ago
* @return string|Response
* @throws Exception
* @throws \yii\db\Exception
7 years ago
*/
3 years ago
public function actionRequest(): string|Response
7 years ago
{
$form = new SignupForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->service->signup($form);
7 years ago
Yii::$app->session->setFlash('success', Yii::t('auth', 'Check your email for further instructions.'));
7 years ago
//return $this->goHome();
return $this->redirect(['auth/auth/login']);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('request', [
'model' => $form,
]);
}
/**
* @param $token
*
3 years ago
* @return Response
7 years ago
*/
3 years ago
public function actionConfirm($token): Response
7 years ago
{
try {
3 years ago
$this->service->confirm($token);
7 years ago
Yii::$app->session->setFlash('success', Yii::t('auth', 'Your email is confirmed.'));
7 years ago
return $this->redirect(['auth/auth/login']);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
7 years ago
return $this->goHome();
}
}