Skip to content

Commit

Permalink
avniproject/avni-server#397 | User change own password on Keycloak
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Apr 4, 2023
1 parent 7c5f04a commit d9cb762
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/openchs-android/src/framework/http/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ let _post = (endpoint, file, fetchWithoutTimeout, bypassAuth = false) => {
return params.then((headers) => fetchFactory(endpoint, "POST", headers, fetchWithoutTimeout))
};

let _put = (endpoint, body, fetchWithoutTimeout, bypassAuth = false) => {
const params = _addAuthIfRequired(makeRequest("json", {body: JSON.stringify(body)}), bypassAuth);
General.logDebug('Requests', `PUT: ${endpoint}`);
return params.then((headers) => {
return fetchFactory(endpoint, "PUT", headers, fetchWithoutTimeout)
})
};

export let post = _post;

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

export let putJSON = (endpoint, body, fetchWithoutTimeout = false, bypassAuth = false) => {
return _put(endpoint, body, fetchWithoutTimeout, bypassAuth)
};

export let postUrlFormEncoded = (endpoint, body) => {
const formBody = new URLSearchParams(body).toString();
return fetchFactory(endpoint, "POST", {headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*'}, body: formBody}, true)
Expand Down
15 changes: 12 additions & 3 deletions packages/openchs-android/src/service/KeycloakAuthService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Service from "../framework/bean/Service";
import { postUrlFormEncoded } from "../framework/http/requests";
import { postUrlFormEncoded, putJSON } from "../framework/http/requests";
import General from "../utility/General";
import AuthenticationError, { NO_USER } from "./AuthenticationError";
import BaseAuthProviderService from "./BaseAuthProviderService";
Expand Down Expand Up @@ -79,8 +79,17 @@ class KeycloakAuthService extends BaseAuthProviderService {
// return new AuthenticationError(NO_USER, "No user or needs login");
}

async changePassword() {
throw new Error("Not implemented for Keycloak");
async changePassword(oldPassword, newPassword) {
const settings = this.settingsService.getSettings();
this.authenticate(settings.userId, oldPassword)
.then(async (authResult) => {
if (authResult.status === 'LOGIN_SUCCESS') {
const changePasswordEndpoint = `${settings.serverURL}/user/changePassword`;
await putJSON(changePasswordEndpoint, {newPassword})
.catch((e) => e);
} else throw new Error('Unable to authenticate user');
})
.catch(e => e);
}

async logout() {
Expand Down

0 comments on commit d9cb762

Please sign in to comment.