Persistent anonymous user session with headless D9

In short: a new php session is started upon every request made through nextjs, I somehow must keep the anonymous user session alive.

For my anonymous users I need keep track of data submitted through nextjs. I Initially developed my app with Drupal 8.9, and many months later upgraded to 9.4-dev.

Before the upgrade to 9.4-dev, I was sure the sessions worked properly across several browsers and anonymous users at the same time; I could use the app, store and re-use unique data per anonymous user etc. Sadly its hard to verify this, downgrading to 8.9 will cause additional pain..

After the upgrade, the anonymous users suddenly share the same session. This might be related to https://www.drupal.org/project/session_based_temp_store/issues/3257214 and https://www.drupal.org/node/3006306 but not sure. Using session_based_temp_store:1.1 results in a shared session and session_based_temp_store:1.2 results in a non-persistent session.

The code below used to work:

 if ($this->currentUser->isAuthenticated()) {   $user_preferences = unserialize($this->currentUser->get('field_preferences')->value);   foreach ($values as $key => $value) {     $user_preferences[$key] = $value;   }   $this->currentUser->set('field_preferences', serialize($user_preferences))->save(); } else {   $session = Drupal::service('session_based_temp_store')->get('mymodule_user');   $user_preferences = $session->get('d_user');   foreach ($values as $key => $value) {     $user_preferences[$key] = $value;   }    $session->set('d_user', $user_preferences); } 

Trying to fix the problem, I tried to use tempstore.private which resulted in the same problem (new session every request).

After this I tried to use the code below, which also resulted in the same problem:

$_SESSION['my_module']['data'] = 'My Data'; $request = $this->requestStack->getCurrentRequest(); $session = $request->getSession(); 

Even test var $_SESSION[‘my_module’][‘data’] is "reset" every time. So it doesn’t really matter what kind of storage approach I try to use; the problem is clearly a non persistent session.

All of my rest resources are called with POST requests and authenticated with oauth2. Everything works nicely for authenticated users. Anonymous users can also successfully make requests.

Searching Google I can’t really find anything useful, which makes me think i’m missing something obvious..

The main goal is: anonymous users must keep a persistent session in a headless drupal, also when the user closes the browser and comes back a week later, the same session must still be alive..

I do not want to store any user entered data in the frontend.

The problem is kind of logical I think..: how can Drupal know who is making the anonymous request? Should I have some sort of anonymous user cookie in the frontend, connected to an anonymous user session in the backend, passed with every request?

Can someone point me into the right direction please? Any help is much appreciated.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Persistent anonymous user session with headless D9

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.