|
| 1 | +import React, { SyntheticEvent, useState } from "react"; |
| 2 | +import Modal from "@components/Modal"; |
| 3 | +import { ErrorCode } from "@lib/auth"; |
| 4 | + |
| 5 | +const errorMessages: { [key: string]: string } = { |
| 6 | + [ErrorCode.IncorrectPassword]: "Current password is incorrect", |
| 7 | + [ErrorCode.NewPasswordMatchesOld]: |
| 8 | + "New password matches your old password. Please choose a different password.", |
| 9 | +}; |
| 10 | + |
| 11 | +const ChangePasswordSection = () => { |
| 12 | + const [oldPassword, setOldPassword] = useState(""); |
| 13 | + const [newPassword, setNewPassword] = useState(""); |
| 14 | + const [successModalOpen, setSuccessModalOpen] = useState(false); |
| 15 | + const [errorMessage, setErrorMessage] = useState<string | null>(null); |
| 16 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 17 | + |
| 18 | + const closeSuccessModal = () => { |
| 19 | + setSuccessModalOpen(false); |
| 20 | + }; |
| 21 | + |
| 22 | + async function changePasswordHandler(e: SyntheticEvent) { |
| 23 | + e.preventDefault(); |
| 24 | + |
| 25 | + if (isSubmitting) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + setIsSubmitting(true); |
| 30 | + setErrorMessage(null); |
| 31 | + |
| 32 | + try { |
| 33 | + const response = await fetch("/api/auth/changepw", { |
| 34 | + method: "PATCH", |
| 35 | + body: JSON.stringify({ oldPassword, newPassword }), |
| 36 | + headers: { |
| 37 | + "Content-Type": "application/json", |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + if (response.status === 200) { |
| 42 | + setOldPassword(""); |
| 43 | + setNewPassword(""); |
| 44 | + setSuccessModalOpen(true); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const body = await response.json(); |
| 49 | + setErrorMessage(errorMessages[body.error] || "Something went wrong. Please try again"); |
| 50 | + } catch (err) { |
| 51 | + console.error("Error changing password", err); |
| 52 | + setErrorMessage("Something went wrong. Please try again"); |
| 53 | + } finally { |
| 54 | + setIsSubmitting(false); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + <div className="mt-6"> |
| 61 | + <h2 className="text-lg leading-6 font-medium text-gray-900">Change Password</h2> |
| 62 | + </div> |
| 63 | + <form className="divide-y divide-gray-200 lg:col-span-9" onSubmit={changePasswordHandler}> |
| 64 | + <div className="py-6 lg:pb-8"> |
| 65 | + <div className="flex"> |
| 66 | + <div className="w-1/2 mr-2"> |
| 67 | + <label htmlFor="current_password" className="block text-sm font-medium text-gray-700"> |
| 68 | + Current Password |
| 69 | + </label> |
| 70 | + <div className="mt-1"> |
| 71 | + <input |
| 72 | + type="password" |
| 73 | + value={oldPassword} |
| 74 | + onInput={(e) => setOldPassword(e.currentTarget.value)} |
| 75 | + name="current_password" |
| 76 | + id="current_password" |
| 77 | + required |
| 78 | + className="shadow-sm focus:ring-black focus:border-black block w-full sm:text-sm border-gray-300 rounded-sm" |
| 79 | + placeholder="Your old password" |
| 80 | + /> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <div className="w-1/2 ml-2"> |
| 84 | + <label htmlFor="new_password" className="block text-sm font-medium text-gray-700"> |
| 85 | + New Password |
| 86 | + </label> |
| 87 | + <div className="mt-1"> |
| 88 | + <input |
| 89 | + type="password" |
| 90 | + name="new_password" |
| 91 | + id="new_password" |
| 92 | + value={newPassword} |
| 93 | + required |
| 94 | + onInput={(e) => setNewPassword(e.currentTarget.value)} |
| 95 | + className="shadow-sm focus:ring-black focus:border-black block w-full sm:text-sm border-gray-300 rounded-sm" |
| 96 | + placeholder="Your super secure new password" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + {errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>} |
| 102 | + <div className="py-8 flex justify-end"> |
| 103 | + <button |
| 104 | + type="submit" |
| 105 | + className="ml-2 bg-neutral-900 border border-transparent rounded-sm shadow-sm py-2 px-4 inline-flex justify-center text-sm font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-black"> |
| 106 | + Save |
| 107 | + </button> |
| 108 | + </div> |
| 109 | + <hr className="mt-4" /> |
| 110 | + </div> |
| 111 | + </form> |
| 112 | + <Modal |
| 113 | + heading="Password updated successfully" |
| 114 | + description="Your password has been successfully changed." |
| 115 | + open={successModalOpen} |
| 116 | + handleClose={closeSuccessModal} |
| 117 | + /> |
| 118 | + </> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export default ChangePasswordSection; |
0 commit comments