Where to store important information needed for the UI in Vue? (Name etc)

I am writing an application using a C# backend and a Vue frontend, and I am a bit sceptical about where is best to store information about the user that is currently logged in.

There are a few posts on here about this, but none of them really have a consensus, everyone is saying different things and I’m not quite sure which is actually recommended, or which people just due and forget about the potential security issues that could arise because of it.

For context, I am talking about stuff like the users name, which I want to show in the header on all pages. My current flow is that user logs in > send request to C# backend > backend returns a JWT which utilises a Pinia store and also stores the JWT as a cookie.

There are a few people on here who are saying that its perfectly acceptable to store names etc in a JWT, but that doesn’t sound like a good idea to me? I thought about potentially just using the store, but that would be problematic for when refreshes happen and when the browser is closed as the user would be logged out.

What would be the recommended way to go about this? Just to return a JWT with a user id or something, and then send that to the API on every single page load? That seems a bit excessive and an additional overhead that isn’t needed? I also thought about potentially just setting a session id in the cookie instead of a JWT and sending an API response every time the page loads (which would allow me greater control over invalidation etc, but also comes at the expense of additional backend requests.

3

There seem to be two questions here.

  1. How to store UI information that’s required for display, For example Username, User profile picture, other User preferences that might need to be displayed on every Page in a Header of some kind.
  2. How to correctly use a JWT returned by the backend.

1. You can persist the local state in localStorage or sessionStorage

This is where I’d recommend storing the UI Information you need. This should mostly be things like ‘Username is Jane Doe’, ‘User has this avatar’ or ‘User prefers website in Darkmode’. You’ll need this on every single page to show it in headers and co, but it’s not extremely sensitive. You should GET this information once on page load and then save it in the store. From there you can persist it to sessionStorage or localStorage, using for example the ‘pinia-plugin-persistedstate’ Plugin for Pinia or just plain javascript.

Of course, sometimes being logged out when you close the browser is the right move, depending on how sensitive your application is.

2. You should send the JWT on any request to an endpoint that requires auth

You wrote that

then send that [JWT] to the API on every single page load? That seems a bit excessive and an additional overhead that isn’t needed?

Every endpoint that’s not open to the public needs the JWT (or some other authorisation mechanism) to verify you’re allowed to access it. That’s not excessive, that’s the base idea behind the technology and ensures you’re stateless. Now, you could go stateful in theory, but REST for example assumes you’re stateless, and it makes scaling a lot easier to be stateless so I’d strongly advice for it.

Sidenote, remember that JWT is not encrypted and should only be sent over a secure connection.

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