import { Platform } from 'react-native' import type { MSALConfiguration, MSALInteractiveParams, MSALSilentParams, MSALAccount, MSALSignoutParams, IPublicClientApplication, MSALResult, MSALAndroidPreferredBrowser, } from './types' import { NativeModules } from 'react-native' type RNMSALNativeModule = { createPublicClientApplication(config: MSALConfiguration): Promise acquireToken(params: MSALInteractiveParams): Promise acquireTokenSilent(params: MSALSilentParams): Promise getAccounts(): Promise getAccount(accountIdentifier: string): Promise removeAccount(account: MSALAccount): Promise signout(params: MSALSignoutParams): Promise getSelectedBrowser(): Promise getSafeCustomTabsBrowsers(): Promise } const RNMSAL: RNMSALNativeModule = NativeModules.RNMSAL // export default RNMSAL export class PublicClientApplication implements IPublicClientApplication { private isInitialized: boolean = false constructor(private readonly config: MSALConfiguration) {} public async init() { if (!this.isInitialized) { await RNMSAL.createPublicClientApplication(this.config) this.isInitialized = true } return this } public async acquireToken(params: MSALInteractiveParams) { this.validateIsInitialized() return await RNMSAL.acquireToken(params) } public async acquireTokenSilent(params: MSALSilentParams) { this.validateIsInitialized() return await RNMSAL.acquireTokenSilent(params) } public async getAccounts() { this.validateIsInitialized() return await RNMSAL.getAccounts() } public async getAccount(accountIdentifier: string) { this.validateIsInitialized() return await RNMSAL.getAccount(accountIdentifier) } public async removeAccount(account: MSALAccount) { this.validateIsInitialized() return await RNMSAL.removeAccount(account) } public async signOut(params: MSALSignoutParams) { this.validateIsInitialized() return await Platform.select({ ios: async () => await RNMSAL.signout(params), default: async () => await RNMSAL.removeAccount(params.account), })() } public async getSelectedBrowser() { this.validateIsInitialized() return await Platform.select({ android: async () => await RNMSAL.getSelectedBrowser(), default: async () => 'N/A', })() } async getSafeCustomTabsBrowsers() { this.validateIsInitialized() return await Platform.select({ android: async () => await RNMSAL.getSafeCustomTabsBrowsers(), default: async () => [], })() } private validateIsInitialized() { if (!this.isInitialized) { throw new Error( 'PublicClientApplication is not initialized. You must call the `init` method before any other method.', ) } } }