register.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <section class="hero is-success is-fullheight">
  3. <div class="hero-body">
  4. <div class="container has-text-centered">
  5. <div class="column is-4 is-offset-4">
  6. <h3 class="title has-text-grey">Register</h3>
  7. <p class="subtitle has-text-grey">Please register to proceed.</p>
  8. <div class="box">
  9. <figure class="avatar">
  10. <img src="https://via.placeholder.com/300">
  11. </figure>
  12. <form>
  13. <div class="field">
  14. <div class="control">
  15. <input
  16. v-model="form.username"
  17. @blur="$v.form.username.$touch()"
  18. class="input is-large"
  19. type="text"
  20. placeholder="Username">
  21. <div v-if="$v.form.username.$error" class="form-error">
  22. <span v-if="!$v.form.username.required" class="help is-danger">Username is required</span>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="field">
  27. <div class="control">
  28. <input
  29. v-model="form.name"
  30. @blur="$v.form.name.$touch()"
  31. class="input is-large"
  32. type="text"
  33. placeholder="Name">
  34. <div v-if="$v.form.name.$error" class="form-error">
  35. <span v-if="!$v.form.name.required" class="help is-danger">Name is required</span>
  36. <span v-if="!$v.form.name.minLength" class="help is-danger">Name minimum length is 6</span>
  37. </div>
  38. </div>
  39. </div>
  40. <div class="field">
  41. <div class="control">
  42. <input
  43. v-model="form.email"
  44. @blur="$v.form.email.$touch()"
  45. class="input is-large"
  46. type="email"
  47. placeholder="Your Email">
  48. <div v-if="$v.form.email.$error" class="form-error">
  49. <span v-if="!$v.form.email.required" class="help is-danger">Email is required</span>
  50. <span v-if="!$v.form.email.emailValidator" class="help is-danger">Email address is not valid</span>
  51. </div>
  52. </div>
  53. </div>
  54. <div class="field">
  55. <div class="control">
  56. <input
  57. v-model="form.avatar"
  58. @blur="$v.form.avatar.$touch()"
  59. class="input is-large"
  60. type="text"
  61. placeholder="Avatar"
  62. autocomplete="">
  63. <div v-if="$v.form.avatar.$error" class="form-error">
  64. <span v-if="!$v.form.avatar.url" class="help is-danger">Url format is not valid!</span>
  65. <span v-if="!$v.form.avatar.supportedFileType" class="help is-danger">Selected file type is not valid!</span>
  66. </div>
  67. </div>
  68. </div>
  69. <div class="field">
  70. <div class="control">
  71. <input
  72. v-model="form.password"
  73. @blur="$v.form.password.$touch()"
  74. class="input is-large"
  75. type="password"
  76. placeholder="Your Password"
  77. autocomplete="new-password">
  78. <div v-if="$v.form.password.$error" class="form-error">
  79. <span v-if="!$v.form.password.required" class="help is-danger">Password is required</span>
  80. <span v-if="!$v.form.password.minLength" class="help is-danger">Password minimum length is 6 letters</span>
  81. </div>
  82. </div>
  83. </div>
  84. <div class="field">
  85. <div class="control">
  86. <input
  87. v-model="form.passwordConfirmation"
  88. @blur="$v.form.passwordConfirmation.$touch()"
  89. class="input is-large"
  90. type="password"
  91. placeholder="Password Confirmation"
  92. autocomplete="off">
  93. <div v-if="$v.form.passwordConfirmation.$error" class="form-error">
  94. <span v-if="!$v.form.passwordConfirmation.required" class="help is-danger">Password is required</span>
  95. <span v-if="!$v.form.passwordConfirmation.sameAs" class="help is-danger">Password confirmation should be the same as password</span>
  96. </div>
  97. </div>
  98. </div>
  99. <button @click.prevent="register" type="button" class="button is-block is-info is-large is-fullwidth">Register</button>
  100. </form>
  101. </div>
  102. <p class="has-text-grey">
  103. <nuxt-link to="/login">Login</nuxt-link> &nbsp;·&nbsp;
  104. <a>Sign Up With Google</a> &nbsp;·&nbsp;
  105. <a href="../">Need Help?</a>
  106. </p>
  107. </div>
  108. </div>
  109. </div>
  110. </section>
  111. </template>
  112. <script>
  113. import { required, email, sameAs, minLength, url } from 'vuelidate/lib/validators'
  114. import { supportedFileType } from '@/helpers/validators'
  115. export default {
  116. middleware: 'guest',
  117. data() {
  118. return {
  119. form : {
  120. username : null,
  121. name : null,
  122. email : null,
  123. avatar : null,
  124. password : null,
  125. passwordConfirmation : null
  126. }
  127. }
  128. },
  129. validations: {
  130. form: {
  131. username : {
  132. required,
  133. minLength: minLength(6)
  134. },
  135. name : {
  136. required,
  137. minLength: minLength(6)
  138. },
  139. email : {
  140. emailValidator : email,
  141. required
  142. },
  143. avatar : {
  144. url,
  145. supportedFileType
  146. },
  147. password : {
  148. required,
  149. minLength: minLength(6)
  150. },
  151. passwordConfirmation : {
  152. required,
  153. sameAs: sameAs('password')
  154. },
  155. }
  156. },
  157. computed : {
  158. isFormValid() {
  159. return !this.$v.form.$invalid
  160. }
  161. },
  162. methods : {
  163. async register() {
  164. console.log(this.form)
  165. this.$v.form.$touch()
  166. if(this.isFormValid){
  167. console.log('register.vue call await this.$store.dispatch(auth/register)')
  168. const result = await this.$store.dispatch('auth/register', this.form)
  169. console.log('register.vue done await this.$store.dispatch(auth/register)', result)
  170. if(result.isAxiosError === true){
  171. console.log('register.vue error await this.$store.dispatch(auth/register)')
  172. this.$toasted.error(result.response.data.errors.message, {duration: 3000})
  173. }else{
  174. this.$router.push('/login')
  175. }
  176. }
  177. }
  178. }
  179. }
  180. </script>
  181. <style scoped>
  182. .hero.is-success {
  183. background: #F2F6FA;
  184. }
  185. .hero .nav, .hero.is-success .nav {
  186. -webkit-box-shadow: none;
  187. box-shadow: none;
  188. }
  189. .box {
  190. margin-top: 5rem;
  191. }
  192. .avatar {
  193. margin-top: -70px;
  194. padding-bottom: 20px;
  195. }
  196. .avatar img {
  197. height: 128px;
  198. width: 128px;
  199. padding: 5px;
  200. background: #fff;
  201. border-radius: 50%;
  202. -webkit-box-shadow: 0 2px 3px rgba(10,10,10,.1), 0 0 0 1px rgba(10,10,10,.1);
  203. box-shadow: 0 2px 3px rgba(10,10,10,.1), 0 0 0 1px rgba(10,10,10,.1);
  204. }
  205. p.subtitle {
  206. padding-top: 1rem;
  207. }
  208. </style>