rnx-kit/packages/react-native-auth
Tommy Nguyen dd8389bcec
fix(align-deps): add profile for 0.76 (#3391)
2024-10-28 16:21:34 +01:00
..
android chore: bump `react-native` to 0.73 (#2956) 2024-02-07 17:05:18 +01:00
ios fix(react-native-auth): RNXAccountType was wrong on iOS (#2636) 2023-08-28 10:04:22 +00:00
src chore: restrict use of `export default` (#2772) 2023-10-30 13:40:33 +01:00
CHANGELOG.md RELEASING: Releasing 44 package(s) (#3342) 2024-09-11 08:59:50 +02:00
README.md docs: automate removal of badges from READMEs (#2324) 2023-04-14 08:24:28 +02:00
RNXAuth.podspec fix(react-native-auth): update tag format in `.podspec` (#1589) 2022-05-31 23:19:06 +02:00
eslint.config.js chore: migrate to ESLint flat config (#2782) 2023-11-03 14:02:18 +01:00
package.json fix(align-deps): add profile for 0.76 (#3391) 2024-10-28 16:21:34 +01:00
tsconfig.json feat(tsconfig): base TypeScript configs for working with Node (#2886) 2023-12-18 12:05:07 +01:00

README.md

@rnx-kit/react-native-auth

Build npm version

@rnx-kit/react-native-auth provides a cross-app uniform API for user authentication.

Install

Add the dependency to your project:

npm add @rnx-kit/react-native-auth

If you're using a different manager, swap out npm with your package manager of choice.

Usage

import {
  acquireTokenWithScopes,
  isAvailable,
} from "@rnx-kit/react-native-auth";

const scopes = ["user.read"];
const userPrincipalName = "arnold@contoso.com";

if (isAvailable()) {
  const result = await acquireTokenWithScopes(
    scopes,
    userPrincipalName,
    "MicrosoftAccount"
  );
} else {
  // Use an alternate authentication method
}
Category Type Name Description
types AccountType Account types. Current valid types are Microsoft accounts (or MSA) and organizational (M365), but can be extended to support other types, e.g. Apple, Google, etc.
types AuthErrorAndroid The authentication error object contains a stack trace on Android.
types AuthErrorIOS The authentication error object contains a stack trace on iOS.
types AuthErrorNative The authentication error object. May contain a native stack trace.
types AuthErrorType The type of error that occurred during authentication.
types AuthErrorUserInfo Authentication error details provided by the underlying implementation. This object can be used to provide the inner exception, or a more user friendly error message.
types AuthResult Authentication result returned on success.
Category Function Description
- acquireTokenWithResource(resource, userPrincipalName, accountType) Acquires a token for a resource.
- acquireTokenWithScopes(scopes, userPrincipalName, accountType) Acquires a token with specified scopes.
- isAvailable() Returns whether this module is available.

Motivation

Many features we build require authentication. The tricky thing about authentication in brownfield apps (i.e. a native app hosting a React Native instance) is that we want to reuse the auth code that the hosting app already has to access the keychain and enable single sign-on. This excludes the use of most React Native auth libraries out there since they are more geared towards standalone use. Additionally, all apps implement this in different ways, so most feature teams implement their own solution for providing access tokens to their features during the development loop, or they rely on their hosting app to provide such a solution. Solutions are often custom-made for the current app and cannot be shared with others without significant effort. They will also have to duplicate this effort when integrating into other apps.

This module aims to define a standard way to acquire access tokens so that React Native feature authors no longer have to care about the underlying implementations. The idea is that by abstracting away the implementation details, React Native features can more easily be integrated into any app that provides an implementation of this module, without having to duplicate the effort of others.