0

in App.vue I get user data (if he is authorized)

onMounted(  async () => {
   await init();
});

added the initialization function to the useAuthComposables file

import { authService } from '@/shared/services/v1/auth.service';
import { useAuthStore } from '@/store/auth';

export default function () {
  const authStore = useAuthStore();

  const init = async () => {
    const [error, data] = await authService.getAuthUser();

    if (error) {
      console.log(error);
    } else {
      authStore.setUser(data);
      authStore.setToken(true);
    }
  };

  return {
    init
  };
}

and the store itself useAuthStore

import { defineStore } from 'pinia';

export const useAuthStore = defineStore('authStore', {
  state: () => ({
    user: null,
    isLoggedIn: false
  }),
  actions: {
    setUser(data: any) {
      this.user = data;
    },
    setToken(isAuth: boolean) {
      this.isLoggedIn = isAuth;
    }
  },
  getters: {
    getUser(state: any) {
      return state.user;
    }
  }
});

when I reload the page (f5) in the route does not see the side

router.beforeEach(async (to, from, next): Promise<any> => {
  const authStore =  useAuthStore();

  console.log('beforeEach',authStore.getUser);

  if (!authStore.isLoggedIn) {
    next({ name: 'Login' });
  }
  else {
    next();
  }
});

but when I just follow the links, it displays a stor

Aslero
  • 198
  • 1
  • 11
  • When you refresh a page, you are loading a new page - so, the store data is no longer - use some form of browser storage (localStorage, cookies, etc) - I think there's pinia plugins for that – Jaromanda X Oct 11 '22 at 04:30
  • Yep, as told above: you should look into persistency. Give a read to that one: https://stackoverflow.com/a/66872372/8816585 – kissu Oct 11 '22 at 08:37
  • i load user data in app.vue – Aslero Oct 11 '22 at 09:39
  • It's not relevant as of where you load it if you don't persist it. Maybe you do, but we don't know what your composable is doing here. – kissu Oct 11 '22 at 09:43
  • when updating in the app, I get a request for a back and saves it in the store – Aslero Oct 11 '22 at 14:55
  • Maybe you missed the 2nd sentence of the link that I've sent you. But pinia is not persisted by default. Doesn't matter if it's there or not, you need extra work on top of that. – kissu Oct 11 '22 at 14:57

0 Answers0