@nevis-security/nevis-mobile-authentication-sdk-react
Version:
React Native plugin for Nevis Mobile Authentication SDK. Supports only mobile.
84 lines (81 loc) • 2.91 kB
JavaScript
/**
* Copyright © 2024 Nevis Security AG. All rights reserved.
*/
import uuid from 'react-native-uuid';
import { UserInteractionPlatformOperationImpl } from '../../cache/operation/UserInteractionPlatformOperation';
import { PlatformOperationCache } from '../../cache/PlatformOperationCache';
import { PasswordChangeErrorConverter } from '../../error/password/change/PasswordChangeErrorConverter';
import { NativeEventListener } from '../../event/NativeEventListener';
import NevisMobileAuthenticationSdkReact from '../../MobileAuthenticationSdk';
import { PasswordChangeMessage } from '../../model/messages/out/PasswordChangeMessage';
import { Operation } from '../Operation';
/**
* 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();
* }
* [...]
* ```
*/
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
;