@nevis-security/nevis-mobile-authentication-sdk-react
Version:
React Native plugin for Nevis Mobile Authentication SDK. Supports only mobile.
84 lines (81 loc) • 2.78 kB
JavaScript
/**
* Copyright © 2023-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 { PinChangeErrorConverter } from '../../error/pin/change/PinChangeErrorConverter';
import { NativeEventListener } from '../../event/NativeEventListener';
import NevisMobileAuthenticationSdkReact from '../../MobileAuthenticationSdk';
import { PinChangeMessage } from '../../model/messages/out/PinChangeMessage';
import { Operation } from '../Operation';
/**
* The object that can be used to change the PIN.
*
* Usage example:
* ```ts
* class PinChangerImpl implements PinChanger {
* async changePin(context: PinChangeContext, handler: PinChangeHandler) {
* handler.pins(oldPin, newPin);
* }
* }
*
* [...]
* async changePin({
* client: MobileAuthenticationClient,
* username: string,
* }): Promise<void> {
* await client.operations.pinChange
* .username(username)
* .pinChanger(PinChangerImpl(...))
* .onSuccess(() {
* // handle success
* })
* .onError((error) {
* // handle error
* })
* .execute();
* }
* [...]
* ```
*/
export class PinChange extends Operation {}
export class PinChangeImpl extends PinChange {
username(username) {
this._username = username;
return this;
}
pinChanger(pinChanger) {
this._pinChanger = pinChanger;
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, this._pinChanger, undefined, undefined);
PlatformOperationCache.getInstance().put(operation);
NativeEventListener.getInstance().start(operationId);
const message = new PinChangeMessage(operationId, this._pinChanger !== undefined, this._onSuccess !== undefined, this._onError !== undefined, this._username, this._pinChanger?.pinPolicy);
function finish() {
NativeEventListener.getInstance().stop(operationId);
PlatformOperationCache.getInstance().delete(operationId);
}
return NevisMobileAuthenticationSdkReact.pinChange(message).then(() => {
finish();
this._onSuccess?.();
}).catch(error => {
finish();
const pinChangeError = new PinChangeErrorConverter(error).convert();
this._onError?.(pinChangeError);
});
}
}
//# sourceMappingURL=PinChange.js.map
;