@contentpass/react-native-contentpass 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +180 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # @contentpass/react-native-contentpass
2
+
3
+ The core Contentpass SDK for React Native. Handles OAuth 2.0 authentication, subscription validation, and impression tracking for Contentpass-enabled apps.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @contentpass/react-native-contentpass
9
+ # or
10
+ yarn add @contentpass/react-native-contentpass
11
+ ```
12
+
13
+ ### Peer dependencies
14
+
15
+ The following peer dependencies must be installed:
16
+
17
+ - [react](https://github.com/facebook/react)
18
+ - [react-native](https://github.com/facebook/react-native)
19
+ - [react-native-app-auth](https://github.com/FormidableLabs/react-native-app-auth) — OAuth 2.0 authentication
20
+ - [react-native-encrypted-storage](https://github.com/emeraldsanto/react-native-encrypted-storage) — secure token storage
21
+
22
+ Some peer dependencies require additional native setup. Refer to their official guides:
23
+
24
+ - [react-native-app-auth setup](https://commerce.nearform.com/open-source/react-native-app-auth/docs#setup)
25
+ - [react-native-encrypted-storage setup](https://github.com/emeraldsanto/react-native-encrypted-storage?tab=readme-ov-file#installation)
26
+
27
+ ### Expo support
28
+
29
+ If you are using Expo, run the following command to enable native directory modifications:
30
+
31
+ ```bash
32
+ npx expo prebuild
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ### Initialization
38
+
39
+ Wrap your app's root component with `ContentpassSdkProvider`:
40
+
41
+ ```tsx
42
+ import { ContentpassSdkProvider } from '@contentpass/react-native-contentpass';
43
+
44
+ const contentpassConfig = {
45
+ propertyId: 'property-id',
46
+ planId: 'plan-id',
47
+ issuer: 'https://my.contentpass.net',
48
+ apiUrl: 'https://api.contentpass.net',
49
+ redirectUrl: 'com.yourapp://oauthredirect',
50
+ samplingRate: 0.1, // optional, default 0.05
51
+ logLevel: 'info', // optional, default disabled
52
+ };
53
+
54
+ export default function App() {
55
+ return (
56
+ <ContentpassSdkProvider contentpassConfig={contentpassConfig}>
57
+ <YourApp />
58
+ </ContentpassSdkProvider>
59
+ );
60
+ }
61
+ ```
62
+
63
+ ### Configuration
64
+
65
+ | Property | Type | Required | Description |
66
+ |----------|------|----------|-------------|
67
+ | `propertyId` | `string` | Yes | Your unique property ID (provided by Contentpass). |
68
+ | `planId` | `string` | Yes | The plan ID to check subscriptions against (provided by Contentpass). |
69
+ | `issuer` | `string` | Yes | The OAuth 2.0 server URL (e.g. `https://my.contentpass.net`). |
70
+ | `apiUrl` | `string` | Yes | The Contentpass API base URL. |
71
+ | `redirectUrl` | `string` | Yes | The deep-link URL your app registers for OAuth redirects. |
72
+ | `samplingRate` | `number` | No | Rate at which impression events are sent for unauthenticated users. Default `0.05` (5%). |
73
+ | `logLevel` | `'debug' \| 'info' \| 'warn' \| 'error'` | No | SDK log level. Logging is disabled by default. |
74
+
75
+ ### SDK methods
76
+
77
+ Access the SDK via the `useContentpassSdk` hook:
78
+
79
+ ```tsx
80
+ import { useContentpassSdk } from '@contentpass/react-native-contentpass';
81
+
82
+ function MyComponent() {
83
+ const {
84
+ authenticate,
85
+ countImpression,
86
+ registerObserver,
87
+ unregisterObserver,
88
+ logout,
89
+ recoverFromError,
90
+ event,
91
+ } = useContentpassSdk();
92
+
93
+ // ...
94
+ }
95
+ ```
96
+
97
+ #### `authenticate(route?)`
98
+
99
+ Initiates the OAuth 2.0 authentication flow via a modal interface. Validates the user's active Contentpass subscription upon success.
100
+
101
+ - `route` (optional): `'login'` or `'signup'` to pre-select the auth screen.
102
+
103
+ #### `countImpression()`
104
+
105
+ Tracks an impression for the current user. Call this whenever a user views a piece of content. Applies to both authenticated and unauthenticated users (subject to `samplingRate` for unauthenticated users).
106
+
107
+ #### `registerObserver(observer)`
108
+
109
+ Registers a callback that fires whenever the authentication/subscription state changes. The callback receives a `ContentpassState` object.
110
+
111
+ #### `unregisterObserver(observer)`
112
+
113
+ Removes a previously registered observer.
114
+
115
+ #### `logout()`
116
+
117
+ Logs the user out by clearing all stored authentication tokens.
118
+
119
+ #### `recoverFromError()`
120
+
121
+ When a background token refresh fails (e.g. due to network issues), the state transitions to `ERROR`. Call this method once connectivity is restored to retry token validation.
122
+
123
+ #### `event(eventCategory, eventAction, eventLabel?, samplingRate?)`
124
+
125
+ Sends a custom analytics event to Contentpass.
126
+
127
+ ### State types
128
+
129
+ The observer callback receives one of the following states:
130
+
131
+ | State | `hasValidSubscription` | Description |
132
+ |-------|------------------------|-------------|
133
+ | `INITIALISING` | — | SDK is starting up and restoring any stored session. |
134
+ | `UNAUTHENTICATED` | `false` | No user is logged in. |
135
+ | `AUTHENTICATED` | `boolean` | User is logged in. `hasValidSubscription` indicates whether they have an active plan. |
136
+ | `ERROR` | — | A background token refresh failed. Contains an `error` property with the original exception. |
137
+
138
+ ```tsx
139
+ import {
140
+ ContentpassStateType,
141
+ useContentpassSdk,
142
+ } from '@contentpass/react-native-contentpass';
143
+ import { useEffect } from 'react';
144
+
145
+ function MyComponent() {
146
+ const { registerObserver, unregisterObserver } = useContentpassSdk();
147
+
148
+ useEffect(() => {
149
+ const observer = (state) => {
150
+ switch (state.state) {
151
+ case ContentpassStateType.AUTHENTICATED:
152
+ console.log('Subscription active:', state.hasValidSubscription);
153
+ break;
154
+ case ContentpassStateType.ERROR:
155
+ console.error('SDK error:', state.error);
156
+ break;
157
+ }
158
+ };
159
+
160
+ registerObserver(observer);
161
+ return () => unregisterObserver(observer);
162
+ }, [registerObserver, unregisterObserver]);
163
+ }
164
+ ```
165
+
166
+ ### CMP adapter interface
167
+
168
+ This package also exports the `CmpAdapter` type, which defines the interface that consent management platform adapters must implement to work with Contentpass. See [`@contentpass/react-native-contentpass-cmp-onetrust`](../react-native-contentpass-cmp-onetrust) for a ready-made OneTrust adapter.
169
+
170
+ ## Integration with Sourcepoint SDK
171
+
172
+ See the [Sourcepoint SDK integration guide](./docs/SOURCEPOINT_SDK_INTEGRATION.md).
173
+
174
+ ## Contributing
175
+
176
+ See the [contributing guide](./docs/CONTRIBUTING.md).
177
+
178
+ ## License
179
+
180
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentpass/react-native-contentpass",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Contentpass React Native SDK",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",