index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div class="md-layout md-alignment-center-center" style="height: 100vh;">
  3. <md-card class="md-layout-item md-size-50">
  4. <md-card-header>
  5. <div class="md-title">
  6. Login
  7. </div>
  8. </md-card-header>
  9. <form @submit.prevent="validateForm">
  10. <md-card-content>
  11. <md-field :class="getValidationClass('email')" md-clearable>
  12. <label for="email">Email</label>
  13. <md-input
  14. id="email"
  15. v-model="form.email"
  16. :disabled="loading"
  17. type="email"
  18. name="email"
  19. autocomplete="email"
  20. />
  21. <span v-if="!$v.form.email.required" class="md-error">The email is required</span>
  22. <span v-else-if="!$v.form.email.email" class="md-error">Invalid required</span>
  23. </md-field>
  24. <md-field :class="getValidationClass('password')">
  25. <label for="password">password</label>
  26. <md-input
  27. id="password"
  28. v-model="form.password"
  29. :disabled="loading"
  30. type="password"
  31. name="password"
  32. autocomplete="password"
  33. />
  34. <span v-if="!$v.form.password.required" class="md-error">The email is required</span>
  35. <span v-else-if="!$v.form.password.minLength" class="md-error">password too short</span>
  36. <span v-else-if="!$v.form.password.maxLength" class="md-error">password too Long</span>
  37. </md-field>
  38. </md-card-content>
  39. <md-card-actions>
  40. <md-button to="/register">
  41. Go To Register
  42. </md-button>
  43. <md-button class="md-primary md-raised" type="submit">
  44. Submit
  45. </md-button>
  46. </md-card-actions>
  47. </form>
  48. <md-snackbar :md-active.sync="isAuthenticated">
  49. {{ form.email }} was successfully logged in!
  50. </md-snackbar>
  51. </md-card>
  52. <!-- Back Button -->
  53. <md-button @click="$router.go(-1)" class="md-fixed md-fab-bottom-right md-fab md-primary">
  54. <md-icon>arrow_back</md-icon>
  55. </md-button>
  56. </div>
  57. </template>
  58. <script>
  59. import { validationMixin } from 'vuelidate'
  60. import { required, email, minLength, maxLength } from 'vuelidate/lib/validators'
  61. export default {
  62. middleware: 'auth',
  63. mixins: [validationMixin],
  64. validations: {
  65. form: {
  66. email: {
  67. required,
  68. email
  69. },
  70. password: {
  71. required,
  72. minLength: minLength(6),
  73. maxLength: maxLength(20)
  74. }
  75. }
  76. },
  77. data: () => ({
  78. form: {
  79. email: '',
  80. password: ''
  81. }
  82. }),
  83. computed: {
  84. loading () {
  85. return this.$store.getters.loading
  86. },
  87. isAuthenticated () {
  88. return this.$store.getters.isAuthenticated
  89. }
  90. },
  91. watch: {
  92. isAuthenticated (value) {
  93. if (value) {
  94. setTimeout(() => this.$router.push('/'), 500)
  95. }
  96. }
  97. },
  98. methods: {
  99. validateForm () {
  100. this.$v.$touch()
  101. if (!this.$v.$invalid) {
  102. this.loginUser()
  103. }
  104. },
  105. async loginUser () {
  106. await this.$store.dispatch('authenticateUser', {
  107. action: 'login',
  108. email: this.form.email,
  109. password: this.form.password,
  110. resturnSecureToken: true
  111. })
  112. },
  113. getValidationClass (fieldName) {
  114. const field = this.$v.form[fieldName]
  115. if (field) {
  116. return {
  117. 'md-invalid': field.$invalid && field.$dirty
  118. }
  119. }
  120. }
  121. }
  122. }
  123. </script>