@nevis-security/nevis-mobile-authentication-sdk-react
Version:
React Native plugin for Nevis Mobile Authentication SDK. Supports only mobile.
86 lines (83 loc) • 2.97 kB
JavaScript
;
/**
* Copyright © 2024 Nevis Security AG. All rights reserved.
*/
import uuid from 'react-native-uuid';
import { UserInteractionPlatformOperationImpl } from "../../cache/operation/UserInteractionPlatformOperation.js";
import { PlatformOperationCache } from "../../cache/PlatformOperationCache.js";
import { PasswordChangeErrorConverter } from "../../error/password/change/PasswordChangeErrorConverter.js";
import { NativeEventListener } from "../../event/NativeEventListener.js";
import NevisMobileAuthenticationSdkReact from "../../MobileAuthenticationSdk.js";
import { PasswordChangeMessage } from "../../model/messages/out/PasswordChangeMessage.js";
import { Operation } from "../Operation.js";
/**
* The object that can be used to change the password.
*
* Usage example:
* ```ts
* class PasswordChangerImpl implements PasswordChanger {
* async changePassword(context: PasswordChangeContext, handler: PasswordChangeHandler) {
* handler.passwords(oldPassword, newPassword);
* }
* }
*
* [...]
* async changePassword({
* client: MobileAuthenticationClient,
* username: string,
* }): Promise<void> {
* await client.operations.passwordChange
* .username(username)
* .passwordChanger(PasswordChangerImpl(...))
* .onSuccess(() {
* // handle success
* })
* .onError((error) {
* // handle error
* })
* .execute();
* }
* [...]
* ```
*
* @group Performing Operations
*/
export class PasswordChange extends Operation {}
export class PasswordChangeImpl extends PasswordChange {
username(username) {
this._username = username;
return this;
}
passwordChanger(passwordChanger) {
this._passwordChanger = passwordChanger;
return this;
}
onSuccess(onSuccess) {
this._onSuccess = onSuccess;
return this;
}
onError(onError) {
this._onError = onError;
return this;
}
async execute() {
const operationId = uuid.v4();
const operation = new UserInteractionPlatformOperationImpl(operationId, undefined, undefined, undefined, undefined, undefined, this._passwordChanger, undefined);
PlatformOperationCache.getInstance().put(operation);
NativeEventListener.getInstance().start(operationId);
const message = new PasswordChangeMessage(operationId, this._passwordChanger !== undefined, this._onSuccess !== undefined, this._onError !== undefined, this._username);
function finish() {
NativeEventListener.getInstance().stop(operationId);
PlatformOperationCache.getInstance().delete(operationId);
}
return NevisMobileAuthenticationSdkReact.passwordChange(message).then(() => {
finish();
this._onSuccess?.();
}).catch(error => {
finish();
const passwordChangeError = new PasswordChangeErrorConverter(error).convert();
this._onError?.(passwordChangeError);
});
}
}
//# sourceMappingURL=PasswordChange.js.map