Allowing anonymous user based on condition I'm currently working on a custom module with an access form and set permissions for anonymous users.

I'm trying to make it so that when an anonymous user enters the password in the access form, they are logged successfully into 'node/3.'

The trick is, 'node/3' and any other basic page node is blocked for anonymous users via the following code I placed in my .MODULE file.

use Drupal\node\NodeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
function cookie_authentication_node_access(NodeInterface $node, $op, AccountInterface $account) {
if ($account-&gt;isAnonymous() &amp;&amp; $node-&gt;gettype() == 'page') {
return AccessResult::forbidden()-&gt;cachePerPermissions();
}

return AccessResult::neutral();
}


Now, how do I get the anonymous user to access the page only for the condition that the password is entered in the access form?

Here's the code for the access form:

&lt;?php
/**
* @file
* Contains \Drupal\resume\Form\CookieForm.
*/
namespace Drupal\cookie_authentication\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class CookieForm extends FormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'cookie_authentication';
  }
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state)       {
     $form['password'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Access'),
      '#required' =&gt; TRUE,
     );

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; $this-&gt;t('Submit'),
      '#button_type' =&gt; 'primary',
     );
    return $form;
  }
  /**
   * {@inheritdoc}
   */

    public function validateForm(array &amp;$form, FormStateInterface  $form_state) {
        $values = $form_state-&gt;getValue('password');
        //$configValue =  $config-&gt;get('access');
        $configValue = \Drupal::config('cookie_authentication.settings')-&gt;get('access');
         if ($values != $configValue) {
            $form_state-&gt;setErrorByName('password', $this-&gt;t('This value is not valid!'));
        }
    }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $password = $form_state-&gt;getValue('password');
    setcookie("password", $password );
    drupal_set_message('valid code');
    $response = Url::fromUserInput('/node/3');
    $form_state-&gt;setRedirectUrl($response);

}

}


The password is loaded through a config form I built into the back-end...
You can store a value on the session if the password match and then check that value:

$_SESSION['password_match'] = TRUE;


Or even better, as suggested by @4k4 you can do it the D8 way.

CookieForm class:

$session = $this-&gt;getRequest()-&gt;getSession();
$session-&gt;set('password_match', TRUE);


hook:

$password_match = \Drupal::request()-&gt;getSession()-&gt;get('password_match');