Using Middleware to Access LocaleStorage in Universal Mode

Here's the dilemma:
My understanding is that accessing LocaleStorage from Middleware in Universal mode
is currently not possible (at least not out-of-the-box.)

Question:
Is there any way to solve this issue? Are there any modules or solutions available?
(if so - what would you recommend as a first step?)

Answer №1

Storing data in local storage within Nuxt middleware is restricted to client-side only. However, utilizing cookies or accessing the Vuex store are viable alternatives:

export default (context) => {
    if (!context.app.context.app.$cookies.get('loginState')) {
        return context.redirect('/login')
    }
}

Alternatively, you can access the store in middleware:

export default function({ store, redirect }) {
  if (!store.getters.GET_LOGIN_STATE) {
    return redirect('/login')
  }
}

In order to work around this limitation, consider storing your data in either the store or a cookie.

Answer №2

If you're looking to save data on the client side that won't be deleted upon page refresh, consider using cookies. Cookies can be accessed in the middleware like so:

When utilizing Nuxt, the context contains a property called req, and the cookies are typically attached to the header.

export default function ({ req, store}) {
   let cookie = require('cookie');
   let cookies = req.headers.cookie;
   let parsedCookies = cookie.parse(cookies);
   console.log(parsedCookies);
}

In this demonstration, we are utilizing the cookie package to parse the cookies: npm i cookie

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The information in columnSummary is missing

Recently, I've been attempting to incorporate columnSummary into my table using Handsontable. Strangely enough, it appears that the function isn't being triggered. Even though the stretchH value is properly set, the table doesn't respond to ...

Ways to turn off emojis in froala editor

I successfully integrated the Froala editor and dynamically generated an ID for it based on specific requirements. Everything is working well, but I now need to disable emoticons in this editor. $('#froala'+key).froalaEditor({ imageUploadP ...

Retrieve a Play Scala variable in the $scope of an AngularJS application

After trying various methods recommended on StackOverflow, I am still struggling to retrieve a Play Scala variable within my Javascript $scope. The line of initialization in an HTML file is as follows: @(playVariable: String)(implicit request: play.api.mv ...

A guide to extracting text from HTML elements with puppeteer

This particular query has most likely been asked numerous times, but despite my extensive search, none of the solutions have proven effective in my case. Here is the Div snippet I am currently dealing with: <div class="dataTables_info" id=&qu ...

Bug with IE lookahead in Regular Expressions

Currently, I am struggling to define the regular expression needed for proper validation in my ASP.NET validator. Although it works fine in Firefox, the sample string is not validating correctly in IE using the following expression: 12{2}12{0-9}1{12,13} ...

Ways to retrieve information from a $$state object

Everytime I try to access $scope.packgs, it shows as a $$state object instead of the array of objects that I'm expecting. When I console log the response, it displays the correct data. What am I doing wrong? This is my controller: routerApp.controll ...

Unable to hide the mobile menu button

I am currently working on a fun website project . I am facing an issue with the mobile menu button not disappearing using display:none in Safari on my iPhone when in landscape mode, even though it works fine in Chrome. My goal is to make the #menu-button ...

Error: The requested collection# cannot be found in the Node.js Express routing system

Recently, I have started learning nodejs and implemented a simple API that allows users to log in with passport and then redirects them to the /collections route. While this part is functioning correctly, I am encountering issues with POST requests which a ...

Utilizing JavaScript to iterate through objects retrieved via Ajax calls

Recently, I've been diving into the world of Javascript and delving deep into AJAX. Utilizing Vanilla JS along with simple AJAX, my current task involves fetching a user object from a URL based on the user's input ID. Despite attempting to use .d ...

Replicate form element

I am having trouble duplicating form items Greetings to all. I have a form and I'm looking to add a button that allows users to duplicate fields each time they click on it. Here is my form: <v-layout v-for="(phone, index) in people.phones" :key=" ...

What is the method for shifting the expansion panel icon position to the left?

I need the expansion arrow in my app to be on the left side of the panel. However, it currently appears on the right side by default. This is what I have tried: <ExpansionPanelSummary className={classes.panelSummary} expandIcon={<ExpandMore ...

Error: JSON parsing failed due to an unexpected character, resulting in null data

Many people have asked similar questions on the same topic, but I have not been able to find a solution for my specific problem. Below is the code snippet that I am working with: $.post("show_search_results.php", {location_name: ""+location_name+"", key: ...

The latest update to the Server Stats code mistakenly changes the channel to "undefined" instead of displaying the total number

I've been working on a private bot for a specific server that displays server statistics and more. However, I've encountered an issue where every time a user joins or leaves the guild, the bot updates a channel with 'undefined' instead ...

Implementing user-driven filtering in a React table

When a user clicks on the submit button, data will be loaded. If no filter is applied, all data will be displayed. const submit = async (e: SyntheticEvent) => { e.preventDefault(); const param = { ...(certificateNo && ...

Using the power of AJAX, retrieve data from a PHP script and showcase the response within

Within my HTML file, I have implemented a feature that allows users to input a value. To validate this input, I created a PHP script that checks if the entered value exists in the database. The script returns the following JSON response: {"active":true} ...

Mastering the Art of Scrolling

Can someone please tell me the name of this specific scrolling technique? I am interested in using something similar for my project. Check out this example site ...

`No success in opening .vue files in Eclipse's Web Page Editor`

When trying to open .vue files as regular HTML files in Eclipse, I attempted to right click on a .vue file and choose Open WithWeb Page Editor. Additionally, I set .vue to be associated with the Web Page Editor in the preferences. Following these steps ex ...

Problem with Material-UI Drawer

Is there a way to make this drawer stay fixed on the page like a sticker and remain active without moving when scrolling? I've tried using docked={false}, but it makes the whole page inactive except for the drawer. Any suggestions on how to solve this ...

There seems to be an error with cheeriojs regarding the initialization of exports.load

I am currently using cheeriojs for web scraping, but I am encountering an issue after loading the body into cheerio. Although the body appears to be well-formatted HTML code, I am receiving errors such as exports.load.initialize. This is preventing me fr ...

Guide to fetching and displaying a PDF file in the browser using Vue.js and Axios

Despite successfully using Axios to download a pdf file on the browser with correct page numbers and orientation, the content appears to be empty. Here is the API code snippet: [HttpGet] [Route("~/api/Document")] public HttpResponseMessage Get(in ...