index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Register
  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="/login">
  41. Go To Login
  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 registerd!
  50. </md-snackbar>
  51. </md-card>
  52. </div>
  53. </template>
  54. <script>
  55. import { validationMixin } from 'vuelidate'
  56. import { required, email, minLength, maxLength } from 'vuelidate/lib/validators'
  57. export default {
  58. middleware: 'auth',
  59. mixins: [validationMixin],
  60. validations: {
  61. form: {
  62. email: {
  63. required,
  64. email
  65. },
  66. password: {
  67. required,
  68. minLength: minLength(6),
  69. maxLength: maxLength(20)
  70. }
  71. }
  72. },
  73. data: () => ({
  74. form: {
  75. email: '',
  76. password: ''
  77. }
  78. }),
  79. computed: {
  80. loading () {
  81. return this.$store.getters.loading
  82. },
  83. isAuthenticated () {
  84. return this.$store.getters.isAuthenticated
  85. }
  86. },
  87. watch: {
  88. isAuthenticated (value) {
  89. if (value) {
  90. setTimeout(() => this.$router.push('/'), 500)
  91. }
  92. }
  93. },
  94. methods: {
  95. validateForm () {
  96. this.$v.$touch()
  97. if (!this.$v.$invalid) {
  98. this.resisterUser()
  99. }
  100. },
  101. async resisterUser () {
  102. await this.$store.dispatch('authenticateUser', {
  103. action: 'register',
  104. email: this.form.email,
  105. password: this.form.password,
  106. resturnSecureToken: true
  107. })
  108. },
  109. getValidationClass (fieldName) {
  110. const field = this.$v.form[fieldName]
  111. if (field) {
  112. return {
  113. 'md-invalid': field.$invalid && field.$dirty
  114. }
  115. }
  116. }
  117. }
  118. }
  119. </script>