Converted views/Login.vue to compositionapi

This commit is contained in:
Joep 2023-09-09 13:20:47 +02:00
parent ec96b32bbe
commit 1602d38df5

View File

@ -11,39 +11,39 @@
type="text" type="text"
autocapitalize="off" autocapitalize="off"
v-model="username" v-model="username"
:placeholder="$t('login.username')" :placeholder="t('login.username')"
/> />
<input <input
class="input input--block" class="input input--block"
type="password" type="password"
v-model="password" v-model="password"
:placeholder="$t('login.password')" :placeholder="t('login.password')"
/> />
<input <input
class="input input--block" class="input input--block"
v-if="createMode" v-if="createMode"
type="password" type="password"
v-model="passwordConfirm" v-model="passwordConfirm"
:placeholder="$t('login.passwordConfirm')" :placeholder="t('login.passwordConfirm')"
/> />
<div v-if="recaptcha" id="recaptcha"></div> <div v-if="recaptcha" id="recaptcha"></div>
<input <input
class="button button--block" class="button button--block"
type="submit" type="submit"
:value="createMode ? $t('login.signup') : $t('login.submit')" :value="createMode ? t('login.signup') : t('login.submit')"
/> />
<p @click="toggleMode" v-if="signup"> <p @click="toggleMode" v-if="signup">
{{ {{
createMode ? $t("login.loginInstead") : $t("login.createAnAccount") createMode ? t("login.loginInstead") : t("login.createAnAccount")
}} }}
</p> </p>
</form> </form>
</div> </div>
</template> </template>
<script> <script setup lang="ts">
import * as auth from "@/utils/auth"; import * as auth from "@/utils/auth";
import { import {
name, name,
@ -52,25 +52,70 @@ import {
recaptchaKey, recaptchaKey,
signup, signup,
} from "@/utils/constants"; } from "@/utils/constants";
import { onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
export default { // Define refs
name: "login", const createMode = ref<boolean>(false)
computed: { const error = ref<string>("")
signup: () => signup, const username = ref<string>("")
name: () => name, const password = ref<string>("")
logoURL: () => logoURL, const passwordConfirm = ref<string>("")
},
data: function () { const route = useRoute()
return { const router = useRouter()
createMode: false, const { t } = useI18n({})
error: "", // Define functions
username: "", const toggleMode = () => createMode.value = !createMode.value;
password: "",
recaptcha: recaptcha,
passwordConfirm: "", const submit = async (event: Event) => {
}; event.preventDefault();
}, event.stopPropagation();
mounted() {
let redirect = route.query.redirect;
if (redirect === "" || redirect === undefined || redirect === null) {
redirect = "/files/";
}
let captcha = "";
if (recaptcha) {
captcha = window.grecaptcha.getResponse();
if (captcha === "") {
error.value = t("login.wrongCredentials");
return;
}
}
if (createMode.value) {
if (password.value !== passwordConfirm.value) {
error.value = t("login.passwordsDontMatch");
return;
}
}
try {
if (createMode.value) {
await auth.signup(username.value, password.value);
}
await auth.login(username.value, password.value, captcha);
// @ts-ignore
router.push({ path: redirect });
} catch (e: any) {
console.error(e);
if (e.message == 409) {
error.value = t("login.usernameTaken");
} else {
error.value = t("login.wrongCredentials");
}
}
}
// Run hooks
onMounted(() => {
if (!recaptcha) return; if (!recaptcha) return;
window.grecaptcha.ready(function () { window.grecaptcha.ready(function () {
@ -78,53 +123,6 @@ export default {
sitekey: recaptchaKey, sitekey: recaptchaKey,
}); });
}); });
}, })
methods: {
toggleMode() {
this.createMode = !this.createMode;
},
async submit(event) {
event.preventDefault();
event.stopPropagation();
let redirect = this.$route.query.redirect;
if (redirect === "" || redirect === undefined || redirect === null) {
redirect = "/files/";
}
let captcha = "";
if (recaptcha) {
captcha = window.grecaptcha.getResponse();
if (captcha === "") {
this.error = this.$t("login.wrongCredentials");
return;
}
}
if (this.createMode) {
if (this.password !== this.passwordConfirm) {
this.error = this.$t("login.passwordsDontMatch");
return;
}
}
try {
if (this.createMode) {
await auth.signup(this.username, this.password);
}
await auth.login(this.username, this.password, captcha);
this.$router.push({ path: redirect });
} catch (e) {
console.error(e);
if (e.message == 409) {
this.error = this.$t("login.usernameTaken");
} else {
this.error = this.$t("login.wrongCredentials");
}
}
},
},
};
</script> </script>