| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | /* | 
					
						
							|  |  |  | 	GoToSocial | 
					
						
							| 
									
										
										
										
											2023-03-12 18:49:06 +01:00
										 |  |  | 	Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | 	SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | 	it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | 	the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | 	(at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | 	but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | 	GNU Affero General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 15:06:17 +01:00
										 |  |  | import { useLogoutMutation, useVerifyCredentialsQuery } from "../../lib/query/login"; | 
					
						
							| 
									
										
										
										
											2023-10-05 16:06:19 +02:00
										 |  |  | import { store } from "../../redux/store"; | 
					
						
							| 
									
										
										
										
											2024-05-05 13:47:22 +02:00
										 |  |  | import React, { ReactNode } from "react"; | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-05 16:06:19 +02:00
										 |  |  | import Login from "./login"; | 
					
						
							|  |  |  | import Loading from "../loading"; | 
					
						
							|  |  |  | import { Error } from "../error"; | 
					
						
							| 
									
										
										
										
											2023-10-17 12:46:06 +02:00
										 |  |  | import { NoArg } from "../../lib/types/query"; | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-05 16:06:19 +02:00
										 |  |  | export function Authorization({ App }) { | 
					
						
							| 
									
										
										
										
											2025-03-17 15:06:17 +01:00
										 |  |  | 	const { current: loginState, expectingRedirect } = store.getState().login; | 
					
						
							|  |  |  | 	const skip = (loginState == "none" || loginState == "loggedout" || expectingRedirect); | 
					
						
							| 
									
										
										
										
											2024-05-05 13:47:22 +02:00
										 |  |  | 	const [ logoutQuery ] = useLogoutMutation(); | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-05 16:06:19 +02:00
										 |  |  | 	const { | 
					
						
							|  |  |  | 		isLoading, | 
					
						
							| 
									
										
										
										
											2024-05-05 13:47:22 +02:00
										 |  |  | 		isFetching, | 
					
						
							| 
									
										
										
										
											2023-10-05 16:06:19 +02:00
										 |  |  | 		isSuccess, | 
					
						
							|  |  |  | 		data: account, | 
					
						
							|  |  |  | 		error, | 
					
						
							| 
									
										
										
										
											2023-10-17 12:46:06 +02:00
										 |  |  | 	} = useVerifyCredentialsQuery(NoArg, { skip: skip }); | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	let showLogin = true; | 
					
						
							| 
									
										
										
										
											2024-05-05 13:47:22 +02:00
										 |  |  | 	let content: ReactNode; | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-05 13:47:22 +02:00
										 |  |  | 	if (isLoading || isFetching) { | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 		showLogin = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-17 12:46:06 +02:00
										 |  |  | 		let loadingInfo = ""; | 
					
						
							| 
									
										
										
										
											2025-03-17 15:06:17 +01:00
										 |  |  | 		if (loginState == "awaitingcallback") { | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 			loadingInfo = "Processing OAUTH callback."; | 
					
						
							| 
									
										
										
										
											2025-03-17 15:06:17 +01:00
										 |  |  | 		} else if (loginState == "loggedin") { | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 			loadingInfo = "Verifying stored login."; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		content = ( | 
					
						
							|  |  |  | 			<div> | 
					
						
							|  |  |  | 				<Loading /> {loadingInfo} | 
					
						
							|  |  |  | 			</div> | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2024-05-05 13:47:22 +02:00
										 |  |  | 	} else if (error !== undefined) { | 
					
						
							| 
									
										
										
										
											2025-02-26 13:04:55 +01:00
										 |  |  | 		// Something went wrong,
 | 
					
						
							|  |  |  | 		// log the user out.
 | 
					
						
							|  |  |  | 		logoutQuery(NoArg); | 
					
						
							| 
									
										
										
										
											2024-05-05 13:47:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 		content = ( | 
					
						
							|  |  |  | 			<div> | 
					
						
							|  |  |  | 				<Error error={error} /> | 
					
						
							|  |  |  | 				You can attempt logging in again below: | 
					
						
							|  |  |  | 			</div> | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 15:06:17 +01:00
										 |  |  | 	if (loginState == "loggedin" && isSuccess) { | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 		return <App account={account} />; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return ( | 
					
						
							|  |  |  | 			<section className="oauth"> | 
					
						
							|  |  |  | 				<h1>GoToSocial Settings</h1> | 
					
						
							|  |  |  | 				{content} | 
					
						
							|  |  |  | 				{showLogin && <Login />} | 
					
						
							|  |  |  | 			</section> | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-10-05 16:06:19 +02:00
										 |  |  | } |