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 (4497B)


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