-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from vnma0/pwd-change
Integrating the ability to change password
- Loading branch information
Showing
3 changed files
with
64 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/app/globalStatusBar/userSetting/stub/passwordChange.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @name constructRequestBody | ||
* @description Create an `URLSearchParams` object suitable for sending as credential changing request | ||
* @param {String} password - Self-explanatory | ||
* @param {String} newPassword - Self-explanatory | ||
* @returns {URLSearchParams} - new `URLSearchParams` with given parameters | ||
*/ | ||
|
||
function constructRequestBody(password, newPassword) { | ||
let out = new URLSearchParams(); | ||
out.append('password', password); | ||
out.append('newPassword', newPassword); | ||
return out; | ||
} | ||
|
||
/** | ||
* @name passwordChange | ||
* @description Change password. Reload the page upon successful response. | ||
* @param {String} userId - user ID to change password | ||
* @param {String} password - old password | ||
* @param {String} newPassword - new password | ||
* @param {Function} func - Function to call upon operation finishing. | ||
* Signature: `function (success : Boolean)`, with `success` as `Response.ok`. | ||
* @returns {Promise<Response>} | ||
*/ | ||
|
||
export default async function passwordChange (userId = window.hestia.user.userId, | ||
password, newPassword, func) { | ||
return fetch(`/api/users/${userId}/password`, { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
mode: 'cors', | ||
body: constructRequestBody(password, newPassword) | ||
}).then(res => { | ||
// console.log(res.text()) | ||
if (res.ok) | ||
// password successfully changed | ||
window.location.reload() | ||
// reload, because the server will sign out automatically | ||
else throw new Error('Request to change password failed') | ||
}).then((okay) => { | ||
if (typeof func === 'function') | ||
func(okay); | ||
}).catch(err => { | ||
if (typeof window.hestia.pushNotification === 'function') | ||
window.hestia.pushNotification( | ||
'Failed to change your password' | ||
) | ||
}) | ||
} |