Package Exports
- react-native-msal
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-native-msal) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
react-native-msal
Getting started
Requires React Native >=0.61
$ yarn add react-native-msal
Common setup
- Register your application in the Azure Portal
- Set up redirect URLs for your application in the portal. You will need one for both iOS and Android. They will have the following patterns:
- iOS:
msauth.<BUNDLE_ID>://auth.- ex:
msauth.energy.stash.msal.example://auth
- ex:
- Android:
msauth://<PACKAGE>/<BASE64_URL_ENCODED_PACKAGE_SIGNATURE>- ex:
msauth://energy.stash.msal.example/ab%4E1lPIzBP2j9uELdUz%2BcarjgxQ%3D - Get your package signature from your
*.keystore, or from the Google Play console if you have automatic app signing turned on. For local debugging you can enter this command to read yourdebug.keystore:keytool -list -v -keystore path/to/debug.keystore -alias androiddebugkey -storepass android -keypass android - Convert the SHA1 signature to base64:
echo -n "<YOUR_SHA1_SIGNATURE>" | openssl dgst -binary -sha1 | openssl base64 - URL-encode the base64 string
- ex:
- iOS:
Android Setup
- Follow steps 1 through 3 of the Using MSAL section of the Android MSAL docs.
IMPORTANT: For Step 2, you MUST create a file in your assets folder (
android/app/src/main/assets) namedmsal_config.jsoncontaining your MSAL configuration. If you don't have anassetsfolder already, you will have to create one
iOS Setup
- Follow the steps detailed in the Configuring MSAL section of the iOS MSAL docs
Usage
See example usage in App.tsx in the example app
import MSALClient from 'react-native-msal';
const clientId = '<clientId>';
const authority = '<authority>';
const scopes = ['scope'];
const msalClient = new MSALClient(clientId);
// The first time signing in you must use this call to perform an interactive login
// Use the token from result.accessToken to call your API
// See when the token expires with result.expiresOn
// Store result.account.identifier for acquiring tokens silently or clearing the token cache
const result = await msalClient.acquireToken({
authority,
scopes,
});
// Acquire a token silently
// You may specify `forceRefresh: true` to force acquiring a brand new token
const result = await msalClient.acquireTokenSilent({
authority,
scopes,
accountIdentifier: result.account.identifier,
});
// Removes all tokens from the cache for this application for the provided account
// A call to acquireToken will be required for acquiring subsequent access tokens
await msalClient.removeAccount({
authority,
accountIdentifier: result.account.identifier,
});
// Removes all tokens from the cache for this application for the provided account
// Additionally, this will remove the account from the system browser
// A call to acquireToken will be required for acquiring subsequent access tokens
// Only available on iOS platform, falls back to `removeAccount` on Android
await msalClient.signout({
authority,
accountIdentifier: result.account.identifier,
});Example
To run the example, first:
yarn bootstrap- Add the redirect URLs in your tenant:
- Android:
msauth://com.example/P6akJ4YYsuUDahjqGra9mAflzdA%3D - iOS:
msauth.com.example://auth
- Android:
- Update the
msalConfigobject inApp.tsxwith your details
Android
- Edit the
msal_config.jsonasset file to include your client id and authorities yarn example android
iOS
yarn example ios