Files
vmall/apps/mall/pages/register.vue
T
james 0ceb4a2b25 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
2026-09-17 16:13:16 +00:00

202 lines
5.0 KiB
Vue

<script setup lang="ts">
import { ApiError } from "@vmall/shared";
const { t } = useI18n();
const { $api } = useNuxtApp();
const session = useSessionStore();
const router = useRouter();
const displayName = ref("");
const email = ref("");
const password = ref("");
const confirmPassword = ref("");
const errorKey = ref("");
const submitting = ref(false);
/** 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) {
errorKey.value = "auth.validationRequired";
return false;
}
if (!/^\S+@\S+\.\S+$/.test(email.value)) {
errorKey.value = "auth.validationEmail";
return false;
}
if (password.value.length < MIN_PASSWORD_LENGTH) {
errorKey.value = "auth.validationPassword";
return false;
}
if (password.value !== confirmPassword.value) {
errorKey.value = "auth.validationMismatch";
return false;
}
return true;
}
async function submit(): Promise<void> {
errorKey.value = "";
if (!validate()) return;
submitting.value = true;
try {
const auth = await $api.register(email.value, password.value, displayName.value);
session.setAuth(auth);
await router.push("/");
} catch (error) {
errorKey.value = error instanceof ApiError && error.status === 409
? "auth.emailTaken"
: "auth.requestFailed";
} finally {
submitting.value = false;
}
}
</script>
<template>
<section class="auth-page">
<div class="auth-inner">
<div class="auth-panel">
<div class="auth-tabs">
<h1>{{ t("auth.registerTab") }}</h1>
<NuxtLink to="/login">{{ t("auth.loginTab") }}</NuxtLink>
</div>
<form @submit.prevent="submit">
<label class="field">
<span>{{ t("common.displayName") }}</span>
<input v-model.trim="displayName" class="minput" type="text" autocomplete="name" :placeholder="t('auth.displayNamePlaceholder')" />
</label>
<label class="field">
<span>{{ t("common.email") }}</span>
<input v-model.trim="email" class="minput" type="email" autocomplete="email" :placeholder="t('auth.emailPlaceholder')" />
</label>
<label class="field">
<span>{{ t("common.password") }}</span>
<input v-model="password" class="minput" type="password" autocomplete="new-password" :placeholder="t('auth.passwordPlaceholder')" />
</label>
<label class="field">
<span>{{ t("auth.confirmPassword") }}</span>
<input v-model="confirmPassword" class="minput" type="password" autocomplete="new-password" :placeholder="t('auth.confirmPasswordPlaceholder')" />
</label>
<p v-if="errorKey" class="error">{{ t(errorKey) }}</p>
<button class="mbtn red block submit" type="submit" :disabled="submitting">
{{ t("auth.registerAction") }}
</button>
</form>
<div class="auth-links">
<span>{{ t("auth.haveAccount") }}</span>
<NuxtLink to="/login">{{ t("auth.loginNow") }}</NuxtLink>
<NuxtLink to="/forgot-password">{{ t("auth.forgotPassword") }}</NuxtLink>
</div>
</div>
</div>
</section>
</template>
<style scoped>
.auth-page {
min-height: 720px;
background: #f6f6f6 url("/mock/login-bg.svg") center top / cover no-repeat;
padding: 50px 0;
box-sizing: border-box;
}
.auth-inner {
width: 1200px;
min-height: 540px;
margin: 0 auto;
display: flex;
justify-content: flex-end;
align-items: flex-start;
}
.auth-panel {
width: 400px;
min-height: 540px;
box-sizing: border-box;
padding: 40px;
background: #fff;
box-shadow: 0 4px 22px rgba(0, 0, 0, 0.08);
}
.auth-tabs {
display: flex;
align-items: baseline;
gap: 18px;
margin-bottom: 24px;
border-bottom: 1px solid var(--mall-line);
padding-bottom: 14px;
}
h1 {
margin: 0;
color: var(--mall-ink);
font-size: 20px;
font-weight: 600;
}
.auth-tabs a,
.auth-links a {
color: var(--mall-red);
font-size: 13px;
text-decoration: none;
}
.auth-tabs a:hover,
.auth-links a:hover {
color: var(--mall-red-dark);
}
.field {
display: block;
margin-bottom: 13px;
}
.field > span:first-child {
display: block;
margin-bottom: 6px;
color: var(--mall-muted);
font-size: 13px;
}
.code-row {
display: flex;
gap: 8px;
}
.code-row .minput {
min-width: 0;
flex: 1;
}
.code-button {
flex: 0 0 100px;
padding: 5px 8px;
white-space: nowrap;
}
.submit {
margin-top: 4px;
}
.error {
margin: -2px 0 10px;
color: var(--mall-red);
font-size: 12px;
}
.auth-links {
display: flex;
align-items: center;
gap: 8px;
margin-top: 17px;
color: var(--mall-faint);
font-size: 12px;
flex-wrap: wrap;
}
@media (max-width: 1240px) {
.auth-inner {
width: calc(100% - 40px);
}
}
@media (max-width: 640px) {
.auth-page {
padding: 20px;
}
.auth-inner {
width: 100%;
}
.auth-panel {
width: 100%;
padding: 30px 24px;
}
}
</style>