Skip to content
Snippets Groups Projects
Commit 7c04ec4b authored by Christian Foidl's avatar Christian Foidl
Browse files

Issue #3422324: Convey email as query param; support existing destination query param in forms

parent 3514263d
No related branches found
No related tags found
1 merge request!5Issue #3422324: Convey email as query param; support existing destination query param in forms
Pipeline #98473 passed
......@@ -72,6 +72,8 @@ class MagicCodeEmailLoginForm extends FormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$destination = $this->getRequest()->get('destination');
$form['email'] = [
'#type' => 'textfield',
'#title' => $this->t('E-Mail'),
......@@ -111,7 +113,7 @@ class MagicCodeEmailLoginForm extends FormBase {
'#tag' => 'a',
'#attributes' => [
'class' => 'button button--secondary button--login-with-pass',
'href' => Url::fromRoute('user.login')->toString(),
'href' => Url::fromRoute('user.login', ['destination' => $destination])->toString(),
],
'#value' => 'Login with password',
];
......@@ -131,11 +133,14 @@ class MagicCodeEmailLoginForm extends FormBase {
// Send mail.
_magic_code_email_login_mail_notify('login', $form_state->get('user'));
// Set email on session.
$this->saveEmailOnSession($form_state->getValue('email'));
$destination = $this->getRequest()->get('destination');
$this->getRequest()->query->remove('destination');
// Redirect to verification form.
$form_state->setRedirect('magic_code.email_login_verify');
$form_state->setRedirect('magic_code.email_login_verify', [
'destination' => $destination,
'email' => $form_state->getValue('email'),
]);
}
/**
......
......@@ -7,12 +7,14 @@ use Drupal\Core\Flood\FloodInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BareHtmlPageRendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\magic_code\MagicCodeManagerInterface;
use Drupal\magic_code\MagicCodeResult;
use Drupal\magic_code_email_login\MagicCodeEmailLoginFloodTrait;
use Drupal\magic_code_verify_form\Form\MagicCodeVerifyFormBase;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Form to verify magic code email login.
......@@ -75,10 +77,24 @@ class MagicCodeEmailLoginVerifyForm extends MagicCodeVerifyFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$email = $this->getRequest()->get('email');
if (!$email) {
$this->messenger()->addError('E-Mail not set, please try again!');
$destination = $this->getRequest()->get('destination');
$this->getRequest()->query->remove('destination');
$url = Url::fromRoute('magic_code.email_login', [
'destination' => $destination,
]);
return new RedirectResponse($url->toString());
}
$form = parent::buildForm($form, $form_state);
$form['info']['#value'] = $this->t('Please enter the 6-digit code that was sent to <em>@email</em>:', [
'@email' => $this->getEmailFromSession(),
'@email' => $email,
]);
return $form;
......@@ -88,7 +104,7 @@ class MagicCodeEmailLoginVerifyForm extends MagicCodeVerifyFormBase {
* {@inheritdoc}
*/
public function verify(string $code, FormStateInterface $form_state): void {
$email = $this->getEmailFromSession();
$email = $this->getRequest()->get('email');
$userResult = $this->userStorage->loadByProperties([
'mail' => $email,
]);
......@@ -136,7 +152,7 @@ class MagicCodeEmailLoginVerifyForm extends MagicCodeVerifyFormBase {
* {@inheritdoc}
*/
public function resend(FormStateInterface $form_state): void {
$email = $this->getEmailFromSession();
$email = $this->getRequest()->get('email');
if (!$email) {
$this->messenger()->addError($this->t('Could not get email to resend code to.'));
return;
......@@ -151,6 +167,14 @@ class MagicCodeEmailLoginVerifyForm extends MagicCodeVerifyFormBase {
$this->messenger()->addStatus($this->t('A new code has been sent to @email!', [
'@email' => $email,
]));
$destination = $this->getRequest()->get('destination');
$this->getRequest()->query->remove('destination');
$form_state->setRedirect('magic_code.email_login_verify', [
'destination' => $destination,
'email' => $email,
]);
return;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment