summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/LoginPage.vue
blob: 36a7de5a21ce97c5f39bedb6d814c83cdca7c196 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<i18n>
{
  "en": {
    "username": "Username",
    "password": "Password",
    "login": "Login",
    "usernameError": "User not found.",
    "passwordError": "Wrong password.",
    "usernameEmptyError": "Username can not be empty.",
    "passwordEmptyError": "Passwort can not be empty."
  },
  "de": {
    "username": "Benutzername",
    "password": "Passwort",
    "login": "Anmelden",
    "usernameError": "Benutzer nicht gefunden.",
    "passwordError": "Passwort falsch.",
    "usernameEmptyError": "Benutzername kann nicht leer sein.",
    "passwordEmptyError": "Passwort kann nicht leer sein."
  }
}
</i18n>

<template>
  <v-app dark class="grey darken-4 non-selectable">
    <div class="login-page">
      <img class="logo non-draggable" src="@/assets/logo.svg" />
      <v-form class="login-form" ref="form" v-model="valid" lazy-validation @submit.prevent="login">
        <v-text-field
          v-model="username"
          :rules="usernameRules"
          :label="$t('username')"
          autocomplete="off"
          @input="clearErrors"
        ></v-text-field>
        <v-text-field
          type="password"
          v-model="password"
          :rules="passwordRules"
          :label="$t('password')"
          autocomplete="off"
          @input="passwordError = false"
        ></v-text-field>
        <v-btn type="submit" class="login-button primary" raised>{{ $t('login') }}</v-btn>
      </v-form>
    </div>
  </v-app>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'LoginPage',
  data () {
    return {
      valid: true,
      username: '',
      usernameError: false,
      usernameRules: [
        v => !!v || this.$t('usernameEmptyError'),
        v => !this.usernameError || this.$t('usernameError')
      ],
      password: '',
      passwordError: false,
      passwordRules: [
        v => !!v || this.$t('passwordEmptyError'),
        v => !this.passwordError || this.$t('passwordError')
      ]
    }
  },
  computed: mapState(['dark']),
  methods: {
    clearErrors () {
      if (this.usernameError) this.usernameError = false
      if (this.passwordError) {
        this.passwordError = false
        this.$refs.form.validate()
      }
    },
    login () {
      if (this.$refs.form.validate()) {
        this.$http.post('/api/login', { username: this.username, password: this.password })
          .then(response => {
            this.$router.push('/dashboard')
          })
          .catch(error => {
            if (error.response.data.status === 'USER_NOTFOUND') {
              this.usernameError = true
            } else if (error.response.data.status === 'PASSWORD_INVALID') {
              this.passwordError = true
            }
            this.$refs.form.validate()
          })
      }
    }
  }
}
</script>

<style scoped>

.login-page {
  height: 100%;
  width: 100%;
  min-height: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 180px;
  margin-bottom: 60px;
}

.login-form {
  width: 300px;
}

.login-button {
  margin-top: 20px;
  float: right;
}

</style>