Skip to content

Commit

Permalink
Correctly hide all login-unrelated elements when login flow is displayed
Browse files Browse the repository at this point in the history
remp/crm#1019
  • Loading branch information
Jana Culenova authored and miroc committed Nov 3, 2022
1 parent ff04ec9 commit beffb8e
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 143 deletions.
29 changes: 19 additions & 10 deletions src/Forms/CheckoutFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Kdyby\Translation\Translator;
use Nette\Application\UI\Form;
use Nette\Database\Table\ActiveRow;
use Nette\Forms\Controls\TextInput;
use Nette\Http\Request;
use Nette\Security\AuthenticationException;
use Nette\Security\User;
Expand Down Expand Up @@ -202,17 +203,25 @@ public function create($cart, $cartFree = [], $payment = null)
$email->setHtmlAttribute('placeholder', '@');
$email->setRequired('products.frontend.shop.checkout.fields.email_required');

$emailUsable = function ($field, $args) {
$user = $this->usersRepository->findBy('email', $field->getValue());
$emailUsable = function (TextInput $emailField, ?string $password) {
if (strlen($password)) {
// User is trying to log in, formSucceeded will validate the credentials.
return true;
}
$user = $this->usersRepository->findBy('email', $emailField->getValue());
return !$user;
};

$password = $user->addPassword('password', 'Heslo');
$password
->addConditionOn($action, Form::EQUAL, 'login')
->addRule(Form::FILLED, 'products.frontend.shop.checkout.fields.pass_required');
$email->addConditionOn($action, Form::NOT_EQUAL, 'login')
->addRule($emailUsable, 'products.frontend.shop.checkout.fields.account_exists');
}
->addRule($emailUsable, 'products.frontend.shop.checkout.fields.account_exists', $password);

$user->addPassword('password', 'Heslo')
->addConditionOn($action, Form::EQUAL, 'login')
->addRule(Form::FILLED, 'products.frontend.shop.checkout.fields.pass_required');
$user->addSubmit('login_submit', 'products.frontend.shop.checkout.login')
->setValidationScope([$form['user']]);
}

if ($hasDelivery) {
$contact = $form->addContainer('contact');
Expand Down Expand Up @@ -373,7 +382,7 @@ public function create($cart, $cartFree = [], $payment = null)
}
}

$form->addSubmit('finish', 'products.frontend.shop.checkout.fields.login');
$form->addSubmit('finish', 'products.frontend.shop.cart.confirm_order');
$form->addProtection();

$form->setDefaults($defaults);
Expand Down Expand Up @@ -403,13 +412,13 @@ public function gatewayLabel($code)

public function formSucceeded($form, $values)
{
if ($values['action'] == 'login') {
if (isset($form['user']['login_submit']) && $form['user']['login_submit']->isSubmittedBy()) {
$this->user->setExpiration('14 days');
try {
$this->user->login(['username' => $values['user']['email'], 'password' => $values['user']['password']]);
$this->user->setAuthorizator($this->authorizator);

$cart = Json::decode($values['cart'], Json::FORCE_ARRAY);
$cart = Json::decode($this->request->getPost('cart'), Json::FORCE_ARRAY);
$products = $this->productsRepository->findByIds(array_keys($cart));
$removeProducts = [];
foreach ($products as $product) {
Expand Down
Loading

0 comments on commit beffb8e

Please sign in to comment.