Pinia Store caching state values

So, I am having issue with my Pinia store and router guard. Issue is following:
When i go trough ‘logout’ function that basically do following, everything comes in place.

logout function in auth.js pinia store
`async logout() {
this.user = null;
this.session = null;
this.token = null;
this.isEmailVerified = false;

  // Clear local storage
  localStorage.removeItem('user');
  localStorage.removeItem('session');
  localStorage.removeItem('token');
  localStorage.removeItem('isEmailVerified');

  const showStore = useShowStore();
  showStore.closeMenu()
 },`

But when I mannualy remove my session from chrome dev tools(remove session from local storage, token, user, and isEmailVerified) Pinia obviously does not update the state. But the issue is following: I try to get the state from pinia in router guard, and it still keeps previous state, it does not go and actually look in local storage and gets me updated data for session, token, etc

Here is my auth.js pinia store:

 `import { defineStore } from 'pinia';
 import { useShowStore } from "@/store/showStore";

 export const useAuthStore = defineStore({
 id: 'auth',
 state: () => ({
 user: JSON.parse(localStorage.getItem('user')) || null,
 session: JSON.parse(localStorage.getItem('session')) || null,
 token: localStorage.getItem('token') || null,
 isEmailVerified: JSON.parse(localStorage.getItem('isEmailVerified')) || false,
 }),
 getters: {
 isAuthenticated: () => !!localStorage.getItem('token') || null,
 },
 actions: {
 async login(email, password) {
 //...`

And here is router guard

`router.beforeEach((to, from, next) => {
 const authStore = useAuthStore();

 //here it prints session after removing manually
 console.log('Is Authenticated:', authStore.isAuthenticated);

 if (to.meta.requiresAuth && !authStore.isAuthenticated) {
 next('/SignIn'); // Redirect to SignIn if not authenticated
 } else if (to.meta.approachOnlyLoggedOut && authStore.isAuthenticated && authStore.isEmailVerified) {
 next('/'); // Redirect to dashboard if authenticated and email verified
 } else if (authStore.isAuthenticated && !authStore.isEmailVerified && to.path !== '/EmailVerification') {
 if (to.path == "/VerifyEmail") {
 next()
 } else {
  next('/EmailVerification'); // Redirect to EmailVerification if email is not verified
 }
 } else {
 next();
 }  
 });`

How can i get fresh state each time? I don’t want to update state trough action in router, since I will use this ‘isAuthenticated’ on many places in the app, so I don’t want to invoke another action before i get the state, I want this logic centralized.

I have tried many things, but it still keeps state as previous value. Can someone help? Thanks

New contributor

Milan Milosevic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật