The Nuxt Vuex authentication store seems to be having trouble updating my getters

Despite the store containing the data, my getters are not updating reactively.

Take a look at my code below:

function initialAuthState (): AuthState {
  return {
    token: undefined,
    currentUser: undefined,
    refreshToken: undefined
  }
}

export const store: AuthState = initialAuthState()

export const actions: ActionTree<AuthState, RootState> = {
  [AuthStoreActions.LOGIN]: async ({ commit }, payload: { employeeID: string, password?: string }): Promise<User> => {
    const response = await AuthRepository.login(payload)

    // Save Token and User
    commit(AuthStoreMutations.SET_TOKENS, { token: response.data.token, refreshToken: response.data.refreshToken })
    const user = User.parseFromObject(response.data.user)
    commit(AuthStoreMutations.SET_USER, user)
    return new User()
  },
  [AuthStoreActions.LOGOUT]: ({ commit }): void => {
    commit(AuthStoreMutations.CLEAR_STORE)
  }
}

export const mutations: MutationTree<AuthState> = {
  [AuthStoreMutations.SET_TOKENS]: (state: AuthState, value: { token: string, refreshToken: string }) => {
    state.token = value.token
    state.refreshToken = value.refreshToken
  },
  [AuthStoreMutations.SET_USER]: (state: AuthState, value: User) => {
    const t = {}
    Object.assign(t, value)
    state.currentUser = t as User
  },
  [AuthStoreMutations.CLEAR_STORE]: (state: AuthState) => {
    //state.token = null
    //state.refreshToken = null
    //state.currentUser = null
  }
}

export const getters: GetterTree<AuthState, RootState> = {
  [AuthStoreGetters.TOKEN]: (state: AuthState) => state.token,
  [AuthStoreGetters.REFRESH_TOKEN]: (state: AuthState) => state.refreshToken,
  [AuthStoreGetters.CURRENT_USER]: (state: AuthState) => {
    console.log(state.currentUser)
    if (state.currentUser) {
      // needs to be parsed because VuexPersistence does not keep type information
      return User.parseFromObject(state.currentUser)
    }
    return undefined
  }
}

When I call my getter, it looks like this:

@AuthStore.Getter(AuthStoreGetters.TOKEN)
private token?: string

created () {
console.log(this.token)
}

As shown in the screenshot, the data is present. However, the only way to update my getters is by reloading the website.

Answer â„–1

After thorough investigation, we have identified the mistake. 😔

const storage: UserState = initializeUserState()

should be

const data: UserState = initializeUserState()

Hopefully, this correction will benefit others.

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

Updating the default value for react-i18next (stored in localstorage)

When utilizing a backend and language detector, an issue arises: The localstorage contains the key I18nextLng with the value en-US. How can we set the default value to be I18nextLng with the value En? init and other settings are as per the default docume ...

The ReactJS Material Table Tree mode experiences delays when new row data is introduced

My Reactjs app utilizes the material-table widget within a component: render() { return ( <div> <Row> <MaterialTable title="Mon équipement" style={{ width: "100%", margin: "0%" }} ...

Starting Web Server using File (file://) protocol

I'm currently using Quasar to develop a Vue SPA web app/page. For this project, the web app will only run by clicking on the index.html file generated by the Quasar packager. This package will not be distributed online or hosted on any domain. My mai ...

Issues with functionality of React/NextJS audio player buttons arise following implementation of a state

I am currently customizing an Audio Player component in a NextJs application using the ReactAudioPlayer package. However, the standard Import Next/Audio and using just <Audio> without props did not yield the expected results. The player functions as ...

The attribute 'forEach' is not recognized on the data type 'string | string[]'

I'm experiencing an issue with the following code snippet: @Where( ['owner', 'Manager', 'Email', 'productEmail', 'Region'], (keys: string[], values: unknown) => { const query = {}; ...

Creating a task management application using Vue 3 Composition API and Typescript with reactivity

I am in the process of creating a simple todo list application using Vue 3 Composition API and TypeScript. Initially, I set up the function for my component to utilize the ref method to manage the reactivity of user input inserted into the listItems array. ...

Using Vue Formulate to effortlessly upload multiple images

One of my projects involves using a Vue Formulate component for uploading multiple images to an Express.js backend. Here is the code snippet for the Vue Formulate component: <FormulateInput type="image" name="property_ ...

Tips for implementing react-hook-form in an Ionic Modal?

I've been experimenting with implementing react-hook-form in my Ionic React project. Here's a simple form I created: const CustomForm: React.FC<{ color: string }> = ({ color }) => { const { handleSubmit, register } = useForm(); con ...

The initial value for React input is vacant and is not capturing either the state or the prop value

After utilizing Vue for an extended period, I have now transitioned to React. To practice, I am attempting to convert some basic Vue components into React. My initial Vue code was simple as shown below: <template> <div> <h1>Hello { ...

The array containing numbers or undefined values cannot be assigned to an array containing only numbers

Currently facing an issue with TypeScript and types. I have an array of IDs obtained from checkboxes, which may also be empty. An example of values returned from the submit() function: const responseFromSubmit = { 1: { id: "1", value: "true" }, 2: ...

Using TypeScript with Angular-UI Modals

Currently, my goal is to create a modal using angular-ui-bootstrap combined with typescript. To begin, I referenced an example from this link (which originally utilizes jQuery) and proceeded to convert the jQuery code into typescript classes. After succes ...

How to submit a form in Angular2 without reloading the page

I have a straightforward form to transmit data to my component without refreshing the page. I've tried to override the submit function by using "return false," but it doesn't work as expected, and the page still refreshes. Here is the HTML: ...

Leveraging ExpressJS and NuxtJS middleware to transmit post data to a webpage

Could someone please assist me in figuring out how to transfer data from a post request to the loaded Nuxt page? I am struggling with sending the data to the page that will be displayed. My goal is to handle the POST request, and then pass that data along ...

Issue with error display in Vuelidate form validation

Recently, I integrated Vuelidate validation into a login form to enhance its functionality. While testing the form by leaving the username field empty, I expected to see a warning message saying "Name is required", but unfortunately, nothing appeared on th ...

Utilizing Vue.js global variables within a Pinia store: a step-by-step guide

I am currently working on a Vue.js 3 project where I need to utilize a global axios instance in some .vue components and also in a pinia store. Within my main.js file, I have configured axios globally like so: const axiosInstance = axios.create({ base ...

Is there a way to customize the appearance of a MUI5 Tooltip using emotion?

I went through the information in this Stack Overflow post and experimented with the styled method. The code snippet I used is as follows: import * as React from 'react'; import { styled } from '@mui/material/styles'; import Tooltip, { ...

Incorporating a helper JavaScript file to seamlessly integrate Typeform into a project built with Vue

Struggling with incorporating a typeform into my website through the use of both vue and laravel. The problem arises when trying to embed the typeform using a script, as Vue throws an error when attempting to include the script directly within the compone ...

Struggling to locate components in your React, Next.JS, and Typescript project? Storybook is having trouble finding them

I have set up Storybook with my Next.js, TypeScript, and React project. The main project renders fine, but Storybook is breaking and giving me the error message: "Module not found: Error: Can't resolve 'components/atoms' in...". It appears t ...

Error encountered in Next.js: TypeScript error with code ts(7031) - The binding element 'Component' is implicitly assigned the 'any' type

Converting my NextJS project to TypeScript presented a challenge for me. When working on my _app.tsx file, I came across a type error: 'pageProps' implicitly has an 'any' type. ts(7031). The error message likely resembled this image: ht ...

Utilizing Google Analytics 4 in a React TypeScript Environment

Currently, there are two options available: Universal Analytics and Google Analytics 4. The reasons why I lean towards using Google Analytics 4: Universal Analytics is set to be retired on July 1, 2023, so it makes sense to start fresh with Google Analyt ...