allow multiple instances
This commit is contained in:
		
							parent
							
								
									6b1c047bcd
								
							
						
					
					
						commit
						1d2ed28dd5
					
				
					 3 changed files with 34 additions and 18 deletions
				
			
		|  | @ -20,7 +20,7 @@ | ||||||
|   export default { |   export default { | ||||||
|     oncreate: async function () { |     oncreate: async function () { | ||||||
|       if (process.browser) { |       if (process.browser) { | ||||||
|         let instanceData = this.store.get('currentOauthInstance') |         let instanceData = this.store.get('currentInstance') | ||||||
|         if (!instanceData) { |         if (!instanceData) { | ||||||
|           return |           return | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  | @ -38,13 +38,19 @@ class LocalStorageStore extends Store { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| const store = new LocalStorageStore({ | const store = new LocalStorageStore({ | ||||||
|   instanceNameInSearch: '' |   instanceNameInSearch: '', | ||||||
|  |   currentRegisteredInstance: null, | ||||||
|  |   currentRegisteredInstanceName: '', | ||||||
|  |   currentInstance: null, | ||||||
|  |   loggedInInstances: {}, | ||||||
|  |   loggedInInstancesInOrder: [], | ||||||
|  |   instanceThemes: {} | ||||||
| }) | }) | ||||||
| 
 | 
 | ||||||
| store.compute( | store.compute( | ||||||
|   'isUserLoggedIn', |   'isUserLoggedIn', | ||||||
|   ['currentOauthInstance'], |   ['currentInstance', 'loggedInInstances'], | ||||||
|   currentOauthInstance => !!currentOauthInstance && currentOauthInstance.access_token |   (currentInstance, loggedInInstances) => !!(currentInstance && Object.keys(loggedInInstances).includes(currentInstance)) | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| if (process.browser && process.env.NODE_ENV !== 'production') { | if (process.browser && process.env.NODE_ENV !== 'production') { | ||||||
|  |  | ||||||
|  | @ -66,18 +66,25 @@ | ||||||
|     store: () => store, |     store: () => store, | ||||||
|     methods: { |     methods: { | ||||||
|       onReceivedOauthCode: async function(code) { |       onReceivedOauthCode: async function(code) { | ||||||
|         let instanceData = this.store.get('currentOauthInstance') |         let currentRegisteredInstanceName = this.store.get('currentRegisteredInstanceName') | ||||||
|         instanceData.code = code |         let currentRegisteredInstance = this.store.get('currentRegisteredInstance') | ||||||
|         let response = await (await getAccessTokenFromAuthCode( |         let instanceData = await (await getAccessTokenFromAuthCode( | ||||||
|           instanceData.instanceName, |           currentRegisteredInstanceName, | ||||||
|           instanceData.client_id, |           currentRegisteredInstance.client_id, | ||||||
|           instanceData.client_secret, |           currentRegisteredInstance.client_secret, | ||||||
|           instanceData.code |           code | ||||||
|         )).json() |         )).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({ |         this.store.set({ | ||||||
|           'currentOauthInstance': instanceData, |           'loggedInInstances': loggedInInstances, | ||||||
|           'instanceNameInSearch': '', |           'currentInstance': currentRegisteredInstanceName, | ||||||
|  |           'loggedInInstancesInOrder': loggedInInstancesInOrder | ||||||
|         }) |         }) | ||||||
|         this.store.save() |         this.store.save() | ||||||
|         goto('/') |         goto('/') | ||||||
|  | @ -86,11 +93,14 @@ | ||||||
|         event.preventDefault() |         event.preventDefault() | ||||||
|         let instanceName = this.store.get('instanceNameInSearch') |         let instanceName = this.store.get('instanceNameInSearch') | ||||||
|         instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '') |         instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '') | ||||||
|         let data = await (await registerApplication(instanceName)).json() |         let instanceData = await (await registerApplication(instanceName)).json() | ||||||
|         data.instanceName = instanceName |         // TODO: handle error | ||||||
|         this.store.set({'currentOauthInstance': data}) |         this.store.set({ | ||||||
|  |           currentRegisteredInstanceName: instanceName, | ||||||
|  |           currentRegisteredInstance: instanceData | ||||||
|  |         }) | ||||||
|         this.store.save() |         this.store.save() | ||||||
|         let oauthUrl = generateAuthLink(instanceName, data.client_id) |         let oauthUrl = generateAuthLink(instanceName, instanceData.client_id) | ||||||
|         document.location.href = oauthUrl |         document.location.href = oauthUrl | ||||||
|       }, |       }, | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue