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
173 lines
4.0 KiB
Vue
173 lines
4.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 email = ref("");
|
|
const password = ref("");
|
|
const errorKey = ref("");
|
|
const submitting = ref(false);
|
|
|
|
function validate(): boolean {
|
|
if (!email.value || !password.value) {
|
|
errorKey.value = "auth.validationRequired";
|
|
return false;
|
|
}
|
|
if (!/^\S+@\S+\.\S+$/.test(email.value)) {
|
|
errorKey.value = "auth.validationEmail";
|
|
return false;
|
|
}
|
|
// No length rule here: the auth API owns the password policy, and a wrong
|
|
// password should be reported as rejected credentials, not as bad input.
|
|
return true;
|
|
}
|
|
|
|
async function submit(): Promise<void> {
|
|
errorKey.value = "";
|
|
if (!validate()) return;
|
|
submitting.value = true;
|
|
try {
|
|
const auth = await $api.login(email.value, password.value);
|
|
session.setAuth(auth);
|
|
await router.push("/");
|
|
} catch (error) {
|
|
errorKey.value = error instanceof ApiError && error.status === 401
|
|
? "auth.invalidCredentials"
|
|
: "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.loginTab") }}</h1>
|
|
<NuxtLink to="/register">{{ t("auth.registerTab") }}</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("common.password") }}</span>
|
|
<input v-model="password" class="minput" type="password" autocomplete="current-password" :placeholder="t('auth.passwordPlaceholder')" />
|
|
</label>
|
|
<p v-if="errorKey" class="error">{{ t(errorKey) }}</p>
|
|
<button class="mbtn red block submit" type="submit" :disabled="submitting">
|
|
{{ t("auth.loginAction") }}
|
|
</button>
|
|
</form>
|
|
<div class="auth-links">
|
|
<span>{{ t("auth.noAccount") }}</span>
|
|
<NuxtLink to="/register">{{ t("auth.registerNow") }}</NuxtLink>
|
|
<NuxtLink to="/forgot-password">{{ t("auth.forgotPassword") }}</NuxtLink>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.auth-page {
|
|
min-height: 620px;
|
|
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: 450px;
|
|
margin: 0 auto;
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
align-items: flex-start;
|
|
}
|
|
.auth-panel {
|
|
width: 400px;
|
|
min-height: 450px;
|
|
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: 28px;
|
|
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 {
|
|
color: var(--mall-faint);
|
|
font-size: 14px;
|
|
text-decoration: none;
|
|
}
|
|
.auth-tabs a:hover,
|
|
.auth-links a:hover {
|
|
color: var(--mall-red);
|
|
}
|
|
.field {
|
|
display: block;
|
|
margin-bottom: 18px;
|
|
}
|
|
.field span {
|
|
display: block;
|
|
margin-bottom: 7px;
|
|
color: var(--mall-muted);
|
|
font-size: 13px;
|
|
}
|
|
.submit {
|
|
margin-top: 4px;
|
|
}
|
|
.error {
|
|
margin: -4px 0 12px;
|
|
color: var(--mall-red);
|
|
font-size: 12px;
|
|
}
|
|
.auth-links {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
margin-top: 18px;
|
|
color: var(--mall-faint);
|
|
font-size: 12px;
|
|
flex-wrap: wrap;
|
|
}
|
|
.auth-links a {
|
|
color: var(--mall-red);
|
|
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>
|