pleroma-fe/src/components/registration/registration.js

162 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-03-16 22:02:44 +02:00
import useVuelidate from '@vuelidate/core'
import { required, requiredIf, sameAs } from '@vuelidate/validators'
import { mapState as mapPiniaState } from 'pinia'
import { mapActions, mapState } from 'vuex'
2026-01-08 17:26:52 +02:00
import localeService from '../../services/locale/locale.service.js'
2026-01-06 16:23:17 +02:00
import InterfaceLanguageSwitcher from '../interface_language_switcher/interface_language_switcher.vue'
2025-02-10 21:20:56 +00:00
import TermsOfServicePanel from '../terms_of_service_panel/terms_of_service_panel.vue'
2026-01-29 20:40:00 +02:00
import { useInstanceStore } from 'src/stores/instance.js'
import { DAY } from 'src/services/date_utils/date_utils.js'
2017-04-15 18:12:23 +02:00
const registration = {
2026-01-06 16:22:52 +02:00
setup() {
return { v$: useVuelidate() }
},
2017-04-15 18:12:23 +02:00
data: () => ({
user: {
email: '',
fullname: '',
username: '',
password: '',
confirm: '',
2023-01-22 11:15:52 -05:00
birthday: '',
reason: '',
2026-01-06 16:22:52 +02:00
language: [''],
2018-12-15 03:05:47 +03:00
},
2026-01-06 16:22:52 +02:00
captcha: {},
2017-04-15 18:12:23 +02:00
}),
components: {
2025-02-10 21:20:56 +00:00
InterfaceLanguageSwitcher,
2026-01-06 16:22:52 +02:00
TermsOfServicePanel,
},
2026-01-06 16:22:52 +02:00
validations() {
2020-05-04 12:56:39 +03:00
return {
user: {
email: { required: requiredIf(() => this.accountActivationRequired) },
username: { required },
fullname: { required },
password: { required },
confirm: {
required,
2026-01-06 16:22:52 +02:00
sameAs: sameAs(this.user.password),
},
2023-01-22 11:15:52 -05:00
birthday: {
required: requiredIf(() => this.birthdayRequired),
2026-01-06 16:22:52 +02:00
maxValue: (value) => {
return (
!this.birthdayRequired ||
new Date(value).getTime() <= this.birthdayMin.getTime()
)
},
2023-01-22 11:15:52 -05:00
},
reason: { required: requiredIf(() => this.accountApprovalRequired) },
2026-01-06 16:22:52 +02:00
language: {},
},
}
},
2026-01-06 16:22:52 +02:00
created() {
if ((!this.registrationOpen && !this.token) || this.signedIn) {
2019-07-05 10:02:14 +03:00
this.$router.push({ name: 'root' })
}
2018-12-15 03:05:47 +03:00
2018-12-16 22:47:52 +03:00
this.setCaptcha()
},
computed: {
2026-01-06 16:22:52 +02:00
token() {
return this.$route.params.token
},
bioPlaceholder() {
return this.replaceNewlines(this.$t('registration.bio_placeholder'))
},
2026-01-06 16:22:52 +02:00
reasonPlaceholder() {
return this.replaceNewlines(this.$t('registration.reason_placeholder'))
2019-03-18 10:35:13 -04:00
},
2026-01-06 16:22:52 +02:00
birthdayMin() {
2023-01-22 11:15:52 -05:00
const minAge = this.birthdayMinAge
const today = new Date()
today.setUTCMilliseconds(0)
today.setUTCSeconds(0)
today.setUTCMinutes(0)
today.setUTCHours(0)
const minDate = new Date()
minDate.setTime(today.getTime() - minAge * DAY)
return minDate
},
2026-01-06 16:22:52 +02:00
birthdayMinAttr() {
return this.birthdayMin.toJSON().replace(/T.+$/, '')
},
2026-01-06 16:22:52 +02:00
birthdayMinFormatted() {
const browserLocale = localeService.internalToBrowserLocale(
this.$i18n.locale,
)
return (
this.user.birthday &&
new Date(Date.parse(this.birthdayMin)).toLocaleDateString(
browserLocale,
{ timeZone: 'UTC', day: 'numeric', month: 'long', year: 'numeric' },
)
)
2023-01-22 11:15:52 -05:00
},
...mapPiniaState(useInstanceStore, {
registrationOpen: (store) => store.registrationOpen,
embeddedToS: (store) => store.embeddedToS,
termsOfService: (store) => store.tos,
accountActivationRequired: (store) => store.accountActivationRequired,
accountApprovalRequired: (store) => store.accountApprovalRequired,
birthdayRequired: (store) => store.birthdayRequired,
birthdayMinAge: (store) => store.birthdayMinAge,
}),
...mapState({
signedIn: (state) => !!state.users.currentUser,
isPending: (state) => state.users.signUpPending,
serverValidationErrors: (state) => state.users.signUpErrors,
signUpNotice: (state) => state.users.signUpNotice,
hasSignUpNotice: (state) => !!state.users.signUpNotice.message,
2026-01-06 16:22:52 +02:00
}),
},
2017-04-15 18:12:23 +02:00
methods: {
...mapActions(['signUp', 'getCaptcha']),
2026-01-06 16:22:52 +02:00
async submit() {
2017-04-15 18:12:23 +02:00
this.user.nickname = this.user.username
2018-08-05 10:01:38 +03:00
this.user.token = this.token
2018-12-16 20:55:09 +03:00
this.user.captcha_solution = this.captcha.solution
this.user.captcha_token = this.captcha.token
this.user.captcha_answer_data = this.captcha.answer_data
if (this.user.language) {
2026-01-06 16:22:52 +02:00
this.user.language = localeService.internalToBackendLocaleMulti(
this.user.language.filter((k) => k),
)
}
2022-03-29 16:08:57 +03:00
this.v$.$touch()
2022-03-29 16:08:57 +03:00
if (!this.v$.$invalid) {
try {
const status = await this.signUp(this.user)
if (status === 'ok') {
this.$router.push({ name: 'friends' })
}
// If status is not 'ok' (i.e. it needs further actions to be done
// before you can login), display sign up notice, do not switch anywhere
} catch (error) {
console.warn('Registration failed: ', error)
this.setCaptcha()
}
}
2018-12-16 22:47:52 +03:00
},
2026-01-06 16:22:52 +02:00
setCaptcha() {
this.getCaptcha().then((cpt) => {
this.captcha = cpt
})
},
2026-01-06 16:22:52 +02:00
replaceNewlines(str) {
return str.replace(/\s*\n\s*/g, ' \n')
2026-01-06 16:22:52 +02:00
},
},
2017-04-15 18:12:23 +02:00
}
export default registration