@@ -4,29 +4,64 @@ import changePassword from "../AccountForm/accountForm.module.css";
44import { setFormElement } from "../reactExtensions" ;
55
66export default function ChangePassword ( props ) {
7- const [ currentPassword , setCurrentPassword ] = useState ( "" ) ;
7+ const [ currentPassword , setCurrentPassword ] = useState ( "" ) ;
88 const [ newPassword , setNewPassword ] = useState ( "" ) ;
99 const [ confirmNewPassword , setConfirmNewPassword ] = useState ( "" ) ;
1010
1111 const handleChangePasswordClick = e => {
1212 e . preventDefault ( ) ;
1313
14- fetch ( "https://api.borumtech.com/api/login" , {
15- method : "PUT" ,
16- headers : {
17- "content-type" : "application/x-www-form-urlencoded" ,
18- "authorization" : "Basic " + localStorage . getItem ( "apiKey" )
19- } ,
20- body : `password=${ newPassword } `
21- } ) . then ( response => response . json ( ) ) . then ( response => {
22- if ( ! response . ok ) {
23- alert ( "An error occurred and the password could not be changed." ) ;
24- }
25- } )
14+ const userEmail = localStorage . getItem ( "email" ) ;
15+
16+ const verifyCurrentPassword = ( ) =>
17+ fetch ( "https://api.borumtech.com/api/login" , {
18+ method : "POST" ,
19+ headers : {
20+ "content-type" : "application/x-www-form-urlencoded" ,
21+ } ,
22+ body : `email=${ userEmail } &password=${ currentPassword } ` ,
23+ } ) . then ( response => {
24+ if ( response . ok ) return response . json ( ) ;
25+ throw new Error ( response . json ( ) ) ;
26+ } ) ;
27+
28+ const requestPasswordChange = ( ) =>
29+ fetch ( "https://api.borumtech.com/api/login" , {
30+ method : "PUT" ,
31+ headers : {
32+ "content-type" : "application/x-www-form-urlencoded" ,
33+ authorization : "Basic " + localStorage . getItem ( "apiKey" ) ,
34+ } ,
35+ body : `password=${ newPassword } ` ,
36+ } )
37+ . then ( response => {
38+ if ( response . ok ) {
39+ return response . json ( ) ;
40+ }
41+ } )
42+ . then ( response => {
43+ if ( ! response . ok ) {
44+ alert (
45+ "An error occurred and the password could not be changed."
46+ ) ;
47+ }
48+ } ) ;
49+
50+ verifyCurrentPassword ( )
51+ . then ( requestPasswordChange )
52+ . catch ( ( ) => {
53+ alert (
54+ "The current password is not correct. If you do not remember your current password, log out and click 'Forgot Password'"
55+ ) ;
56+ } ) ;
2657 } ;
2758
2859 return (
29- < form style = { { width : "auto" } } onSubmit = { handleChangePasswordClick } className = { changePassword . form } >
60+ < form
61+ style = { { width : "auto" } }
62+ onSubmit = { handleChangePasswordClick }
63+ className = { changePassword . form }
64+ >
3065 < FormField
3166 labelContent = "Current Password"
3267 label = "currpass"
0 commit comments