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
240 lines
5.9 KiB
Vue
240 lines
5.9 KiB
Vue
<script setup lang="ts">
|
|
const { t } = useI18n();
|
|
const email = ref("");
|
|
const password = ref("");
|
|
const confirmPassword = ref("");
|
|
const code = ref("");
|
|
const errorKey = ref("");
|
|
const success = ref(false);
|
|
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);
|
|
});
|
|
|
|
function validate(): boolean {
|
|
if (!email.value || !password.value || !confirmPassword.value || !code.value) {
|
|
errorKey.value = "auth.validationRequired";
|
|
return false;
|
|
}
|
|
if (!/^\S+@\S+\.\S+$/.test(email.value)) {
|
|
errorKey.value = "auth.validationEmail";
|
|
return false;
|
|
}
|
|
// Matches the shared validationPassword message and the auth API's rule.
|
|
if (password.value.length < 8) {
|
|
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;
|
|
await new Promise<void>((resolve) => setTimeout(resolve, 180));
|
|
success.value = true;
|
|
submitting.value = false;
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section class="auth-page">
|
|
<div class="auth-inner">
|
|
<div class="auth-panel">
|
|
<template v-if="success">
|
|
<div class="success-state">
|
|
<div class="success-mark">✓</div>
|
|
<h1>{{ t("auth.resetSuccess") }}</h1>
|
|
<p>{{ t("auth.resetSuccessHint") }}</p>
|
|
<NuxtLink class="mbtn red block" to="/login">{{ t("auth.loginNow") }}</NuxtLink>
|
|
</div>
|
|
</template>
|
|
<template v-else>
|
|
<div class="auth-tabs">
|
|
<h1>{{ t("auth.forgotTab") }}</h1>
|
|
<NuxtLink to="/login">{{ t("auth.loginTab") }}</NuxtLink>
|
|
</div>
|
|
<form @submit.prevent="submit">
|
|
<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("auth.newPassword") }}</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>
|
|
<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.resetAction") }}
|
|
</button>
|
|
</form>
|
|
<div class="auth-links">
|
|
<NuxtLink to="/login">{{ t("auth.loginNow") }}</NuxtLink>
|
|
</div>
|
|
</template>
|
|
</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 {
|
|
margin-top: 17px;
|
|
font-size: 12px;
|
|
text-align: center;
|
|
}
|
|
.success-state {
|
|
padding-top: 60px;
|
|
text-align: center;
|
|
}
|
|
.success-mark {
|
|
width: 54px;
|
|
height: 54px;
|
|
margin: 0 auto 18px;
|
|
border-radius: 50%;
|
|
background: var(--mall-green);
|
|
color: #fff;
|
|
font-size: 34px;
|
|
line-height: 54px;
|
|
}
|
|
.success-state h1 {
|
|
font-size: 20px;
|
|
}
|
|
.success-state p {
|
|
margin: 14px 0 28px;
|
|
color: var(--mall-muted);
|
|
font-size: 13px;
|
|
line-height: 1.6;
|
|
}
|
|
.success-state a {
|
|
text-decoration: none;
|
|
}
|
|
@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>
|