Migrate oauth tokens module to pinia store

This commit is contained in:
Sean King 2025-05-10 22:35:15 -06:00
commit 27f753e8de
No known key found for this signature in database
GPG key ID: 510C52BACD6E7257
7 changed files with 32 additions and 33 deletions

View file

@ -2,6 +2,7 @@ import Importer from 'src/components/importer/importer.vue'
import Exporter from 'src/components/exporter/exporter.vue' import Exporter from 'src/components/exporter/exporter.vue'
import Checkbox from 'src/components/checkbox/checkbox.vue' import Checkbox from 'src/components/checkbox/checkbox.vue'
import { mapState } from 'vuex' import { mapState } from 'vuex'
import { useOAuthTokensStore } from 'src/stores/oauth_tokens'
const DataImportExportTab = { const DataImportExportTab = {
data () { data () {
@ -15,7 +16,7 @@ const DataImportExportTab = {
} }
}, },
created () { created () {
this.$store.dispatch('fetchTokens') useOAuthTokensStore().fetchTokens()
this.fetchBackups() this.fetchBackups()
}, },
components: { components: {

View file

@ -11,6 +11,7 @@ import ProgressButton from 'src/components/progress_button/progress_button.vue'
import withSubscription from 'src/components/../hocs/with_subscription/with_subscription' import withSubscription from 'src/components/../hocs/with_subscription/with_subscription'
import withLoadMore from 'src/components/../hocs/with_load_more/with_load_more' import withLoadMore from 'src/components/../hocs/with_load_more/with_load_more'
import Checkbox from 'src/components/checkbox/checkbox.vue' import Checkbox from 'src/components/checkbox/checkbox.vue'
import { useOAuthTokensStore } from 'src/stores/oauth_tokens'
const BlockList = withLoadMore({ const BlockList = withLoadMore({
fetch: (props, $store) => $store.dispatch('fetchBlocks'), fetch: (props, $store) => $store.dispatch('fetchBlocks'),
@ -39,7 +40,7 @@ const MutesAndBlocks = {
} }
}, },
created () { created () {
this.$store.dispatch('fetchTokens') useOAuthTokensStore().fetchTokens()
this.$store.dispatch('getKnownDomains') this.$store.dispatch('getKnownDomains')
}, },
components: { components: {

View file

@ -2,6 +2,7 @@ import ProgressButton from 'src/components/progress_button/progress_button.vue'
import Checkbox from 'src/components/checkbox/checkbox.vue' import Checkbox from 'src/components/checkbox/checkbox.vue'
import Mfa from './mfa.vue' import Mfa from './mfa.vue'
import localeService from 'src/services/locale/locale.service.js' import localeService from 'src/services/locale/locale.service.js'
import { useOAuthTokensStore } from 'src/stores/oauth_tokens'
const SecurityTab = { const SecurityTab = {
data () { data () {
@ -28,7 +29,7 @@ const SecurityTab = {
} }
}, },
created () { created () {
this.$store.dispatch('fetchTokens') useOAuthTokensStore().fetchTokens()
this.fetchAliases() this.fetchAliases()
}, },
components: { components: {
@ -44,7 +45,7 @@ const SecurityTab = {
return this.$store.state.instance.pleromaBackend return this.$store.state.instance.pleromaBackend
}, },
oauthTokens () { oauthTokens () {
return this.$store.state.oauthTokens.tokens.map(oauthToken => { return useOAuthTokensStore().tokens.map(oauthToken => {
return { return {
id: oauthToken.id, id: oauthToken.id,
appName: oauthToken.app_name, appName: oauthToken.app_name,
@ -151,7 +152,7 @@ const SecurityTab = {
}, },
revokeToken (id) { revokeToken (id) {
if (window.confirm(`${this.$i18n.t('settings.revoke_token')}?`)) { if (window.confirm(`${this.$i18n.t('settings.revoke_token')}?`)) {
this.$store.dispatch('revokeToken', id) useOAuthTokensStore().revokeToken(id)
} }
} }
} }

View file

@ -7,7 +7,6 @@ import config from './config.js'
import profileConfig from './profileConfig.js' import profileConfig from './profileConfig.js'
import adminSettings from './adminSettings.js' import adminSettings from './adminSettings.js'
import authFlow from './auth_flow.js' import authFlow from './auth_flow.js'
import oauthTokens from './oauth_tokens.js'
import drafts from './drafts.js' import drafts from './drafts.js'
import chats from './chats.js' import chats from './chats.js'
@ -21,7 +20,6 @@ export default {
profileConfig, profileConfig,
adminSettings, adminSettings,
authFlow, authFlow,
oauthTokens,
drafts, drafts,
chats chats
} }

View file

@ -1,26 +0,0 @@
const oauthTokens = {
state: {
tokens: []
},
actions: {
fetchTokens ({ rootState, commit }) {
rootState.api.backendInteractor.fetchOAuthTokens().then((tokens) => {
commit('swapTokens', tokens)
})
},
revokeToken ({ rootState, commit, state }, id) {
rootState.api.backendInteractor.revokeOAuthToken({ id }).then((response) => {
if (response.status === 201) {
commit('swapTokens', state.tokens.filter(token => token.id !== id))
}
})
}
},
mutations: {
swapTokens (state, tokens) {
state.tokens = tokens
}
}
}
export default oauthTokens

View file

@ -0,0 +1,24 @@
import { defineStore } from 'pinia'
export const useOAuthTokensStore = defineStore('oauthTokens', {
state: () => ({
tokens: []
}),
actions: {
fetchTokens () {
window.vuex.state.api.backendInteractor.fetchOAuthTokens().then((tokens) => {
this.swapTokens(tokens)
})
},
revokeToken (id) {
window.vuex.state.api.backendInteractor.revokeOAuthToken({ id }).then((response) => {
if (response.status === 201) {
this.swapTokens(this.tokens.filter(token => token.id !== id))
}
})
},
swapTokens (tokens) {
this.tokens = tokens
}
}
});