Advertisement
Guest User

Untitled

a guest
Dec 31st, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. <template> <!--v-if="!this.submitSuccess && this.props.user && Object.keys(this.props.user).length === 0">-->
  2. <form @submit="handleSubmit">
  3. <label htmlFor='Username'>
  4. <div class='inputFormLabel'>Username</div>
  5. <div class='inputFormContainer'>
  6. <input
  7. class='inputFormSpacer inputForm'
  8. type='text'
  9. v-model='form.username'
  10. size='40'
  11. placeholder='Username'
  12. @change='this.handleChange'
  13. />
  14. <!--<div class='inputFormError'>{{this.getErrorMessage('username')}}</div>-->
  15. </div>
  16. </label>
  17. <br />
  18. <label htmlFor='Password'>
  19. <div class='inputFormLabel'>Password</div>
  20. <div class='inputFormContainer'>
  21. <input
  22. class='inputFormSpacer inputForm'
  23. type='password'
  24. v-model='form.password'
  25. size='40'
  26. placeholder='Password'
  27. @change='this.handleChange'
  28. />
  29. <!--<div class='inputFormError'>{{this.getErrorMessage('password')}}</div>-->
  30. </div>
  31. </label>
  32. <br />
  33. <div class='formErrorSpacer inputFormError'>{{this.error}}</div>
  34. <button class='button' type="submit">Submit</button>
  35. </form>
  36. </template>
  37. <!--<template v-else>-->
  38. <!--<div>-->
  39. <!--window.location.href = '/'-->
  40. <!--</div>-->
  41. <!--</template>-->
  42.  
  43. <script>
  44. import { mapState, mapMutations } from 'vuex';
  45. import InputFormComponent from './InputFormComponent';
  46. import { post } from '../util/RequestUtil';
  47. import { loginRules } from '../../../resources/validation/validationRules';
  48. import { createAuthObj, createLoginRequestConfig } from '../util/AppUtil';
  49.  
  50.  
  51. export default {
  52. name: 'loginForm',
  53. // props: {
  54. // user: Object,
  55. // // addUser: Function
  56. // },
  57. data() {
  58. return {
  59. form: {
  60. name: 'loginForm',
  61. username: '',
  62. password: '',
  63. },
  64. };
  65. },
  66. computed: {
  67. ...mapState({
  68. username: state => state.username,
  69. password: state => state.password,
  70. error: state => state.error,
  71. }),
  72. },
  73. components: InputFormComponent,
  74. methods: {
  75. ...mapMutations([
  76. 'ADD_USER',
  77. ]),
  78. addUser(user) {
  79. this.ADD_USER(user);
  80. this.user = {};
  81. },
  82. getErrorMessage(paramName) {
  83. let errorMessage = '';
  84.  
  85. if (paramName === 'username' && this.error !== '') {
  86. errorMessage = this.usernameError;
  87. } else if (this.validationErrors[paramName] && this.validationErrors[paramName] !== '') {
  88. errorMessage = this.validationErrors[paramName];
  89. }
  90.  
  91. return errorMessage;
  92. },
  93. validateParam(paramName, paramValue) {
  94. const validationRule = loginRules[paramName],
  95. validationErrors = this.validationErrors;
  96.  
  97. if (paramValue === '') {
  98. validationErrors[paramName] = validationRule.messageMissing;
  99.  
  100. this.validationErrors = validationErrors;
  101. } else if (this.form.username !== '' && this.form.password !== '' && validationRule.required && paramValue.search(validationRule.regexRule)) {
  102. this.$store.commit('setError', loginRules.login.message);
  103. } else {
  104. delete validationErrors[paramName];
  105.  
  106. this.validationErrors = validationErrors;
  107. }
  108. },
  109. validate() {
  110. this.validationErrors = {};
  111. this.$store.commit('setError', '');
  112.  
  113. this.validateParam('username', this.form.username);
  114. this.validateParam('password', this.form.password);
  115. },
  116. isFormValid() {
  117. return this.formReady && Object.keys(this.validationErrors).length === 0;
  118. },
  119. isFormInvalid() {
  120. return !this.isFormValid();
  121. },
  122. handleChange(event) {
  123. this[event.target.name] = event.target.value;
  124. },
  125. handleSubmit(event) {
  126. event.preventDefault();
  127.  
  128. this.validate();
  129.  
  130. this.formReady = true;
  131.  
  132. if (this.isFormValid()) {
  133. const requestData = {
  134. username: this.form.username,
  135. password: this.form.password,
  136. },
  137. requestConfig = createLoginRequestConfig(requestData);
  138.  
  139. return post(requestConfig).then(response => {
  140. if (response.status === 200 && response.headers['x-auth-token'] && response.data) {
  141. const result = response.data;
  142.  
  143. if (result.data && Object.keys(result.data).length > 0) {
  144. const userData = result.data,
  145. authToken = response.headers['x-auth-token'],
  146. authObj = createAuthObj(userData, authToken);
  147.  
  148. localStorage.setItem('authObj', authObj);
  149.  
  150. this.submitSuccess = true;
  151. // this.addUser(userData);
  152. }
  153. } else {
  154. this.submitSuccess = false;
  155.  
  156. // console.log('INVALID FORM response', response);
  157.  
  158. if (response.validationErrors) {
  159. if (response.validationErrors.username) {
  160. this.$store.commit('setError', response.validationErrors.username);
  161. } else if (response.validationErrors.error) {
  162. this.$store.commit('setError', response.validationErrors.error);
  163. }
  164. }
  165. }
  166. })
  167. .catch((err) => {
  168. // console.error(err);
  169.  
  170. this.$store.commit('setError', 'There was a problem accessing your account');
  171. });
  172. } else {
  173. console.log('INVALID FORM validationErrors', this.validationErrors);
  174. }
  175. },
  176. },
  177. };
  178. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement