|
1 | 1 | <template> |
2 | 2 | <v-card height="100%"> |
3 | | - <v-form @submit.prevent="handleLogin()" ref="form" v-model="valid"> |
| 3 | + <v-form @submit.prevent="handleLogin()" ref="form" v-model="isValid"> |
4 | 4 | <v-card-title class="title">Ik wil inloggen</v-card-title> |
5 | 5 | <v-card-text> |
6 | | - <v-alert |
7 | | - :value="errorMessage !== null" |
8 | | - class="mb-10" |
9 | | - transition="fade-transition" |
10 | | - type="error" |
| 6 | + <v-alert :value="!!errorMessage.length" |
| 7 | + class="mb-10" |
| 8 | + transition="fade-transition" |
| 9 | + type="error" |
11 | 10 | > |
12 | 11 | {{errorMessage}} |
13 | 12 | </v-alert> |
|
42 | 41 | </template> |
43 | 42 |
|
44 | 43 | <script> |
45 | | -import loginRequest from '../../api/endpoints/authorisation/login'; |
| 44 | +import LoginRequest from '../../api/endpoints/authorisation/login'; |
| 45 | +import {mapGetters} from 'vuex'; |
46 | 46 |
|
47 | 47 | export default { |
48 | 48 | name: 'LoginCard', |
49 | 49 | data() { |
50 | 50 | return { |
51 | | - errorMessage: null, |
| 51 | + errorMessage: '', |
52 | 52 | isLoading: false, |
53 | 53 | showPassword: false, |
54 | | - valid: null, |
| 54 | + isValid: false, |
55 | 55 | form: { |
56 | 56 | email: '', |
57 | 57 | password: '', |
58 | 58 | }, |
59 | 59 | isRedirecting: false, |
60 | 60 | }; |
61 | 61 | }, |
| 62 | + computed: { |
| 63 | + ...mapGetters({ |
| 64 | + findError: 'Error/find', |
| 65 | + }), |
| 66 | + }, |
62 | 67 | methods: { |
63 | | - async handleLogin() { |
64 | | - this.$refs.form.validate(); |
65 | | - if (!this.valid) { |
66 | | - return; |
67 | | - } |
| 68 | + handleLogin() { |
68 | 69 | this.isLoading = true; |
69 | | - const {success, message, token} = await loginRequest(this.form.email, this.form.password); |
70 | | -
|
71 | | - this.isLoading = false; |
72 | | -
|
73 | | - if (success) { |
74 | | - this.isRedirecting = true; |
75 | | - return this.redirectToAuthDispense(token); |
76 | | - } |
77 | | -
|
78 | | - this.errorMessage = message; |
| 70 | + LoginRequest(this.form.email, this.form.password) |
| 71 | + .then(res => { |
| 72 | + this.isRedirecting = true; |
| 73 | + this.redirectToAuthDispense(res.data.token); |
| 74 | + }) |
| 75 | + .catch(() => this.errorMessage = this.findError('email')) |
| 76 | + .finally(() => this.isLoading = false); |
79 | 77 | }, |
80 | 78 | redirectToAuthDispense(token) { |
81 | | - const form = document.createElement("form"); |
| 79 | + const form = document.createElement('form'); |
82 | 80 |
|
83 | 81 | form.method = 'POST'; |
84 | 82 | form.action = process.env.VUE_APP_ROOT_API + '/auth/dispense'; |
85 | 83 |
|
86 | | - const redirectUriElement = document.createElement("input"); |
| 84 | + const redirectUriElement = document.createElement('input'); |
87 | 85 | redirectUriElement.name = 'redirect_uri'; |
88 | | - redirectUriElement.value= 'home'; |
| 86 | + redirectUriElement.value = 'home'; |
89 | 87 | form.appendChild(redirectUriElement); |
90 | 88 |
|
91 | | - const emailElement = document.createElement("input"); |
| 89 | + const emailElement = document.createElement('input'); |
92 | 90 | emailElement.name = 'email'; |
93 | | - emailElement.value= this.form.email; |
| 91 | + emailElement.value = this.form.email; |
94 | 92 | form.appendChild(emailElement); |
95 | 93 |
|
96 | | - const tokenElement = document.createElement("input"); |
| 94 | + const tokenElement = document.createElement('input'); |
97 | 95 | tokenElement.name = 'token'; |
98 | | - tokenElement.value= token; |
| 96 | + tokenElement.value = token; |
99 | 97 | form.appendChild(tokenElement); |
100 | 98 |
|
101 | 99 | document.body.appendChild(form); |
102 | 100 | form.submit(); |
103 | | - } |
| 101 | + }, |
104 | 102 | }, |
105 | 103 | }; |
106 | 104 | </script> |
107 | | - |
108 | | -<style scoped> |
109 | | -
|
110 | | -</style> |
0 commit comments