logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

registration.js (4667B)


  1. import useVuelidate from '@vuelidate/core'
  2. import { required, requiredIf, sameAs } from '@vuelidate/validators'
  3. import { mapActions, mapState } from 'vuex'
  4. import InterfaceLanguageSwitcher from '../interface_language_switcher/interface_language_switcher.vue'
  5. import localeService from '../../services/locale/locale.service.js'
  6. import { DAY } from 'src/services/date_utils/date_utils.js'
  7. import TermsOfServicePanel from '../terms_of_service_panel/terms_of_service_panel.vue'
  8. const registration = {
  9. setup () { return { v$: useVuelidate() } },
  10. data: () => ({
  11. user: {
  12. email: '',
  13. fullname: '',
  14. username: '',
  15. password: '',
  16. confirm: '',
  17. birthday: '',
  18. reason: '',
  19. language: ['']
  20. },
  21. captcha: {}
  22. }),
  23. components: {
  24. InterfaceLanguageSwitcher,
  25. TermsOfServicePanel
  26. },
  27. validations () {
  28. return {
  29. user: {
  30. email: { required: requiredIf(() => this.accountActivationRequired) },
  31. username: { required },
  32. fullname: { required },
  33. password: { required },
  34. confirm: {
  35. required,
  36. sameAs: sameAs(this.user.password)
  37. },
  38. birthday: {
  39. required: requiredIf(() => this.birthdayRequired),
  40. maxValue: value => {
  41. return !this.birthdayRequired || new Date(value).getTime() <= this.birthdayMin.getTime()
  42. }
  43. },
  44. reason: { required: requiredIf(() => this.accountApprovalRequired) },
  45. language: {}
  46. }
  47. }
  48. },
  49. created () {
  50. if ((!this.registrationOpen && !this.token) || this.signedIn) {
  51. this.$router.push({ name: 'root' })
  52. }
  53. this.setCaptcha()
  54. },
  55. computed: {
  56. token () { return this.$route.params.token },
  57. bioPlaceholder () {
  58. return this.replaceNewlines(this.$t('registration.bio_placeholder'))
  59. },
  60. reasonPlaceholder () {
  61. return this.replaceNewlines(this.$t('registration.reason_placeholder'))
  62. },
  63. birthdayMin () {
  64. const minAge = this.birthdayMinAge
  65. const today = new Date()
  66. today.setUTCMilliseconds(0)
  67. today.setUTCSeconds(0)
  68. today.setUTCMinutes(0)
  69. today.setUTCHours(0)
  70. const minDate = new Date()
  71. minDate.setTime(today.getTime() - minAge * DAY)
  72. return minDate
  73. },
  74. birthdayMinAttr () {
  75. return this.birthdayMin.toJSON().replace(/T.+$/, '')
  76. },
  77. birthdayMinFormatted () {
  78. const browserLocale = localeService.internalToBrowserLocale(this.$i18n.locale)
  79. return this.user.birthday && new Date(Date.parse(this.birthdayMin)).toLocaleDateString(browserLocale, { timeZone: 'UTC', day: 'numeric', month: 'long', year: 'numeric' })
  80. },
  81. ...mapState({
  82. registrationOpen: (state) => state.instance.registrationOpen,
  83. signedIn: (state) => !!state.users.currentUser,
  84. isPending: (state) => state.users.signUpPending,
  85. serverValidationErrors: (state) => state.users.signUpErrors,
  86. signUpNotice: (state) => state.users.signUpNotice,
  87. hasSignUpNotice: (state) => !!state.users.signUpNotice.message,
  88. termsOfService: (state) => state.instance.tos,
  89. embeddedToS: (state) => state.instance.embeddedToS,
  90. accountActivationRequired: (state) => state.instance.accountActivationRequired,
  91. accountApprovalRequired: (state) => state.instance.accountApprovalRequired,
  92. birthdayRequired: (state) => state.instance.birthdayRequired,
  93. birthdayMinAge: (state) => state.instance.birthdayMinAge
  94. })
  95. },
  96. methods: {
  97. ...mapActions(['signUp', 'getCaptcha']),
  98. async submit () {
  99. this.user.nickname = this.user.username
  100. this.user.token = this.token
  101. this.user.captcha_solution = this.captcha.solution
  102. this.user.captcha_token = this.captcha.token
  103. this.user.captcha_answer_data = this.captcha.answer_data
  104. if (this.user.language) {
  105. this.user.language = localeService.internalToBackendLocaleMulti(this.user.language.filter(k => k))
  106. }
  107. this.v$.$touch()
  108. if (!this.v$.$invalid) {
  109. try {
  110. const status = await this.signUp(this.user)
  111. if (status === 'ok') {
  112. this.$router.push({ name: 'friends' })
  113. }
  114. // If status is not 'ok' (i.e. it needs further actions to be done
  115. // before you can login), display sign up notice, do not switch anywhere
  116. } catch (error) {
  117. console.warn('Registration failed: ', error)
  118. this.setCaptcha()
  119. }
  120. }
  121. },
  122. setCaptcha () {
  123. this.getCaptcha().then(cpt => { this.captcha = cpt })
  124. },
  125. replaceNewlines (str) {
  126. return str.replace(/\s*\n\s*/g, ' \n')
  127. }
  128. }
  129. }
  130. export default registration