|
1 | 1 | # react-native-contentpass |
2 | 2 |
|
3 | | -Contentpass React Native SDK |
| 3 | +Contentpass React Native SDK enables easy integration of Contentpass functionality into your React Native applications. |
4 | 4 |
|
5 | 5 | ## Installation |
| 6 | +Install the package using npm or Yarn: |
6 | 7 |
|
7 | 8 | ```sh |
8 | 9 | npm install react-native-contentpass |
9 | 10 | ``` |
10 | 11 |
|
| 12 | +or |
| 13 | + |
| 14 | +```sh |
| 15 | +yarn add react-native-contentpass |
| 16 | +``` |
| 17 | + |
| 18 | +### Peer Dependencies |
| 19 | +The following peer dependencies must also be installed: |
| 20 | +- [react](https://github.com/facebook/react) (Required for React Native projects.) |
| 21 | +- [react-native](https://github.com/facebook/react-native) (Core React Native framework) |
| 22 | +- [react-native-app-auth](https://github.com/FormidableLabs/react-native-app-auth) (Used for OAuth 2.0 authentication) |
| 23 | +- [react-native-encrypted-storage](https://github.com/emeraldsanto/react-native-encrypted-storage) (Ensures secure storage of authentication tokens) |
| 24 | + |
| 25 | +Some dependencies require additional setup in the native code. Refer to their official guides: |
| 26 | +- [react-native-app-auth setup](https://commerce.nearform.com/open-source/react-native-app-auth/docs#setup) |
| 27 | +- [react-native-encrypted-storage setup](https://github.com/emeraldsanto/react-native-encrypted-storage?tab=readme-ov-file#installation) |
| 28 | + |
| 29 | +### Expo support |
| 30 | +If you are using Expo, you need to run the following command to enable modifications to the `ios` and `android` directories: |
| 31 | + |
| 32 | +```sh |
| 33 | +npx expo prebuild |
| 34 | +``` |
| 35 | + |
11 | 36 | ## Usage |
12 | 37 |
|
| 38 | +### Initialization |
| 39 | +Wrap your app's root component with ContentpassSdkProvider. The provider requires a configuration object (contentpassConfig) with the following properties: |
| 40 | +- `propertyId` - Your unique property ID |
| 41 | +- `issuer` - The OAuth 2.0 server URL (e.g. `https://my.contentpass.net`) |
| 42 | +- `redirectUrl` - the redirect URL of your app to which the OAuth2 server will redirect after the authentication |
| 43 | + |
| 44 | + |
| 45 | +```jsx |
| 46 | +import React from 'react'; |
| 47 | +import { ContentpassSdkProvider } from 'react-native-contentpass'; |
| 48 | + |
| 49 | +const contentpassConfig = { |
| 50 | + propertyId: 'your-property-id', |
| 51 | + issuer: 'https://my.contentpass.net', |
| 52 | + redirectUrl: 'com.yourapp://oauthredirect', |
| 53 | +}; |
| 54 | + |
| 55 | +const App = () => { |
| 56 | + return ( |
| 57 | + <ContentpassSdkProvider contentpassConfig={contentpassConfig}> |
| 58 | + <YourApp /> |
| 59 | + </ContentpassSdkProvider> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +export default App; |
| 64 | +``` |
| 65 | + |
| 66 | +### SDK Methods |
| 67 | +The SDK exposes the following methods through the `useContentpassSdk` hook: |
| 68 | + |
| 69 | +### authenticate |
| 70 | +Initiates the OAuth 2.0 authentication process via a modal interface. It validates the user’s active Contentpass subscriptions |
| 71 | +upon successful authentication. |
| 72 | + |
| 73 | +### registerObserver |
| 74 | +Registers a callback function to listen for changes in the user’s authentication and subscription status. The observer function |
| 75 | +receives a state object describing the current status (see the exported [ContentpassState](./src/types/ContentpassState.ts) type). |
| 76 | + |
| 77 | +### unregisterObserver |
| 78 | +Unregisters a previously registered observer. The observer will no longer receive updates. |
| 79 | + |
| 80 | +### logout |
| 81 | +Logs the user out by clearing all stored authentication tokens. |
| 82 | + |
| 83 | +### recoverFromError |
| 84 | +During background token refresh, an error state may occur due to poor or no internet connection. This is indicated by the |
| 85 | +`state` switching to `ERROR`. The state object includes a reference to the original exception that was thrown. As the SDK |
| 86 | +does not monitor the device's connection state, you must notify the SDK when the network connection has been reestablished |
| 87 | +or improved. The SDK will then refresh and revalidate the user's authentication tokens. |
| 88 | + |
| 89 | +```jsx |
| 90 | +import React, { useEffect } from 'react'; |
| 91 | +import { useContentpassSdk } from 'react-native-contentpass'; |
| 92 | + |
| 93 | +const YourApp = () => { |
| 94 | + const { authenticate, registerObserver, unregisterObserver, logout, recoverFromError } = useContentpassSdk(); |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + const observer = (state) => { |
| 98 | + console.log('Contentpass state changed:', state); |
| 99 | + }; |
13 | 100 |
|
14 | | -```js |
15 | | -import { multiply } from 'react-native-contentpass'; |
| 101 | + registerObserver(observer); |
16 | 102 |
|
17 | | -// ... |
| 103 | + return () => { |
| 104 | + unregisterObserver(observer); |
| 105 | + }; |
| 106 | + }, []); |
18 | 107 |
|
19 | | -const result = await multiply(3, 7); |
| 108 | + return ( |
| 109 | + <button onClick={authenticate}>Authenticate</button> |
| 110 | + ); |
| 111 | +}; |
20 | 112 | ``` |
21 | 113 |
|
22 | 114 |
|
|
0 commit comments