mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 12:42:25 -05:00 
			
		
		
		
	refactor oauth registration flow
This commit is contained in:
		
					parent
					
						
							
								0bcf63b2cf
							
						
					
				
			
			
				commit
				
					
						11ac9e2fd2
					
				
			
		
					 1 changed files with 33 additions and 23 deletions
				
			
		|  | @ -12,19 +12,20 @@ module.exports = function Auth({setOauth}) { | ||||||
| 		// check if current domain runs an instance
 | 		// check if current domain runs an instance
 | ||||||
| 		let thisUrl = new URL(window.location.origin); | 		let thisUrl = new URL(window.location.origin); | ||||||
| 		thisUrl.pathname = "/api/v1/instance"; | 		thisUrl.pathname = "/api/v1/instance"; | ||||||
| 		fetch(thisUrl.href) | 		Promise.try(() => { | ||||||
| 			.then((res) => res.json()) | 			return fetch(thisUrl.href); | ||||||
| 			.then((json) => { | 		}).then((res) => { | ||||||
| 				if (json && json.uri) { | 			if (res.status == 200) { | ||||||
| 					if (isStillMounted) { | 				return res.json(); | ||||||
| 						setInstance(json.uri); | 			} | ||||||
| 					} | 		}).then((json) => { | ||||||
| 				} | 			if (json && json.uri && isStillMounted) { | ||||||
| 			}) | 				setInstance(json.uri); | ||||||
| 			.catch((e) => { | 			} | ||||||
| 				console.error("caught", e); | 		}).catch((e) => { | ||||||
| 				// no instance here
 | 			console.log("error checking instance response:", e); | ||||||
| 			}); | 		}); | ||||||
|  | 
 | ||||||
| 		return () => { | 		return () => { | ||||||
| 			// cleanup function
 | 			// cleanup function
 | ||||||
| 			isStillMounted = false; | 			isStillMounted = false; | ||||||
|  | @ -32,18 +33,27 @@ module.exports = function Auth({setOauth}) { | ||||||
| 	}, []); | 	}, []); | ||||||
| 
 | 
 | ||||||
| 	function doAuth() { | 	function doAuth() { | ||||||
| 		let oauth = oauthLib({ |  | ||||||
| 			instance: instance, |  | ||||||
| 			client_name: "GoToSocial Admin Panel", |  | ||||||
| 			scope: ["admin"], |  | ||||||
| 			website: window.location.href |  | ||||||
| 		}); |  | ||||||
| 		setOauth(oauth); |  | ||||||
| 
 |  | ||||||
| 		return Promise.try(() => { | 		return Promise.try(() => { | ||||||
| 			return oauth.register(); | 			return new URL(instance); | ||||||
| 		}).then(() => { | 		}).catch(TypeError, () => { | ||||||
|  | 			return new URL(`https://${instance}`); | ||||||
|  | 		}).then((parsedURL) => { | ||||||
|  | 			let url = parsedURL.toString(); | ||||||
|  | 			let oauth = oauthLib({ | ||||||
|  | 				instance: url, | ||||||
|  | 				client_name: "GoToSocial Admin Panel", | ||||||
|  | 				scope: ["admin"], | ||||||
|  | 				website: window.location.href | ||||||
|  | 			}); | ||||||
|  | 			setOauth(oauth); | ||||||
|  | 			setInstance(url); | ||||||
|  | 			return oauth.register().then(() => { | ||||||
|  | 				return oauth; | ||||||
|  | 			}); | ||||||
|  | 		}).then((oauth) => { | ||||||
| 			return oauth.authorize(); | 			return oauth.authorize(); | ||||||
|  | 		}).catch((e) => { | ||||||
|  | 			console.log("error authenticating:", e); | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue