|
26 | 26 | />
|
27 | 27 | <v-text-field
|
28 | 28 | v-model="form.user.password"
|
29 |
| - class="mb-4" |
| 29 | + :rules="form.passwordRules" |
| 30 | + class="mb-3" |
30 | 31 | label="Password"
|
31 |
| - type="password" |
| 32 | + :type="showPasswordType" |
32 | 33 | required
|
| 34 | + append-inner-icon="mdi-eye" |
| 35 | + @click:append-inner="toggleShowPassword" |
| 36 | + /> |
| 37 | + <v-alert |
| 38 | + v-if="form.error" |
| 39 | + class="mb-6" |
| 40 | + text="Wrong email or password" |
| 41 | + type="error" |
33 | 42 | />
|
34 | 43 | <v-btn
|
35 |
| - elevation="1" |
36 | 44 | :loading="form.loading"
|
| 45 | + color="primary" |
| 46 | + class="mt-1" |
| 47 | + prepend-icon="mdi-check" |
37 | 48 | @click="submit"
|
38 | 49 | >
|
39 | 50 | Submit
|
|
53 | 64 | export default {
|
54 | 65 | data: () => ({
|
55 | 66 | form: {
|
| 67 | + error: false, |
56 | 68 | valid: false,
|
57 | 69 | loading: false,
|
| 70 | + showPassword: false, |
58 | 71 | user: {
|
59 | 72 | username: '',
|
60 | 73 | password: '',
|
61 | 74 | },
|
62 | 75 | emailRules: [
|
63 | 76 | (value) => {
|
64 | 77 | if (value) return true;
|
65 |
| -
|
66 | 78 | return 'E-mail is required.';
|
67 | 79 | },
|
68 | 80 | (value) => {
|
69 | 81 | if (/.+@.+\..+/.test(value)) return true;
|
70 |
| -
|
71 | 82 | return 'E-mail must be valid.';
|
72 | 83 | },
|
73 | 84 | ],
|
| 85 | + passwordRules: [ |
| 86 | + (value) => { |
| 87 | + if (value.length > 3) return true; |
| 88 | + return 'Password must be longer than 3 characters'; |
| 89 | + }, |
| 90 | + ], |
74 | 91 | },
|
75 | 92 | }),
|
| 93 | + computed: { |
| 94 | + showPasswordType() { |
| 95 | + return this.form.showPassword ? 'text' : 'password'; |
| 96 | + }, |
| 97 | + }, |
76 | 98 | methods: {
|
| 99 | + toggleShowPassword() { |
| 100 | + this.form.showPassword = !this.form.showPassword; |
| 101 | + }, |
77 | 102 | async submit() {
|
78 | 103 | if (this.form.valid === false) {
|
79 | 104 | return;
|
80 | 105 | }
|
81 | 106 | this.form.loading = true;
|
82 |
| - const { data } = await this.axios.post('/api/login', this.form.user); |
83 |
| - localStorage.setItem('token', data.token); |
84 |
| - this.$router.push('/'); |
85 |
| - this.form.loading = false; |
| 107 | + try { |
| 108 | + const { data } = await this.axios.post('/api/login', this.form.user); |
| 109 | + localStorage.setItem('token', data.token); |
| 110 | + this.$router.push('/'); |
| 111 | + } catch { |
| 112 | + this.form.error = true; |
| 113 | + } finally { |
| 114 | + this.form.loading = false; |
| 115 | + } |
86 | 116 | },
|
87 | 117 | },
|
88 | 118 | };
|
|
0 commit comments