This repository has been archived on 2022-05-05. You can view files and clone it, but cannot push or open issues or pull requests.
userausfall/src/components/LoginForm.vue

75 lines
2 KiB
Vue
Raw Normal View History

2021-04-13 11:01:50 +02:00
<template>
2021-04-15 10:05:54 +02:00
<form class="box" @submit.prevent="doAction">
2021-08-02 10:14:38 +02:00
<div v-if="errorMessage" class="notification is-danger">
{{ errorMessage }}
</div>
<b-field label="Benutzername">
<b-input type="text" v-model="user.username" />
2021-04-13 11:01:50 +02:00
</b-field>
<b-field label="Kennwort">
<b-input type="password" v-model="user.password" />
2021-04-13 11:01:50 +02:00
</b-field>
2021-08-02 10:14:38 +02:00
<b-field v-if="mode === 'signup'" label="Kennwort wiederholen">
<b-input type="password" v-model="password2" />
</b-field>
2021-04-13 11:01:50 +02:00
<div class="buttons">
2021-08-02 10:14:38 +02:00
<b-button native-type="submit" type="is-primary">
2021-04-15 10:05:54 +02:00
{{ mode === "login" ? "Anmelden" : "Konto anlegen" }}
2021-04-13 11:01:50 +02:00
</b-button>
2021-08-02 10:14:38 +02:00
<router-link v-if="mode === 'login'" to="/signup"
>Konto anlegen</router-link
>
<router-link v-else to="/login">Anmelden</router-link>
2021-04-13 11:01:50 +02:00
</div>
</form>
</template>
<script lang="ts">
2021-08-02 10:14:38 +02:00
import { Component, Prop, Watch } from "vue-property-decorator";
import { mixins } from "vue-class-component";
import { User } from "@/api";
2021-04-16 10:36:52 +02:00
import { Route } from "vue-router";
2021-08-02 10:14:38 +02:00
import { NotifyMixin } from "@/mixins";
2021-04-13 11:01:50 +02:00
@Component
2021-08-02 10:14:38 +02:00
export default class LoginForm extends mixins(NotifyMixin) {
2021-04-15 10:05:54 +02:00
@Prop() private user!: User;
2021-04-15 10:05:54 +02:00
private mode: "login" | "signup" = "login";
2021-08-02 10:14:38 +02:00
private password2 = "";
private errorMessage = "";
2021-04-15 10:05:54 +02:00
2021-04-16 10:36:52 +02:00
public created(): void {
if (this.$route.name === "signup") this.mode = "signup";
}
@Watch("$route")
public routeChanged(to: Route): void {
if (to.name === "signup") {
this.mode = "signup";
} else {
this.mode = "login";
}
}
2021-04-15 10:05:54 +02:00
private async doAction() {
2021-08-02 10:14:38 +02:00
try {
if (this.mode === "login") {
await this.user.login();
if (this.$route.name !== "home") this.$router.push({ name: "home" });
} else {
await this.user.signup();
this.$router.push({ name: "login" });
this.showSuccess("Du kannst dich nun anmelden.");
}
} catch {
this.errorMessage = "Fehler";
2021-04-15 10:05:54 +02:00
}
}
}
2021-04-13 11:01:50 +02:00
</script>
2021-08-02 10:14:38 +02:00
<style lang="scss"></style>