MSAL React Native wrapper for iOS and Android
npm install react-native-msal2



MSAL React Native wrapper for iOS and Android
```
npm i react-native-msal2
`typescript
import PublicClientApplication from 'react-native-msal2';
import type { MSALConfiguration /, etc / } from 'react-native-msal2';
const config: MSALConfiguration = {
auth: {
clientId: 'your-client-id',
// This authority is used as the default in acquireToken and acquireTokenSilent if not provided to those methods.
// Defaults to 'https://login.microsoftonline.com/common'
authority: 'https://
},
};
const scopes = ['scope1', 'scope2'];
// Initialize the public client application:
const pca = new PublicClientApplication(config);
try {
await pca.init();
} catch (error) {
console.error('Error initializing the pca, check your config.', error);
}
// Acquiring a token for the first time, you must call pca.acquireToken
const params: MSALInteractiveParams = { scopes };
const result: MSALResult | undefined = await pca.acquireToken(params);
// On subsequent token acquisitions, you can call pca.acquireTokenSilentforceRefresh
// Force the token to refresh with the optionpca.getAccounts
const params: MSALSilentParams = {
account: result!.account, // or get this by filtering the result from (see below)
scopes,
forceRefresh: true,
};
const result: MSALResult | undefined = await pca.acquireTokenSilent(params);
// Get all accounts for which this application has refresh tokens
const accounts: MSALAccount[] = await pca.getAccounts();
// Retrieve the account matching the identifier
const account: MSALAccount | undefined = await pca.getAccount(result!.account.identifier);
// Remove all tokens from the cache for this application for the provided account
const success: boolean = await pca.removeAccount(result!.account);
// Same as pca.removeAccount with the exception that, if called on iOS with the signoutFromBrowser option set to true, it will additionally remove the account from the system browser`
const params: MSALSignoutParams = {
account: result!.account,
signoutFromBrowser: true,
};
const success: boolean = await pca.signOut(params);
Run a single build with npm run build and find the output in /dist.
Tests configured for React Native can be run with npm test or npm run test:watch in watch mode.
To test your plugin on a device run the following to create a React Native app using it.
``
npm run app
cd app
npm run ios / npm run android
The following command will automatically copy over changes made to the plugin to the app.
```
npm run watch