Lost Vuex struggles when it is manually navigating a route in Vue-router

Exclusively on Safari browser, I encounter an issue when manually entering the URL in the navigation bar after logging in. For example, if I type "http://x.x.x.x:8080/gestione", the browser loses the vuex store state (specifically the gest_user module with the currentUser) and redirects to the login component. I am using Vuex-persistedstate:

Routes:

const routes = [
  {
    id: "login",
    path: "/login",
    name: "login",
    component: Login,
    meta: {
      ...meta.default
    }
  },
  {
    id: "home",
    path: "/",
    name: "homeriluser",
    component: HomeUser,
    meta: {
      ...meta.public, authorize: [Role.user, Role.admin]
    },
  },
  {
    id: "gestione",
    path: "/gestione",
    name: "gestrilevazioni",
    component: GestRils,
    meta: {
      ...meta.admin, authorize: [Role.admin]
    }
  },
  {
    path: "/modifica",
    name: "modificaril",
    component: EditRil,
    meta: {
      ...meta.public, authorize: [Role.user, Role.admin]
    }
  },
  {
    path: "*",
    name: "page404",
    component: Pagenotfound,
    meta: {
      ...meta.public
    }
  }
];

My router:

import Vue from 'vue';
import VueRouter from 'vue-router';
import routes from "./router";
import store from "@/store/index";

Vue.use(VueRouter);

const router = new VueRouter({
  routes,
  mode: "history"
});

router.beforeEach(async (to, from, next) => {
  
  // Redirect to login page if not logged in and trying to access a restricted page
  const { auth } = to.meta;
  const { authorize } = to.meta;
  const currentUser = store.state.gest_user;
  
  if (auth) {
    if (!currentUser.username) {
      // Not logged in, so redirect to login page with the return URL
      return next({ path: '/login', query: { returnUrl: to.path } });
    }
    
    // Check if route is restricted by role
    if (authorize && authorize.length && !authorize.includes(currentUser.roles)) {
      // Role not authorized, so redirect to the home page
      return next({ path: '/' });
    }
  }
  next();
});

export default router;

My store:

import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex);
Vue.prototype.$http = axios;
Vue.prototype.axios = axios;

const debug = process.env.NODE_ENV !== "production";

const customPersist = createPersistedState({
  paths: ["gest_user", "gest_rilevazione.rilevazione"],
  storage: {
    getItem: key => sessionStorage.getItem(key),
    setItem: (key, value) => {
      sessionStorage.setItem(key, value)
    },
    removeItem: key => sessionStorage.removeItem(key)
  }
});

const store = new Vuex.Store({
  modules: {
    pubblico: pubblico,
    amministrazione: amministrazione,
    loading_console: loading_console,
    login_console: login_console,
    gest_itemconc: gest_itemconc,
    gest_rilslave: gest_rilslave,
    gest_rilmaster: gest_rilmaster,
    sidebar_console: sidebar_console,
    gest_user: gest_user,
    gest_newril: gest_newril,
    gest_rilevazione: gest_rilevazione
  },
  plugins: [customPersist],
  strict: debug
});

export default store;

Could someone explain why this issue is occurring?

Answer №1

It seems like you are utilizing the browser's local storage feature to store data. Can you please verify if local storage is activated in your Safari browser settings?

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

What are the best practices for managing user forms and uploading files?

How should data in text fields and file upload fields be handled according to best practices? This question is a more generalized version of one I previously asked, which can be found here. Let's consider the scenario of a user registering an accoun ...

Discovering a website's console content using the "web-request" npm package

I recently added the web-request NPM package to my project with the goal of retrieving console messages from a specific website. However, I encountered an issue as I was unsure of how to achieve this. var result = await WebRequest.get('http://disco ...

Various JSON Tag values occurring more than once

Upon receiving a JSON Object in HTML, I am passing it to a JavaScript function defined below. The issue arises when the same folderName appears repeatedly on the HTML page. JSON Object: { "folderName": "arjunram-test", "objects": [ { ...

Ways to integrate a component within a custom component in the React framework

I am looking to dynamically change the body content, how can I achieve this? import React from 'react' // import { Link } from 'react-router-dom' export default function ProjectTemplate(props) { const Css= { "--backgr ...

What is the proper way to place a newly added element using absolute positioning?

Currently, I am in the process of developing a tooltip feature. This function involves adding a div with tooltip text inside it to the element that is clicked. However, I am facing a challenge in positioning this element above the clicked element every tim ...

What is the best way to show the user profile on a Forum?

I am struggling to figure out how to display the username of a user on my forum page. I currently only have access to the user's ID and need help in extracting their name instead. It seems that I lack knowledge about mongoose and could really benefit ...

Struggling with customizing the style of react mui-datatables version 4

Currently, I am utilizing "mui-datatables": "^4.2.2", "@mui/material": "^5.6.1", and have attempted to customize the styling in the following manner: Refer to the Customize Styling official documentation for more details // CUSTOMIZING MUI DATATABLES imp ...

Utilizing @casl/vue in conjunction with pinia: A guide to integrating these

I'm currently facing an issue with integrating @casl/ability and Vue 3 with Pinia. I'm unsure of how to make it work seamlessly. Here is a snippet from my app.js: import { createApp } from "vue" const app = createApp({}) // pinetree i ...

Guide on linking navigation to various buttons on the Angular menu

I am looking to enhance the functionality of my left menu buttons by adding a navigation path to each one (excluding the main menu). The menu items' names are received as @Input. I have set up a dictionary mapping all the items' names to their r ...

How can we add a class to a radio button using jQuery when it is checked, and remove the class when it

I'm looking for help with using jQuery to toggle between two radio buttons and display different divs based on the selection. jQuery(document).ready(function($) { if ($('#user_personal').is(':checked')) { $('.user_co ...

When downloading text using Angular, the file may not display new line characters correctly when opened in Notepad

When downloading text data as a .txt file in my Angular application using JavaScript code, I encountered an issue. Below is the code snippet: function download_text_as_file(data: string) { var element = document.createElement('a') eleme ...

Menu options are unresponsive to clicks in the dropdown

Need help fixing my dropdown menu issue. Whenever I hover over the dropdown, it disappears before I can click on any items. Here is a snippet of the code causing the problem: #navContainer { margin: 0; padding: 0; padding-top: 17px; width: 220 ...

Incorporating an offset with the I18nPluralPipe

Having trouble with my multiselect dropdown and the text pluralization. I attempted to use the I18nPluralPipe, but can't seem to set an offset of 1. ListItem = [Lion, Tiger, Cat, Fox] Select 1 Item(Tiger) = "Tiger", Select 3 Item(Tiger, Cat, Fox) = ...

What is the best way to implement a multi-row form in Vue.js?

Form Structure: <card-wrapper v-for="(passenger, index) in form.passenger" :key="index" :icon="['fas', 'user']" :title="`Passenger ${index + 1}`" class="mb-5" > <validation-observer ...

Tips for steering clear of getting caught in the initial focus trap

I have implemented Focus-trap (https://www.npmjs.com/package/focus-trap) in my modal to enhance keyboard accessibility. Here is a snippet of my code: <FocusTrap focusTrapOptions={{onDeactivate: onClose}} > <div> ... </div> <Focu ...

Ember - connecting to a JSON data source

Recently, I have been following a tutorial/example from codeschool and everything is going well. However, the example code includes the line: App.ApplicationAdapter = DS.FixtureAdapter.extend(); Now, I want to maintain all the existing functionality but ...

When an item in the accordion is clicked, the modal's left side scroll bar automatically scrolls to the top. Is there a solution to prevent this behavior and

When I first load the page and click on the Sales accordion, then proceed to click on Total reported and forecasted sales, the scrollbar jumps back up to the top The marked ng-container is specifically for the UI of Total reported and forecasted sales He ...

Is it feasible to establish a direct link between an Angular.js application and Dynamodb? Are there any other solutions available to eliminate the need for a backend

Personally, I believe that removing the backend could be a successful strategy for addressing sysadmin/scale issues. Do you agree with this approach? Is there a proven method for eliminating the backend in web applications that require database access? I& ...

What steps are required to transform a TypeScript class with decorators into a proper Vue component?

When I inquire about the inner workings of vue-class-component, it seems that my question is often deemed too broad. Despite examining the source code, I still struggle to grasp its functionality and feel the need to simplify my understanding. Consider th ...

The Tab component's onClick event is nonfunctional

I am currently utilizing the Tab feature from the material-ui library in my React project. As I try to come up with a workaround for an issue I am facing, I notice that my onClick event listener is not being triggered. An example of one of the tabs: < ...