From 32b9c4a14d0e94907d1ba1e0ac588f3714eded4e Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 10 Aug 2026 13:52:45 +0300 Subject: [PATCH] initial pinia implementation --- src/components/registration/registration.js | 65 +- src/components/registration/registration.vue | 22 +- src/stores/users.js | 671 +++++++++++++++++++ 3 files changed, 735 insertions(+), 23 deletions(-) create mode 100644 src/stores/users.js diff --git a/src/components/registration/registration.js b/src/components/registration/registration.js index ea07c662b..1dbc99859 100644 --- a/src/components/registration/registration.js +++ b/src/components/registration/registration.js @@ -1,14 +1,16 @@ import useVuelidate from '@vuelidate/core' import { required, requiredIf, sameAs } from '@vuelidate/validators' -import { mapState as mapPiniaState } from 'pinia' -import { mapActions, mapState } from 'vuex' +import { mapActions, mapState } from 'pinia' import InterfaceLanguageSwitcher from 'src/components/interface_language_switcher/interface_language_switcher.vue' import TermsOfServicePanel from 'src/components/terms_of_service_panel/terms_of_service_panel.vue' import localeService from '../../services/locale/locale.service.js' import { useInstanceStore } from 'src/stores/instance.js' +import { useOAuthStore } from 'src/stores/oauth.js' +import { useUsersStore } from 'src/stores/users.js' +import { getCaptcha, register } from 'src/api/public.js' import { DAY } from 'src/services/date_utils/date_utils.js' const registration = { @@ -26,6 +28,9 @@ const registration = { reason: '', language: [''], }, + signUpPending: false, + signUpErrors: [], + signUpNotice: {}, captcha: {}, }), components: { @@ -58,7 +63,7 @@ const registration = { } }, created() { - if ((!this.registrationOpen && !this.token) || this.signedIn) { + if ((!this.registrationOpen && !this.token) || this.loggedIn) { this.$router.push({ name: 'root' }) } @@ -100,7 +105,10 @@ const registration = { ) ) }, - ...mapPiniaState(useInstanceStore, { + hasSignUpNotice(state) { + return this.signUpNotice.message + }, + ...mapState(useInstanceStore, { registrationOpen: (store) => store.registrationOpen, embeddedToS: (store) => store.embeddedToS, termsOfService: (store) => store.tos, @@ -109,16 +117,49 @@ const registration = { 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, - }), + ...mapState(useUsersStore, ['loggedIn']), }, methods: { - ...mapActions(['signUp', 'getCaptcha']), + ...mapActions(useUsersStore, ['loginUser']), + getCaptcha(store) { + return getCaptcha({ + credentials: useOAuthStore().token, + }).then(({ data }) => data) + }, + async signUp(userInfo) { + const oauthStore = useOAuthStore() + + this.signUpPending = true + this.signUpErrors = [] + this.signUpNotice = {} + + try { + const token = await oauthStore.ensureAppToken() + const { data } = await register({ + credentials: token, + params: { ...userInfo }, + }) + + if (data.access_token) { + this.signUpPending = false + oauthStore.setToken(data.access_token) + await this.loginUser(data.access_token) + return 'ok' + } else { + // Request succeeded, but user cannot login yet. + this.signUpErrors = [] + this.signUpNotice = data + return 'request_sent' + } + } catch (e) { + const errors = e.message + this.signUpErrors = errors + this.signUpNotice = {} + throw e + } finally { + this.signUpPending = false + } + }, async submit() { this.user.nickname = this.user.username this.user.token = this.token diff --git a/src/components/registration/registration.vue b/src/components/registration/registration.vue index b969a416e..0abcfe656 100644 --- a/src/components/registration/registration.vue +++ b/src/components/registration/registration.vue @@ -28,7 +28,7 @@ @@ -126,7 +126,7 @@ @@ -259,7 +259,7 @@