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

108 lines
3.3 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'
import InterfaceLanguageSwitcher from '../interface_language_switcher/interface_language_switcher.vue'
import localeService from '../../services/locale/locale.service.js'
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: '',
language: ''
2018-12-15 00:05:47 +00:00
},
captcha: {}
2017-04-15 16:12:23 +00:00
}),
components: {
InterfaceLanguageSwitcher
},
2020-05-04 09:56:39 +00:00
validations () {
return {
user: {
email: { required: requiredIf(() => this.accountActivationRequired) },
username: { required },
fullname: { required },
password: { required },
confirm: {
required,
2022-03-29 13:08:57 +00:00
sameAs: sameAs(this.user.password)
},
reason: { required: requiredIf(() => this.accountApprovalRequired) },
language: {}
}
}
},
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
if (this.user.language) {
this.user.language = localeService.internalToBackendLocale(this.user.language)
}
2022-03-29 13:08:57 +00:00
this.v$.$touch()
2022-03-29 13:08:57 +00:00
if (!this.v$.$invalid) {
try {
const data = await this.signUp(this.user)
if (data.me) {
this.$router.push({ name: 'friends' })
} else if (data.identifier === 'awaiting_approval') {
this.$router.push({ name: 'registration-request-sent' })
} else if (data.identifier === 'missing_confirmed_email') {
this.$router.push({ name: 'awaiting-email-confirmation' })
} else {
console.warn('Unknown response from sign up', data)
}
} 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