audon/audon-fe/src/views/LoginView.vue

101 wiersze
2.6 KiB
Vue
Czysty Zwykły widok Historia

2022-12-03 03:20:49 +00:00
<script>
import { RouterLink } from "vue-router";
2022-12-04 05:19:41 +00:00
import { useVuelidate } from "@vuelidate/core";
2022-12-26 12:18:09 +00:00
import { required, helpers, email, or } from "@vuelidate/validators";
2022-12-06 13:20:36 +00:00
import { validators } from "../assets/utils";
2022-12-07 05:45:05 +00:00
import { map } from "lodash-es";
2022-12-04 05:19:41 +00:00
import axios from "axios";
2022-12-03 03:20:49 +00:00
export default {
2022-12-04 05:19:41 +00:00
setup() {
return {
v$: useVuelidate(),
};
},
2022-12-03 03:20:49 +00:00
data() {
return {
server: "",
2022-12-04 17:52:44 +00:00
serverErr: "",
2022-12-03 03:20:49 +00:00
};
},
2022-12-04 05:19:41 +00:00
validations() {
return {
server: {
2022-12-17 02:30:46 +00:00
required: helpers.withMessage(this.$t("addressRequired"), required),
2022-12-04 05:19:41 +00:00
hostname: helpers.withMessage(
2022-12-26 12:18:09 +00:00
this.$t("errors.invalidAddress"),
or(validators.fqdn, email)
)
2022-12-04 05:19:41 +00:00
},
};
},
computed: {
serverErrors() {
const errors = this.v$.server.$errors;
2022-12-07 05:45:05 +00:00
const messages = map(errors, (e) => e.$message);
2022-12-04 17:52:44 +00:00
if (this.serverErr !== "") {
messages.push(this.serverErr);
}
return messages;
2022-12-04 05:19:41 +00:00
},
},
2022-12-03 03:20:49 +00:00
methods: {
2022-12-04 05:19:41 +00:00
async onSubmit() {
2022-12-26 12:18:09 +00:00
if (this.server.includes("@")) {
this.server = this.server.split("@", 2)[1]
}
2022-12-04 05:19:41 +00:00
const isFormCorrect = await this.v$.$validate();
if (!isFormCorrect) {
return;
}
2022-12-04 17:52:44 +00:00
try {
2022-12-04 19:50:55 +00:00
const response = await axios.postForm("/app/login", {
2022-12-10 03:16:43 +00:00
redir: this.$route.query.l ?? "/",
2022-12-04 17:52:44 +00:00
server: this.server,
});
if (response.status === 201) {
this.serverErr = "";
2022-12-07 05:45:05 +00:00
location.assign(response.data);
2022-12-04 17:52:44 +00:00
}
} catch (error) {
2022-12-06 08:57:20 +00:00
if (error.response?.status === 404) {
2022-12-26 12:18:09 +00:00
this.serverErr = this.$t("errors.serverNotFound");
2022-12-04 17:52:44 +00:00
}
2022-12-04 05:19:41 +00:00
}
},
2022-12-06 08:57:20 +00:00
onInput() {
2022-12-04 17:52:44 +00:00
this.v$.server.$touch();
this.serverErr = "";
2022-12-06 08:57:20 +00:00
},
2022-12-04 05:19:41 +00:00
},
2022-12-03 03:20:49 +00:00
};
</script>
<template>
2023-01-03 03:58:41 +00:00
<div class="text-center mb-8">
<img src="../assets/img/audon-wordmark-white-text.svg" :draggable="false" alt="Branding Wordmark" style="width: 100%; max-width: 200px;" />
</div>
2022-12-17 02:30:46 +00:00
<v-alert v-if="$route.query.l" type="warning" variant="text">
<div>{{ $t("loginRequired") }}</div>
2022-12-06 08:57:20 +00:00
</v-alert>
2022-12-04 05:19:41 +00:00
<v-form ref="form" @submit.prevent="onSubmit" class="my-3" lazy-validation>
2022-12-03 03:20:49 +00:00
<v-text-field
v-model="server"
name="server"
2022-12-17 02:30:46 +00:00
:label="$t('server')"
2022-12-03 03:20:49 +00:00
placeholder="mastodon.example"
class="mb-2"
2022-12-04 05:19:41 +00:00
:error-messages="serverErrors"
2022-12-04 17:52:44 +00:00
@update:model-value="onInput"
2022-12-08 08:56:29 +00:00
type="url"
2022-12-03 03:20:49 +00:00
clearable
/>
2022-12-04 05:19:41 +00:00
<v-btn block @click="onSubmit" :disabled="!v$.$dirty || v$.$error"
2022-12-17 02:30:46 +00:00
>{{ $t("login") }}</v-btn
2022-12-04 05:19:41 +00:00
>
2022-12-03 03:20:49 +00:00
</v-form>
<div class="w-100 text-right">
2022-12-17 02:30:46 +00:00
<RouterLink to="/about">{{ $t("about") }}</RouterLink>
2022-12-03 03:20:49 +00:00
</div>
</template>