From e7a2a7267dbd8a4ee3d266d22249459d028569d6 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Wed, 22 May 2019 19:13:41 +0300 Subject: Proper clientId/secret/token caching, MastoAPI registration --- src/boot/after_store.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/boot/after_store.js') diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 603de348..caaede59 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -3,6 +3,8 @@ import VueRouter from 'vue-router' import routes from './routes' import App from '../App.vue' import { windowWidth } from '../services/window_utils/window_utils' +import { getOrCreateApp, getClientToken } from '../services/new_api/oauth.js' +import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' const getStatusnetConfig = async ({ store }) => { try { @@ -188,6 +190,17 @@ const getCustomEmoji = async ({ store }) => { } } +const getAppSecret = async ({ store }) => { + const { state, commit } = store + const { oauth, instance } = state + return getOrCreateApp({ ...oauth, instance: instance.server, commit }) + .then((app) => getClientToken({ ...app, instance: instance.server })) + .then((token) => { + commit('setClientToken', token.access_token) + commit('setBackendInteractor', backendInteractorService(store.getters.getToken())) + }) +} + const getNodeInfo = async ({ store }) => { try { const res = await window.fetch('/nodeinfo/2.0.json') @@ -228,7 +241,7 @@ const setConfig = async ({ store }) => { const apiConfig = configInfos[0] const staticConfig = configInfos[1] - await setSettings({ store, apiConfig, staticConfig }) + await setSettings({ store, apiConfig, staticConfig }).then(getAppSecret({ store })) } const checkOAuthToken = async ({ store }) => { -- cgit v1.2.3-70-g09d2 From af75c6d1ea392477c647708dcd0e712c514a1b60 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 13 Jun 2019 00:39:51 +0300 Subject: No longer sending extra data, renamed some properties --- src/boot/after_store.js | 4 ++-- src/components/login_form/login_form.js | 6 ++++-- src/components/oauth_callback/oauth_callback.js | 4 +++- src/modules/oauth.js | 10 +++++----- 4 files changed, 14 insertions(+), 10 deletions(-) (limited to 'src/boot/after_store.js') diff --git a/src/boot/after_store.js b/src/boot/after_store.js index caaede59..4bcd1fb5 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -246,9 +246,9 @@ const setConfig = async ({ store }) => { const checkOAuthToken = async ({ store }) => { return new Promise(async (resolve, reject) => { - if (store.state.oauth.token) { + if (store.state.oauth.userToken) { try { - await store.dispatch('loginUser', store.state.oauth.token) + await store.dispatch('loginUser', store.state.oauth.userToken) } catch (e) { console.log(e) } diff --git a/src/components/login_form/login_form.js b/src/components/login_form/login_form.js index 0097e18a..7d49fade 100644 --- a/src/components/login_form/login_form.js +++ b/src/components/login_form/login_form.js @@ -11,8 +11,9 @@ const LoginForm = { }, methods: { oAuthLogin () { + const { clientId } = this.$store.state.oauth const data = { - ...this.$store.state.oauth, + clientId, instance: this.$store.state.instance.server, commit: this.$store.commit } @@ -21,8 +22,9 @@ const LoginForm = { .then((app) => { oauthApi.login({ ...app, ...data }) }) }, submit () { + const { clientId } = this.$store.state.oauth const data = { - ...this.$store.state.oauth, + clientId, instance: this.$store.state.instance.server, commit: this.$store.commit } diff --git a/src/components/oauth_callback/oauth_callback.js b/src/components/oauth_callback/oauth_callback.js index 48ddd10d..2c6ca235 100644 --- a/src/components/oauth_callback/oauth_callback.js +++ b/src/components/oauth_callback/oauth_callback.js @@ -4,8 +4,10 @@ const oac = { props: ['code'], mounted () { if (this.code) { + const { clientId } = this.$store.state.oauth + oauth.getToken({ - ...this.$store.state.oauth, + clientId, instance: this.$store.state.instance.server, code: this.code }).then((result) => { diff --git a/src/modules/oauth.js b/src/modules/oauth.js index 242e29c3..4b233b21 100644 --- a/src/modules/oauth.js +++ b/src/modules/oauth.js @@ -2,8 +2,8 @@ const oauth = { state: { clientId: false, clientSecret: false, - token: false, - clientToken: false + appToken: false, + userToken: false }, mutations: { setClientData (state, { clientId, clientSecret }) { @@ -11,15 +11,15 @@ const oauth = { state.clientSecret = clientSecret }, setClientToken (state, token) { - state.clientToken = token + state.appToken = token }, setToken (state, token) { - state.token = token + state.userToken = token } }, getters: { getToken: state => () => { - return state.token || state.clientToken + return state.userToken || state.appToken } } } -- cgit v1.2.3-70-g09d2 From dba8d8910cd390d059dfa0827156fa350ed1108c Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 13 Jun 2019 10:00:06 +0300 Subject: fix --- src/boot/after_store.js | 2 +- src/modules/oauth.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'src/boot/after_store.js') diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 3530c3a9..77a5e976 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -248,7 +248,7 @@ const checkOAuthToken = async ({ store }) => { return new Promise(async (resolve, reject) => { if (store.state.oauth.userToken) { try { - await store.dispatch('loginUser', store.state.oauth.userToken) + await store.dispatch('loginUser', store.getters.getUserToken()) } catch (e) { console.log(e) } diff --git a/src/modules/oauth.js b/src/modules/oauth.js index 77ad2fa8..c87e4777 100644 --- a/src/modules/oauth.js +++ b/src/modules/oauth.js @@ -29,6 +29,11 @@ const oauth = { // state.token is userToken with older name, coming from persistent state // added here for smoother transition, otherwise user will be logged out return state.userToken || state.token || state.appToken + }, + getUserToken: state => () => { + // state.token is userToken with older name, coming from persistent state + // added here for smoother transition, otherwise user will be logged out + return state.userToken || state.token } } } -- cgit v1.2.3-70-g09d2 From 0988065f50f8614846efe762d7703b11a349abbb Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 13 Jun 2019 10:05:22 +0300 Subject: fix --- src/boot/after_store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/boot/after_store.js') diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 77a5e976..7661dbe6 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -246,7 +246,7 @@ const setConfig = async ({ store }) => { const checkOAuthToken = async ({ store }) => { return new Promise(async (resolve, reject) => { - if (store.state.oauth.userToken) { + if (store.getters.getUserToken()) { try { await store.dispatch('loginUser', store.getters.getUserToken()) } catch (e) { -- cgit v1.2.3-70-g09d2 From acbeea59ff196c16e7992c80e56a638ce3a623b6 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 13 Jun 2019 10:11:17 +0300 Subject: rename mutations according to actual property names --- src/boot/after_store.js | 2 +- src/modules/oauth.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/boot/after_store.js') diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 7661dbe6..c271d413 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -196,7 +196,7 @@ const getAppSecret = async ({ store }) => { return getOrCreateApp({ ...oauth, instance: instance.server, commit }) .then((app) => getClientToken({ ...app, instance: instance.server })) .then((token) => { - commit('setClientToken', token.access_token) + commit('setAppToken', token.access_token) commit('setBackendInteractor', backendInteractorService(store.getters.getToken())) }) } diff --git a/src/modules/oauth.js b/src/modules/oauth.js index c87e4777..11cb10fe 100644 --- a/src/modules/oauth.js +++ b/src/modules/oauth.js @@ -17,7 +17,7 @@ const oauth = { state.clientId = clientId state.clientSecret = clientSecret }, - setClientToken (state, token) { + setAppToken (state, token) { state.appToken = token }, setToken (state, token) { -- cgit v1.2.3-70-g09d2