import { handleErrorCodes } from './error'; import { getNativeModule } from './getNativeModule'; import NativeAuthsignalPushModule, { type Spec as AuthsignalPushModuleSpec, } from './NativeAuthsignalPushModule'; import type { AddCredentialInput, AppChallenge, AppCredential, AuthsignalResponse, UpdateChallengeInput, UpdatedAppCredential, } from './types'; interface ConstructorArgs { tenantID: string; baseURL: string; enableLogging: boolean; } const AuthsignalPushModule = getNativeModule( 'AuthsignalPushModule', NativeAuthsignalPushModule ); export class AuthsignalPush { tenantID: string; baseURL: string; enableLogging: boolean; private initialized = false; constructor({ tenantID, baseURL, enableLogging }: ConstructorArgs) { this.tenantID = tenantID; this.baseURL = baseURL; this.enableLogging = enableLogging; } async getCredential(): Promise< AuthsignalResponse > { await this.ensureModuleIsInitialized(); try { const data = (await AuthsignalPushModule.getCredential()) as | AppCredential | undefined; return { data }; } catch (ex) { if (this.enableLogging) { console.log(ex); } return handleErrorCodes(ex); } } async addCredential({ token, requireUserAuthentication = false, keychainAccess, performAttestation, pushToken, }: AddCredentialInput = {}): Promise> { await this.ensureModuleIsInitialized(); try { const data = (await AuthsignalPushModule.addCredential( token ?? null, requireUserAuthentication, keychainAccess ?? null, performAttestation ?? false, pushToken ?? null )) as AppCredential; return { data }; } catch (ex) { if (this.enableLogging) { console.log(ex); } return handleErrorCodes(ex); } } async removeCredential(): Promise> { await this.ensureModuleIsInitialized(); try { const data = await AuthsignalPushModule.removeCredential(); return { data }; } catch (ex) { if (this.enableLogging) { console.log(ex); } return handleErrorCodes(ex); } } async getChallenge(): Promise> { await this.ensureModuleIsInitialized(); try { const data = (await AuthsignalPushModule.getChallenge()) as | AppChallenge | undefined; return { data }; } catch (ex) { if (this.enableLogging) { console.log(ex); } return handleErrorCodes(ex); } } async updateChallenge({ challengeId, approved, verificationCode = null, }: UpdateChallengeInput): Promise> { await this.ensureModuleIsInitialized(); try { const data = await AuthsignalPushModule.updateChallenge( challengeId, approved, verificationCode ); return { data }; } catch (ex) { if (this.enableLogging) { console.log(ex); } return handleErrorCodes(ex); } } async updateCredential( pushToken: string ): Promise> { await this.ensureModuleIsInitialized(); try { const data = (await AuthsignalPushModule.updateCredential( pushToken )) as UpdatedAppCredential; return { data }; } catch (ex) { if (this.enableLogging) { console.log(ex); } return handleErrorCodes(ex); } } private async ensureModuleIsInitialized() { if (this.initialized) { return; } await AuthsignalPushModule.initialize(this.tenantID, this.baseURL); this.initialized = true; } }