keanu-weblite/src/components/Login.vue

144 lines
3.8 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">
2020-11-09 15:55:17 +01:00
<h4>Login</h4>
2020-11-09 10:26:56 +01:00
<v-form v-model="isValid">
<v-text-field
v-model="user.username"
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 &&
this.message.message &&
this.message.message.toLowerCase().includes("user")
) {
this.userErrorMessage = this.message.message;
} else {
this.userErrorMessage = null;
}
if (
this.message &&
this.message.message &&
this.message.message.toLowerCase().includes("pass")
) {
this.passErrorMessage = this.message.message;
} else {
this.passErrorMessage = null;
}
},
},
methods: {
handleLogin() {
if (this.user.username && this.user.password) {
// 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);
if (user.username.startsWith('@') && user.username.includes(':')) {
const parts = user.username.split(":");
user.username = parts[0].substring(1);
user.server = "https://" + parts[1];
}
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 =
(error.response && error.response.data) ||
error.message ||
error.toString();
}
);
}
},
},
};
2020-11-09 15:55:17 +01:00
</script>
<style lang="scss">
@import "@/assets/css/login.scss";
</style>