| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | import { SyntheticEvent, useState } from "react"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 23:36:43 +00:00
										 |  |  | import Button from "@calcom/ui/Button"; | 
					
						
							|  |  |  | import { Dialog, DialogContent } from "@calcom/ui/Dialog"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | import { ErrorCode } from "@lib/auth"; | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  | import { useLocale } from "@lib/hooks/useLocale"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | import TwoFactorAuthAPI from "./TwoFactorAuthAPI"; | 
					
						
							|  |  |  | import TwoFactorModalHeader from "./TwoFactorModalHeader"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface DisableTwoFactorAuthModalProps { | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  |   /** Called when the user closes the modal without disabling two-factor auth */ | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |   onCancel: () => void; | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  |   /** Called when the user disables two-factor auth */ | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |   onDisable: () => void; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  | const DisableTwoFactorAuthModal = ({ onDisable, onCancel }: DisableTwoFactorAuthModalProps) => { | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |   const [password, setPassword] = useState(""); | 
					
						
							|  |  |  |   const [isDisabling, setIsDisabling] = useState(false); | 
					
						
							|  |  |  |   const [errorMessage, setErrorMessage] = useState<string | null>(null); | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  |   const { t } = useLocale(); | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async function handleDisable(e: SyntheticEvent) { | 
					
						
							|  |  |  |     e.preventDefault(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isDisabling) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     setIsDisabling(true); | 
					
						
							|  |  |  |     setErrorMessage(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const response = await TwoFactorAuthAPI.disable(password); | 
					
						
							|  |  |  |       if (response.status === 200) { | 
					
						
							|  |  |  |         onDisable(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const body = await response.json(); | 
					
						
							|  |  |  |       if (body.error === ErrorCode.IncorrectPassword) { | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |         setErrorMessage(t("incorrect_password")); | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |         setErrorMessage(t("something_went_wrong")); | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |       setErrorMessage(t("something_went_wrong")); | 
					
						
							|  |  |  |       console.error(t("error_disabling_2fa"), e); | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |     } finally { | 
					
						
							|  |  |  |       setIsDisabling(false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <Dialog open={true}> | 
					
						
							|  |  |  |       <DialogContent> | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |         <TwoFactorModalHeader title={t("disable_2fa")} description={t("disable_2fa_recommendation")} /> | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         <form onSubmit={handleDisable}> | 
					
						
							|  |  |  |           <div className="mb-4"> | 
					
						
							|  |  |  |             <label htmlFor="password" className="mt-4 block text-sm font-medium text-gray-700"> | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |               {t("password")} | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |             </label> | 
					
						
							|  |  |  |             <div className="mt-1"> | 
					
						
							|  |  |  |               <input | 
					
						
							|  |  |  |                 type="password" | 
					
						
							|  |  |  |                 name="password" | 
					
						
							|  |  |  |                 id="password" | 
					
						
							|  |  |  |                 required | 
					
						
							|  |  |  |                 value={password} | 
					
						
							|  |  |  |                 onInput={(e) => setPassword(e.currentTarget.value)} | 
					
						
							| 
									
										
										
										
											2022-02-09 00:05:13 +00:00
										 |  |  |                 className="block w-full rounded-sm border-gray-300 shadow-sm focus:border-neutral-900 focus:ring-neutral-900 sm:text-sm" | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |               /> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             {errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>} | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </form> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         <div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse"> | 
					
						
							|  |  |  |           <Button | 
					
						
							|  |  |  |             type="submit" | 
					
						
							| 
									
										
										
										
											2022-02-01 22:17:37 +00:00
										 |  |  |             className="ltr:ml-2 rtl:mr-2" | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |             onClick={handleDisable} | 
					
						
							|  |  |  |             disabled={password.length === 0 || isDisabling}> | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |             {t("disable")} | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |           </Button> | 
					
						
							|  |  |  |           <Button color="secondary" onClick={onCancel}> | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |             {t("cancel")} | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |           </Button> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </DialogContent> | 
					
						
							|  |  |  |     </Dialog> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default DisableTwoFactorAuthModal; |