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

90 lines
2.5 KiB
JavaScript
Raw Normal View History

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