diff --git a/routes/_components/Timeline.html b/routes/_components/Timeline.html index a11b2b8..7a63919 100644 --- a/routes/_components/Timeline.html +++ b/routes/_components/Timeline.html @@ -20,7 +20,7 @@ export default { oncreate: async function () { if (process.browser) { - let instanceData = this.store.get('currentOauthInstance') + let instanceData = this.store.get('currentInstance') if (!instanceData) { return } diff --git a/routes/_utils/store.js b/routes/_utils/store.js index 700dbec..8d03d58 100644 --- a/routes/_utils/store.js +++ b/routes/_utils/store.js @@ -38,13 +38,19 @@ class LocalStorageStore extends Store { } const store = new LocalStorageStore({ - instanceNameInSearch: '' + instanceNameInSearch: '', + currentRegisteredInstance: null, + currentRegisteredInstanceName: '', + currentInstance: null, + loggedInInstances: {}, + loggedInInstancesInOrder: [], + instanceThemes: {} }) store.compute( 'isUserLoggedIn', - ['currentOauthInstance'], - currentOauthInstance => !!currentOauthInstance && currentOauthInstance.access_token + ['currentInstance', 'loggedInInstances'], + (currentInstance, loggedInInstances) => !!(currentInstance && Object.keys(loggedInInstances).includes(currentInstance)) ) if (process.browser && process.env.NODE_ENV !== 'production') { diff --git a/routes/settings/add-instance.html b/routes/settings/add-instance.html index 5c83977..5e3e0fc 100644 --- a/routes/settings/add-instance.html +++ b/routes/settings/add-instance.html @@ -66,18 +66,25 @@ store: () => store, methods: { onReceivedOauthCode: async function(code) { - let instanceData = this.store.get('currentOauthInstance') - instanceData.code = code - let response = await (await getAccessTokenFromAuthCode( - instanceData.instanceName, - instanceData.client_id, - instanceData.client_secret, - instanceData.code + let currentRegisteredInstanceName = this.store.get('currentRegisteredInstanceName') + let currentRegisteredInstance = this.store.get('currentRegisteredInstance') + let instanceData = await (await getAccessTokenFromAuthCode( + currentRegisteredInstanceName, + currentRegisteredInstance.client_id, + currentRegisteredInstance.client_secret, + code )).json() - instanceData = Object.assign(instanceData, response) + // TODO: handle error + let loggedInInstances = this.store.get('loggedInInstances') + let loggedInInstancesInOrder = this.store.get('loggedInInstancesInOrder') + loggedInInstances[currentRegisteredInstanceName] = instanceData + if (!loggedInInstancesInOrder.includes(currentRegisteredInstanceName)) { + loggedInInstancesInOrder.push(currentRegisteredInstanceName) + } this.store.set({ - 'currentOauthInstance': instanceData, - 'instanceNameInSearch': '', + 'loggedInInstances': loggedInInstances, + 'currentInstance': currentRegisteredInstanceName, + 'loggedInInstancesInOrder': loggedInInstancesInOrder }) this.store.save() goto('/') @@ -86,11 +93,14 @@ event.preventDefault() let instanceName = this.store.get('instanceNameInSearch') instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '') - let data = await (await registerApplication(instanceName)).json() - data.instanceName = instanceName - this.store.set({'currentOauthInstance': data}) + let instanceData = await (await registerApplication(instanceName)).json() + // TODO: handle error + this.store.set({ + currentRegisteredInstanceName: instanceName, + currentRegisteredInstance: instanceData + }) this.store.save() - let oauthUrl = generateAuthLink(instanceName, data.client_id) + let oauthUrl = generateAuthLink(instanceName, instanceData.client_id) document.location.href = oauthUrl }, }