103 lines
3.3 KiB
Vue
103 lines
3.3 KiB
Vue
<script setup lang="ts">
|
|
import { ApiError } from "@vmall/shared";
|
|
|
|
const { t } = useI18n();
|
|
const { $api } = useNuxtApp();
|
|
const session = useSessionStore();
|
|
const router = useRouter();
|
|
const route = useRoute();
|
|
|
|
const email = ref("");
|
|
const password = ref("");
|
|
const errorKey = ref("");
|
|
const submitting = ref(false);
|
|
|
|
/**
|
|
* Where to go after signing in. Only a same-origin absolute path is honoured:
|
|
* "//host" and "https://host" would otherwise turn this into an open redirect.
|
|
*/
|
|
const redirectTarget = computed((): string => {
|
|
const raw = route.query.redirect;
|
|
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
return typeof value === "string" && value.startsWith("/") && !value.startsWith("//")
|
|
? value
|
|
: "/";
|
|
});
|
|
|
|
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(redirectTarget.value);
|
|
} catch (error) {
|
|
errorKey.value =
|
|
error instanceof ApiError && error.status === 401
|
|
? "auth.invalidCredentials"
|
|
: "auth.requestFailed";
|
|
} finally {
|
|
submitting.value = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section class="bg-bg text-text min-h-screen px-5 py-10 font-sans">
|
|
<VCard class="mx-auto my-10 max-w-[420px] p-6 sm:p-10">
|
|
<div class="border-border mb-7 flex items-baseline gap-4 border-b pb-3.5">
|
|
<h1 class="m-0 text-xl font-semibold">{{ t("auth.loginTab") }}</h1>
|
|
<NuxtLink class="text-primary hover:text-primary-hover text-sm" to="/register">{{
|
|
t("auth.registerTab")
|
|
}}</NuxtLink>
|
|
</div>
|
|
<form @submit.prevent="submit">
|
|
<VField :label="t('common.email')">
|
|
<VInput
|
|
v-model.trim="email"
|
|
type="email"
|
|
autocomplete="email"
|
|
:placeholder="t('auth.emailPlaceholder')"
|
|
/>
|
|
</VField>
|
|
<VField :label="t('common.password')">
|
|
<VInput
|
|
v-model="password"
|
|
type="password"
|
|
autocomplete="current-password"
|
|
:placeholder="t('auth.passwordPlaceholder')"
|
|
/>
|
|
</VField>
|
|
<p v-if="errorKey" class="text-danger -mt-1 mb-3 text-xs">{{ t(errorKey) }}</p>
|
|
<VBtn class="mt-1 w-full" variant="primary" type="submit" :disabled="submitting">
|
|
{{ t("auth.loginAction") }}
|
|
</VBtn>
|
|
</form>
|
|
<div class="text-muted mt-5 flex flex-wrap items-center gap-2 text-xs">
|
|
<span>{{ t("auth.noAccount") }}</span>
|
|
<NuxtLink class="text-primary hover:text-primary-hover" to="/register">{{
|
|
t("auth.registerNow")
|
|
}}</NuxtLink>
|
|
<NuxtLink class="text-primary hover:text-primary-hover" to="/forgot-password">{{
|
|
t("auth.forgotPassword")
|
|
}}</NuxtLink>
|
|
</div>
|
|
</VCard>
|
|
</section>
|
|
</template>
|