feat(mall): authenticate against the live API

Wave 2 of replacing the fixed-data mock adapter: the auth domain joins the live
list, so credentials, roles and tokens belong to the real user.

- session: validate a restored token through /auth/me instead of trusting
  localStorage, clearing it on 401/403 but keeping it when the API is merely
  unreachable; the route guard now uses the validated session
- login: report a 401 as invalid credentials rather than a generic failure, and
  drop the 6-character client rule so the API owns the password policy
- register: raise the rule to the API's 8 characters, remove the
  verification-code field (its button only counted down and the value was never
  sent), and report a duplicate email (409) distinctly
- TopBar and the user profile render their session-dependent branch client-only:
  validating the session before hydration made those localStorage-backed
  branches report hydration mismatches the previous code did not

Verified against the running backend: wrong password rejected, real JWT issued,
/user reachable, a short password refused with no network call, duplicate email
reported, a tampered token cleared and bounced to sign-in, a stale token kept
when the API is down, and the fixed-data rollback still signs in with the
backend stopped.

Also re-cuts docs/TBD-migrate-wave.md: auth is done, and cart, orders,
shipments and invoices must move together, because the mock adapter keeps their
state in one shared object and a partial flip fails at checkout.

OpenSpec change: openspec/changes/replace-mock-api-wave-2
This commit is contained in:
2026-09-17 16:13:16 +00:00
parent e0e833d0e5
commit 0ceb4a2b25
15 changed files with 280 additions and 94 deletions
+10 -31
View File
@@ -1,4 +1,6 @@
<script setup lang="ts">
import { ApiError } from "@vmall/shared";
const { t } = useI18n();
const { $api } = useNuxtApp();
const session = useSessionStore();
@@ -8,30 +10,14 @@ const displayName = ref("");
const email = ref("");
const password = ref("");
const confirmPassword = ref("");
const code = ref("");
const errorKey = ref("");
const submitting = ref(false);
const countdown = ref(0);
let timer: ReturnType<typeof setInterval> | undefined;
function startCountdown(): void {
if (countdown.value > 0) return;
countdown.value = 60;
timer = setInterval(() => {
countdown.value -= 1;
if (countdown.value <= 0 && timer) {
clearInterval(timer);
timer = undefined;
}
}, 1000);
}
onUnmounted(() => {
if (timer) clearInterval(timer);
});
/** Mirrors the auth API's rule so a short password fails here, not as a 400. */
const MIN_PASSWORD_LENGTH = 8;
function validate(): boolean {
if (!displayName.value || !email.value || !password.value || !confirmPassword.value || !code.value) {
if (!displayName.value || !email.value || !password.value || !confirmPassword.value) {
errorKey.value = "auth.validationRequired";
return false;
}
@@ -39,7 +25,7 @@ function validate(): boolean {
errorKey.value = "auth.validationEmail";
return false;
}
if (password.value.length < 6) {
if (password.value.length < MIN_PASSWORD_LENGTH) {
errorKey.value = "auth.validationPassword";
return false;
}
@@ -58,8 +44,10 @@ async function submit(): Promise<void> {
const auth = await $api.register(email.value, password.value, displayName.value);
session.setAuth(auth);
await router.push("/");
} catch {
errorKey.value = "auth.requestFailed";
} catch (error) {
errorKey.value = error instanceof ApiError && error.status === 409
? "auth.emailTaken"
: "auth.requestFailed";
} finally {
submitting.value = false;
}
@@ -91,15 +79,6 @@ async function submit(): Promise<void> {
<span>{{ t("auth.confirmPassword") }}</span>
<input v-model="confirmPassword" class="minput" type="password" autocomplete="new-password" :placeholder="t('auth.confirmPasswordPlaceholder')" />
</label>
<label class="field">
<span>{{ t("auth.smsCode") }}</span>
<span class="code-row">
<input v-model.trim="code" class="minput" type="text" :placeholder="t('auth.smsCodePlaceholder')" />
<button class="mbtn code-button" type="button" :disabled="countdown > 0" @click="startCountdown">
{{ countdown > 0 ? t("auth.codeCountdown", { n: countdown }) : t("auth.getCode") }}
</button>
</span>
</label>
<p v-if="errorKey" class="error">{{ t(errorKey) }}</p>
<button class="mbtn red block submit" type="submit" :disabled="submitting">
{{ t("auth.registerAction") }}