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

100 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-03-16 21:02:44 +01:00
import useVuelidate from '@vuelidate/core'
import { required, requiredIf, sameAs } from '@vuelidate/validators'
import { mapActions, mapState } from 'vuex'
import InterfaceLanguageSwitcher from '../interface_language_switcher/interface_language_switcher.vue'
import localeService from '../../services/locale/locale.service.js'
2017-04-15 18:12:23 +02:00
const registration = {
2022-03-16 21:02:44 +01:00
setup () { return { v$: useVuelidate() } },
2017-04-15 18:12:23 +02:00
data: () => ({
user: {
email: '',
fullname: '',
username: '',
password: '',
confirm: '',
reason: '',
language: ''
2018-12-15 01:05:47 +01:00
},
captcha: {}
2017-04-15 18:12:23 +02:00
}),
components: {
InterfaceLanguageSwitcher
},
2020-05-04 11:56:39 +02:00
validations () {
return {
user: {
email: { required: requiredIf(() => this.accountActivationRequired) },
username: { required },
fullname: { required },
password: { required },
confirm: {
required,
2022-03-29 15:08:57 +02:00
sameAs: sameAs(this.user.password)
},
reason: { required: requiredIf(() => this.accountApprovalRequired) },
language: {}
}
}
},
created () {
if ((!this.registrationOpen && !this.token) || this.signedIn) {
2019-07-05 09:02:14 +02:00
this.$router.push({ name: 'root' })
}
2018-12-15 01:05:47 +01:00
2018-12-16 20:47:52 +01:00
this.setCaptcha()
},
computed: {
token () { return this.$route.params.token },
2019-03-18 15:35:13 +01:00
bioPlaceholder () {
return this.replaceNewlines(this.$t('registration.bio_placeholder'))
},
reasonPlaceholder () {
return this.replaceNewlines(this.$t('registration.reason_placeholder'))
2019-03-18 15:35:13 +01:00
},
...mapState({
registrationOpen: (state) => state.instance.registrationOpen,
signedIn: (state) => !!state.users.currentUser,
isPending: (state) => state.users.signUpPending,
serverValidationErrors: (state) => state.users.signUpErrors,
2020-05-04 11:56:39 +02:00
termsOfService: (state) => state.instance.tos,
accountActivationRequired: (state) => state.instance.accountActivationRequired,
accountApprovalRequired: (state) => state.instance.accountApprovalRequired
})
},
2017-04-15 18:12:23 +02:00
methods: {
...mapActions(['signUp', 'getCaptcha']),
async submit () {
2017-04-15 18:12:23 +02:00
this.user.nickname = this.user.username
2018-08-05 09:01:38 +02:00
this.user.token = this.token
2018-12-16 18:55:09 +01: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) {
this.user.language = localeService.internalToBackendLocale(this.user.language)
}
2022-03-29 15:08:57 +02:00
this.v$.$touch()
2022-03-29 15:08:57 +02:00
if (!this.v$.$invalid) {
try {
await this.signUp(this.user)
2019-07-05 09:02:14 +02:00
this.$router.push({ name: 'friends' })
} catch (error) {
console.warn('Registration failed: ', error)
this.setCaptcha()
}
}
2018-12-16 20:47:52 +01:00
},
2018-12-16 20:55:11 +01:00
setCaptcha () {
2018-12-16 20:47:52 +01:00
this.getCaptcha().then(cpt => { this.captcha = cpt })
},
replaceNewlines (str) {
return str.replace(/\s*\n\s*/g, ' \n')
2017-04-15 18:12:23 +02:00
}
}
}
export default registration