|
1 | | -# @contentpass/react-native-contentpass |
| 1 | +# react-native-contentpass |
2 | 2 |
|
3 | | -Contentpass React Native SDK enables easy integration of Contentpass functionality into your React Native applications. |
| 3 | +Monorepo for the Contentpass React Native SDK and related packages. |
4 | 4 |
|
5 | | -## Installation |
6 | | -Install the package using npm or Yarn: |
| 5 | +## Packages |
7 | 6 |
|
8 | | -```sh |
9 | | -npm install @contentpass/react-native-contentpass |
10 | | -``` |
| 7 | +| Package | Description | |
| 8 | +|---------|-------------| |
| 9 | +| [`@contentpass/react-native-contentpass`](./packages/react-native-contentpass) | Core SDK — authentication, subscription validation, and impression tracking. | |
| 10 | +| [`@contentpass/react-native-contentpass-ui`](./packages/react-native-contentpass-ui) | Pre-built UI components for the Contentpass consent layer. | |
| 11 | +| [`@contentpass/react-native-contentpass-cmp-onetrust`](./packages/react-native-contentpass-cmp-onetrust) | OneTrust CMP adapter for the Contentpass SDK. | |
11 | 12 |
|
12 | | -or |
| 13 | +## Examples |
13 | 14 |
|
14 | | -```sh |
15 | | -yarn add @contentpass/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 | | - |
36 | | -## Usage |
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 (ask Contentpass team for details) |
41 | | -- `planId` - The ID of the plan you want to check the user's subscription status against (ask Contentpass team for details) |
42 | | -- `issuer` - The OAuth 2.0 server URL (e.g. `https://my.contentpass.net`) |
43 | | -- `redirectUrl` - the redirect URL of your app to which the OAuth2 server will redirect after the authentication |
44 | | -- `samplingRate` - Optional: The rate at which the SDK will send impression events for unauthenticated users. Default is 0.05 (5%) |
45 | | -- `logLevel` - Optional: The log level for the SDK. By default logger is disabled. Possible values are 'info', 'warn', 'error' and 'debug' |
46 | | - |
47 | | - |
48 | | -```jsx |
49 | | -import React from 'react'; |
50 | | -import { ContentpassSdkProvider } from '@contentpass/react-native-contentpass'; |
51 | | - |
52 | | -const contentpassConfig = { |
53 | | - propertyId: 'property-id', |
54 | | - planId: 'plan-id', |
55 | | - issuer: 'https://my.contentpass.net', |
56 | | - redirectUrl: 'com.yourapp://oauthredirect', |
57 | | - samplingRate: 0.1, |
58 | | - logLevel: 'info' |
59 | | -}; |
60 | | - |
61 | | -const App = () => { |
62 | | - return ( |
63 | | - <ContentpassSdkProvider contentpassConfig={contentpassConfig}> |
64 | | - <YourApp /> |
65 | | - </ContentpassSdkProvider> |
66 | | - ); |
67 | | -}; |
68 | | - |
69 | | -export default App; |
70 | | -``` |
71 | | - |
72 | | -### SDK Methods |
73 | | -The SDK exposes the following methods through the `useContentpassSdk` hook: |
74 | | - |
75 | | -### authenticate |
76 | | -Initiates the OAuth 2.0 authentication process via a modal interface. It validates the user’s active Contentpass subscriptions |
77 | | -upon successful authentication. |
78 | | - |
79 | | -### countImpression |
80 | | -Tracks and increments the impression count for the current user. This method should be invoked whenever a user views a |
81 | | -piece of content. It applies to all users, whether authenticated or unauthenticated. |
82 | | - |
83 | | -### registerObserver |
84 | | -Registers a callback function to listen for changes in the user’s authentication and subscription status. The observer function |
85 | | -receives a state object describing the current status (see the exported [ContentpassState](./packages/react-native-contentpass/src/types/ContentpassState.ts) type). |
86 | | - |
87 | | -### unregisterObserver |
88 | | -Unregisters a previously registered observer. The observer will no longer receive updates. |
89 | | - |
90 | | -### logout |
91 | | -Logs the user out by clearing all stored authentication tokens. |
92 | | - |
93 | | -### recoverFromError |
94 | | -During background token refresh, an error state may occur due to poor or no internet connection. This is indicated by the |
95 | | -`state` switching to `ERROR`. The state object includes a reference to the original exception that was thrown. As the SDK |
96 | | -does not monitor the device's connection state, you must notify the SDK when the network connection has been reestablished |
97 | | -or improved. The SDK will then refresh and revalidate the user's authentication tokens. |
98 | | - |
99 | | -```jsx |
100 | | -import React, { useEffect } from 'react'; |
101 | | -import { useContentpassSdk } from '@contentpass/react-native-contentpass'; |
102 | | -import { Button, View } from 'react-native'; |
103 | | - |
104 | | -const YourApp = () => { |
105 | | - const { |
106 | | - authenticate, |
107 | | - countImpression, |
108 | | - registerObserver, |
109 | | - unregisterObserver, |
110 | | - logout, |
111 | | - recoverFromError |
112 | | - } = useContentpassSdk(); |
113 | | - |
114 | | - useEffect(() => { |
115 | | - const observer = (state) => { |
116 | | - console.log('Contentpass state changed:', state); |
117 | | - }; |
118 | | - |
119 | | - registerObserver(observer); |
120 | | - |
121 | | - return () => { |
122 | | - unregisterObserver(observer); |
123 | | - }; |
124 | | - }, []); |
125 | | - |
126 | | - return ( |
127 | | - <View> |
128 | | - <Button onPress={authenticate} title={'Authenticate'} /> |
129 | | - <Button onPress={countImpression} title={'Count Impression'} /> |
130 | | - </View> |
131 | | - ); |
132 | | -}; |
133 | | -``` |
134 | | - |
135 | | -## Integration with Sourcepoint SDK |
136 | | - |
137 | | -See the [Sourcepoint SDK documentation](packages/react-native-contentpass/docs/SOURCEPOINT_SDK_INTEGRATION.md) for information on integrating the Contentpass SDK with the Sourcepoint SDK. |
| 15 | +| Example | Description | |
| 16 | +|---------|-------------| |
| 17 | +| [`examples/onetrust`](./examples/onetrust) | Integration using OneTrust as the CMP. | |
| 18 | +| [`examples/sourcepoint`](./examples/sourcepoint) | Integration using Sourcepoint as the CMP (bare React Native). | |
| 19 | +| [`examples/sourcepoint-expo`](./examples/sourcepoint-expo) | Integration using Sourcepoint as the CMP (Expo). | |
138 | 20 |
|
139 | 21 | ## Contributing |
140 | 22 |
|
141 | | -See the [contributing guide](packages/react-native-contentpass/docs/CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. |
142 | | - |
| 23 | +See the [contributing guide](./packages/react-native-contentpass/docs/CONTRIBUTING.md). |
143 | 24 |
|
144 | 25 | ## License |
145 | 26 |
|
146 | 27 | MIT |
147 | | - |
148 | | ---- |
149 | | - |
150 | | -Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob) |
0 commit comments