Skip to content

Commit d9cb762

Browse files
committed
avniproject/avni-server#397 | User change own password on Keycloak
1 parent 7c5f04a commit d9cb762

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/openchs-android/src/framework/http/requests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ let _post = (endpoint, file, fetchWithoutTimeout, bypassAuth = false) => {
8484
return params.then((headers) => fetchFactory(endpoint, "POST", headers, fetchWithoutTimeout))
8585
};
8686

87+
let _put = (endpoint, body, fetchWithoutTimeout, bypassAuth = false) => {
88+
const params = _addAuthIfRequired(makeRequest("json", {body: JSON.stringify(body)}), bypassAuth);
89+
General.logDebug('Requests', `PUT: ${endpoint}`);
90+
return params.then((headers) => {
91+
return fetchFactory(endpoint, "PUT", headers, fetchWithoutTimeout)
92+
})
93+
};
94+
8795
export let post = _post;
8896

8997
export let get = (endpoint, bypassAuth = false) => {
@@ -94,6 +102,10 @@ export let getJSON = (endpoint, bypassAuth = false) => {
94102
return _get(endpoint, bypassAuth);
95103
};
96104

105+
export let putJSON = (endpoint, body, fetchWithoutTimeout = false, bypassAuth = false) => {
106+
return _put(endpoint, body, fetchWithoutTimeout, bypassAuth)
107+
};
108+
97109
export let postUrlFormEncoded = (endpoint, body) => {
98110
const formBody = new URLSearchParams(body).toString();
99111
return fetchFactory(endpoint, "POST", {headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*'}, body: formBody}, true)

packages/openchs-android/src/service/KeycloakAuthService.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Service from "../framework/bean/Service";
2-
import { postUrlFormEncoded } from "../framework/http/requests";
2+
import { postUrlFormEncoded, putJSON } from "../framework/http/requests";
33
import General from "../utility/General";
44
import AuthenticationError, { NO_USER } from "./AuthenticationError";
55
import BaseAuthProviderService from "./BaseAuthProviderService";
@@ -79,8 +79,17 @@ class KeycloakAuthService extends BaseAuthProviderService {
7979
// return new AuthenticationError(NO_USER, "No user or needs login");
8080
}
8181

82-
async changePassword() {
83-
throw new Error("Not implemented for Keycloak");
82+
async changePassword(oldPassword, newPassword) {
83+
const settings = this.settingsService.getSettings();
84+
this.authenticate(settings.userId, oldPassword)
85+
.then(async (authResult) => {
86+
if (authResult.status === 'LOGIN_SUCCESS') {
87+
const changePasswordEndpoint = `${settings.serverURL}/user/changePassword`;
88+
await putJSON(changePasswordEndpoint, {newPassword})
89+
.catch((e) => e);
90+
} else throw new Error('Unable to authenticate user');
91+
})
92+
.catch(e => e);
8493
}
8594

8695
async logout() {

0 commit comments

Comments
 (0)