keanu-weblite/src/components/Login.vue

142 lines
3.6 KiB
Vue
Raw Normal View History

2020-11-09 10:26:56 +01:00
<template>
2021-01-08 11:07:02 +01:00
<div class="login-root">
<v-btn v-if="showBackArrow" icon @click.stop="$navigation.pop">
<v-icon>arrow_back</v-icon>
</v-btn>
<div color="rgba(255,255,255,0.1)" class="text-center">
2021-01-21 16:31:37 +01:00
<div class="h2">Login</div>
2020-11-09 10:26:56 +01:00
<v-form v-model="isValid">
<v-text-field
2021-02-01 13:13:23 +01:00
v-model="user.user_id"
2020-11-09 10:26:56 +01:00
label="Username"
2020-11-09 15:55:17 +01:00
color="black"
background-color="white"
outlined
2020-11-09 10:26:56 +01:00
:rules="[(v) => !!v || 'Username is required']"
:error="userErrorMessage != null"
:error-messages="userErrorMessage"
required
2020-12-04 10:01:00 +01:00
v-on:keyup.enter="$refs.password.focus()"
2020-11-09 10:26:56 +01:00
></v-text-field>
<v-text-field
2020-12-04 10:01:00 +01:00
ref="password"
2020-11-09 10:26:56 +01:00
v-model="user.password"
label="Password"
2020-11-09 15:55:17 +01:00
color="black"
background-color="white"
outlined
2020-11-09 10:26:56 +01:00
type="password"
:rules="[(v) => !!v || 'Password is required']"
:error="passErrorMessage != null"
:error-messages="passErrorMessage"
required
2020-12-04 10:01:00 +01:00
v-on:keyup.enter="() => { if (isValid && !loading) { handleLogin() }}"
2020-11-09 10:26:56 +01:00
></v-text-field>
<v-btn
:disabled="!isValid || loading"
primary
large
block
@click.stop="handleLogin"
:loading="loading"
>Login</v-btn
>
</v-form>
2020-11-09 15:55:17 +01:00
</div>
2020-11-09 10:26:56 +01:00
</div>
</template>
<script>
import User from "../models/user";
export default {
name: "Login",
data() {
return {
user: new User("https://neo.keanu.im", "", ""),
isValid: true,
loading: false,
message: "",
userErrorMessage: null,
passErrorMessage: null,
};
},
computed: {
loggedIn() {
return this.$store.state.auth.status.loggedIn;
},
currentUser() {
return this.$store.state.auth.user;
},
2021-01-08 11:07:02 +01:00
showBackArrow() {
2021-01-11 17:42:58 +01:00
return this.$navigation && this.$navigation.canPop();
2021-01-08 11:07:02 +01:00
}
2020-11-09 10:26:56 +01:00
},
created() {
if (this.loggedIn) {
this.$navigation.push({name: "Chat", params: { roomId: this.$matrix.currentRoomId }}, -1);
2020-11-09 10:26:56 +01:00
}
},
watch: {
user: {
handler() {
// Reset manual errors
this.userErrorMessage = null;
this.passErrorMessage = null;
},
deep: true,
},
message() {
if (
this.message &&
2021-01-21 16:31:37 +01:00
this.message.toLowerCase().includes("user")
2020-11-09 10:26:56 +01:00
) {
2021-01-21 16:31:37 +01:00
this.userErrorMessage = this.message;
2020-11-09 10:26:56 +01:00
} else {
this.userErrorMessage = null;
}
if (
this.message &&
2021-01-21 16:31:37 +01:00
this.message.toLowerCase().includes("pass")
2020-11-09 10:26:56 +01:00
) {
2021-01-21 16:31:37 +01:00
this.passErrorMessage = this.message;
2020-11-09 10:26:56 +01:00
} else {
this.passErrorMessage = null;
}
},
},
methods: {
handleLogin() {
2021-02-01 13:13:23 +01:00
if (this.user.user_id && this.user.password) {
2021-01-21 16:31:37 +01:00
// Reset errors
this.message = null;
// Is it a full matrix user id? Modify a copy, so that the UI will still show the full ID.
var user = Object.assign({}, this.user);
user.normalize();
2020-11-09 10:26:56 +01:00
this.loading = true;
this.$store.dispatch("auth/login", user).then(
2020-11-09 10:26:56 +01:00
() => {
this.$navigation.push({name: "Chat", params: { roomId: this.$matrix.currentRoomId }}, -1);
2020-11-09 10:26:56 +01:00
},
(error) => {
this.loading = false;
this.message =
2021-01-21 16:31:37 +01:00
(error.data && error.data.error) ||
2020-11-09 10:26:56 +01:00
error.message ||
error.toString();
2021-01-21 16:31:37 +01:00
console.log("Message set to ", this.message);
2020-11-09 10:26:56 +01:00
}
);
}
},
},
};
2020-11-09 15:55:17 +01:00
</script>
<style lang="scss">
@import "@/assets/css/login.scss";
</style>