Files
hedena_gold/apps/user-app/src/stores/session.ts
T

59 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-09-09 06:46:27 +03:30
import { defineStore } from 'pinia';
const TOKEN_KEY = 'gold-bank:token';
const CUSTOMER_ID_KEY = 'gold-bank:customerId';
const MOBILE_KEY = 'gold-bank:mobile';
/** Legacy's isUserLoggedIn() (Mixins/checkAccess.ts) reads this key. */
const LEGACY_FLAG_KEY = 'appToken';
2026-09-09 06:46:27 +03:30
/**
* The logged-in customer's session.
*
* `token` is the credential. It is a real customer JWT issued by
* POST /api/customer/auth/verify-otp after the SMS code is checked server-side,
* and it is what every /customer/me* request is authorised with.
*
* `customerId` is kept for display and for cart bookkeeping only — it is NOT an
* identity claim any more. Until the OTP migration, the app sent this id in the
* URL of /customers/:id and the server trusted it, which meant anyone could
* read anyone's profile, orders and ledger by changing the value. The API no
* longer has an id-bearing customer route at all; the server derives the
* customer from the token. See SEC-08 in docs/parity/ISSUES.md.
2026-09-09 06:46:27 +03:30
*/
export const useSessionStore = defineStore('session', {
state: () => ({
token: localStorage.getItem(TOKEN_KEY) as string | null,
customerId: localStorage.getItem(CUSTOMER_ID_KEY) as string | null,
mobile: localStorage.getItem(MOBILE_KEY) as string | null,
2026-09-09 06:46:27 +03:30
}),
getters: {
/** Authenticated means holding a token, not merely knowing an id. */
isLoggedIn: (state) => Boolean(state.token),
},
2026-09-09 06:46:27 +03:30
actions: {
login(token: string, customerId: string, mobile: string) {
this.token = token;
2026-09-09 06:46:27 +03:30
this.customerId = customerId;
this.mobile = mobile;
localStorage.setItem(TOKEN_KEY, token);
localStorage.setItem(CUSTOMER_ID_KEY, customerId);
localStorage.setItem(MOBILE_KEY, mobile);
localStorage.setItem(LEGACY_FLAG_KEY, token);
2026-09-09 06:46:27 +03:30
},
/**
* Clears everything. Called on explicit logout and by the API client when a
* request comes back 401 — an expired or revoked token must not leave stale
* personal data on screen.
*/
2026-09-09 06:46:27 +03:30
logout() {
this.token = null;
2026-09-09 06:46:27 +03:30
this.customerId = null;
this.mobile = null;
localStorage.removeItem(TOKEN_KEY);
localStorage.removeItem(CUSTOMER_ID_KEY);
localStorage.removeItem(MOBILE_KEY);
localStorage.removeItem(LEGACY_FLAG_KEY);
2026-09-09 06:46:27 +03:30
},
},
});