|
| 1 | +import Vue from 'vue'; |
| 2 | +import {isObject} from 'lodash'; |
| 3 | + |
| 4 | +export default Vue.component('login-form', { |
| 5 | + props: { |
| 6 | + user: { |
| 7 | + type: Object, |
| 8 | + required: false, |
| 9 | + default: function () { |
| 10 | + return { |
| 11 | + email: '', |
| 12 | + password: '', |
| 13 | + }; |
| 14 | + }, |
| 15 | + } |
| 16 | + }, |
| 17 | + data: function () { |
| 18 | + return { |
| 19 | + showSpinner: false, |
| 20 | + } |
| 21 | + }, |
| 22 | + mounted: function () { |
| 23 | + (this.$refs.email as HTMLElement).focus(); |
| 24 | + }, |
| 25 | + methods: { |
| 26 | + // Handle a login request eminating from the form. If 2FA is required the |
| 27 | + // user will be presented with the 2FA modal window. |
| 28 | + submitForm: function () { |
| 29 | + const self = this; |
| 30 | + this.$data.showSpinner = true; |
| 31 | + |
| 32 | + this.$flash.clear(); |
| 33 | + this.$store.dispatch('auth/login', {user: this.$props.user.email, password: this.$props.user.password}) |
| 34 | + .then(response => { |
| 35 | + if (response.complete) { |
| 36 | + return window.location = response.intended; |
| 37 | + } |
| 38 | + |
| 39 | + this.$props.user.password = ''; |
| 40 | + this.$data.showSpinner = false; |
| 41 | + this.$router.push({name: 'checkpoint', query: {token: response.token}}); |
| 42 | + }) |
| 43 | + .catch(err => { |
| 44 | + this.$props.user.password = ''; |
| 45 | + this.$data.showSpinner = false; |
| 46 | + (this.$refs.password as HTMLElement).focus(); |
| 47 | + this.$store.commit('auth/logout'); |
| 48 | + |
| 49 | + if (!err.response) { |
| 50 | + return console.error(err); |
| 51 | + } |
| 52 | + |
| 53 | + const response = err.response; |
| 54 | + if (response.data && isObject(response.data.errors)) { |
| 55 | + response.data.errors.forEach(function (error: any) { |
| 56 | + self.$flash.error(error.detail); |
| 57 | + }); |
| 58 | + } |
| 59 | + }); |
| 60 | + }, |
| 61 | + |
| 62 | + // Update the email address associated with the login form |
| 63 | + // so that it is populated in the parent model automatically. |
| 64 | + updateEmail: function (event: { target: HTMLInputElement }) { |
| 65 | + this.$emit('update-email', event.target.value); |
| 66 | + } |
| 67 | + }, |
| 68 | + template: ` |
| 69 | + <form class="login-box" method="post" |
| 70 | + v-on:submit.prevent="submitForm" |
| 71 | + > |
| 72 | + <div class="flex flex-wrap -mx-3 mb-6"> |
| 73 | + <div class="input-open"> |
| 74 | + <input class="input open-label" id="grid-username" type="text" name="user" aria-labelledby="grid-username-label" required |
| 75 | + ref="email" |
| 76 | + :class="{ 'has-content' : user.email.length > 0 }" |
| 77 | + :readonly="showSpinner" |
| 78 | + :value="user.email" |
| 79 | + v-on:input="updateEmail($event)" |
| 80 | + /> |
| 81 | + <label id="grid-username-label" for="grid-username">{{ $t('strings.user_identifier') }}</label> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + <div class="flex flex-wrap -mx-3 mb-6"> |
| 85 | + <div class="input-open"> |
| 86 | + <input class="input open-label" id="grid-password" type="password" name="password" aria-labelledby="grid-password-label" required |
| 87 | + ref="password" |
| 88 | + :class="{ 'has-content' : user.password && user.password.length > 0 }" |
| 89 | + :readonly="showSpinner" |
| 90 | + v-model="user.password" |
| 91 | + /> |
| 92 | + <label id="grid-password-label" for="grid-password">{{ $t('strings.password') }}</label> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + <div> |
| 96 | + <button id="grid-login-button" class="btn btn-blue btn-jumbo" type="submit" aria-label="Log in" |
| 97 | + v-bind:disabled="showSpinner"> |
| 98 | + <span class="spinner white" v-bind:class="{ hidden: ! showSpinner }"> </span> |
| 99 | + <span v-bind:class="{ hidden: showSpinner }"> |
| 100 | + {{ $t('auth.sign_in') }} |
| 101 | + </span> |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + <div class="pt-6 text-center"> |
| 105 | + <router-link class="text-xs text-grey tracking-wide no-underline uppercase hover:text-grey-dark" aria-label="Forgot password" |
| 106 | + :to="{ name: 'forgot-password' }"> |
| 107 | + {{ $t('auth.forgot_password.label') }} |
| 108 | + </router-link> |
| 109 | + </div> |
| 110 | + </form> |
| 111 | + `, |
| 112 | +}); |
0 commit comments