### Start Example App Packager Source: https://github.com/contentpass/react-native-contentpass/blob/main/docs/CONTRIBUTING.md Commands to start the development packager for the example application. This allows for live reloading of changes in the example app. ```shell yarn example start ``` ```shell yarn expo-example start ``` -------------------------------- ### Run Example App on iOS Source: https://github.com/contentpass/react-native-contentpass/blob/main/docs/CONTRIBUTING.md Commands to build and run the example application on an iOS simulator or device. Requires running 'bundle install' in the 'example/ios' directory first. ```shell yarn example ios ``` ```shell yarn expo-example ios ``` -------------------------------- ### Start Metro Server (npm/Yarn) Source: https://github.com/contentpass/react-native-contentpass/blob/main/example/README.md Starts the Metro bundler, which is essential for running React Native applications. This command should be executed from the root of the React Native project. ```bash # using npm npm start # OR using Yarn yarn start ``` -------------------------------- ### Install CocoaPods Source: https://github.com/contentpass/react-native-contentpass/blob/main/docs/CONTRIBUTING.md Command to install CocoaPods, a dependency manager for Swift and Objective-C projects. This is a prerequisite for running iOS example applications. ```shell sudo gem install cocoapods ``` -------------------------------- ### Run Example App on Android Source: https://github.com/contentpass/react-native-contentpass/blob/main/docs/CONTRIBUTING.md Commands to build and run the example application on an Android device or emulator. This is used to test changes made to the library. ```shell yarn example android ``` ```shell yarn expo-example android ``` -------------------------------- ### Install @contentpass/react-native-contentpass and Dependencies Source: https://context7.com/contentpass/react-native-contentpass/llms.txt Installs the main Contentpass React Native SDK package along with its required peer dependencies, react-native-app-auth and react-native-encrypted-storage. This setup is necessary before integrating the SDK into your application. ```bash # Install the main package npm install @contentpass/react-native-contentpass # Or using Yarn yarn add @contentpass/react-native-contentpass # Required peer dependencies npm install react-native-app-auth react-native-encrypted-storage ``` -------------------------------- ### Run Application on iOS (npm/Yarn) Source: https://github.com/contentpass/react-native-contentpass/blob/main/example/README.md Builds and runs the React Native application on an iOS simulator or device. The Metro server must be running concurrently. ```bash # using npm npm run ios # OR using Yarn yarn ios ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/contentpass/react-native-contentpass/blob/main/docs/CONTRIBUTING.md Command to install all required project dependencies using Yarn workspaces. This command should be run in the root directory of the project. ```shell yarn ``` -------------------------------- ### Install @contentpass/react-native-contentpass using npm or Yarn Source: https://github.com/contentpass/react-native-contentpass/blob/main/README.md Installs the Contentpass React Native SDK package. Ensure you have Node.js and npm or Yarn installed. ```sh npm install @contentpass/react-native-contentpass ``` ```sh yarn add @contentpass/react-native-contentpass ``` -------------------------------- ### Run Application on Android (npm/Yarn) Source: https://github.com/contentpass/react-native-contentpass/blob/main/example/README.md Builds and runs the React Native application on an Android device or emulator. Ensure the Metro server is running in a separate terminal. ```bash # using npm npm run android # OR using Yarn yarn android ``` -------------------------------- ### Sourcepoint CMP Integration for Contentpass Authentication Source: https://context7.com/contentpass/react-native-contentpass/llms.txt Shows how to integrate the Contentpass SDK with Sourcepoint CMP for consent management. This example configures Sourcepoint to receive subscription status via targeting parameters and handles custom actions within the consent dialog, such as initiating Contentpass authentication. ```tsx import { useEffect, useRef, useState } from 'react'; import { SPConsentManager } from '@sourcepoint/react-native-cmp'; import { useContentpassSdk, ContentpassState, ContentpassStateType, } from '@contentpass/react-native-contentpass'; const sourcePointConfig = { accountId: 12345, propertyId: 67890, propertyName: 'your-property.com', }; const AppWithSourcepoint = () => { const [authResult, setAuthResult] = useState(); const contentpassSdk = useContentpassSdk(); const spConsentManager = useRef(null); // Listen to Contentpass state changes useEffect(() => { const observer = (state: ContentpassState) => setAuthResult(state); contentpassSdk.registerObserver(observer); return () => contentpassSdk.unregisterObserver(observer); }, [contentpassSdk]); // Initialize Sourcepoint after auth state is known useEffect(() => { if (!authResult || authResult.state === ContentpassStateType.INITIALISING) { return; } const { accountId, propertyName, propertyId } = sourcePointConfig; const manager = new SPConsentManager(); manager.build(accountId, propertyId, propertyName, { gdpr: { targetingParams: { // Pass subscription status to Sourcepoint acps: authResult.hasValidSubscription ? 'true' : 'false', }, }, }); // Handle custom "Login with Contentpass" button in consent dialog manager.onAction((action) => { if (action.customActionId === "cp('login')") { contentpassSdk.authenticate().catch(console.error); } }); manager.loadMessage(); spConsentManager.current = manager; return () => { manager.dispose(); }; }, [authResult, contentpassSdk]); return ; }; ``` -------------------------------- ### Contentpass State Handling with TypeScript Types Source: https://context7.com/contentpass/react-native-contentpass/llms.txt Demonstrates how to use exported TypeScript types from the Contentpass SDK for type-safe handling of authentication states. Includes type guard functions and an example state handler for different authentication scenarios. ```tsx import { ContentpassState, ContentpassStateType, InitialisingState, UnauthenticatedState, AuthenticatedState, ErrorState, } from '@contentpass/react-native-contentpass'; // Type guard functions for state handling const isAuthenticated = (state: ContentpassState): state is AuthenticatedState => { return state.state === ContentpassStateType.AUTHENTICATED; }; const hasActiveSubscription = (state: ContentpassState): boolean => { return isAuthenticated(state) && state.hasValidSubscription; }; // Example state handler const handleState = (state: ContentpassState) => { switch (state.state) { case ContentpassStateType.INITIALISING: // state: InitialisingState return { showLoader: true }; case ContentpassStateType.UNAUTHENTICATED: // state: UnauthenticatedState // state.hasValidSubscription is always false return { showLoginPrompt: true }; case ContentpassStateType.AUTHENTICATED: // state: AuthenticatedState // state.hasValidSubscription is boolean return { showContent: true, isPremium: state.hasValidSubscription }; case ContentpassStateType.ERROR: // state: ErrorState // state.error contains the original exception return { showError: true, error: state.error }; } }; ``` -------------------------------- ### Integrate Contentpass and Sourcepoint SDKs in React Native App Source: https://github.com/contentpass/react-native-contentpass/blob/main/docs/SOURCEPOINT_SDK_INTEGRATION.md This code snippet demonstrates how to integrate the Contentpass and Sourcepoint SDKs within a React Native application. It sets up the Sourcepoint consent manager, listens for Contentpass state changes, and triggers Contentpass authentication when a specific Sourcepoint custom action is detected. Ensure you have both SDKs installed and Sourcepoint configured correctly. ```jsx import { useEffect, useRef, useState } from 'react'; import { SPConsentManager } from '@sourcepoint/react-native-cmp'; import { ContentpassStateType, useContentpassSdk } from '@contentpass/react-native-contentpass'; const sourcePointConfig = { accountId: 'SOURCEPOINT_ACCOUNT_ID', propertyId: 'SOURCEPOINT_PROPERTY_ID', propertyName: 'SOURCEPOINT_PROPERTY_NAME', }; const setupSourcepoint = (hasValidSubscription) => { const { accountId, propertyName, propertyId } = sourcePointConfig; const spConsentManager = new SPConsentManager(); spConsentManager.build(accountId, propertyId, propertyName, { gdpr: { targetingParams: { acps: hasValidSubscription ? 'true' : 'false', }, }, }); return spConsentManager; }; const App = () => { const [authResult, setAuthResult] = useState(); const contentpassSdk = useContentpassSdk(); const spConsentManager = useRef(); useEffect(() => { const onContentpassStateChange = (state) => { setAuthResult(state); }; contentpassSdk.registerObserver(onContentpassStateChange); return () => { contentpassSdk.unregisterObserver(onContentpassStateChange); }; }, [contentpassSdk]); useEffect(() => { // wait for the authResult to be set before setting up Sourcepoint if (!authResult || authResult.state === ContentpassStateType.INITIALISING) { return; } spConsentManager.current = setupSourcepoint( authResult.hasValidSubscription || false ); spConsentManager.current?.onAction((action) => { if (action.customActionId === "cp('login')") { contentpassSdk.authenticate() } }); spConsentManager.current?.loadMessage(); return () => { spConsentManager.current?.dispose(); }; }, [authResult, contentpassSdk]); return ( // Your app content ) } ``` -------------------------------- ### Initialize Contentpass SDK with ContentpassSdkProvider Source: https://context7.com/contentpass/react-native-contentpass/llms.txt Demonstrates how to set up the Contentpass SDK within a React Native application using the ContentpassSdkProvider. This provider wraps the application's root component and requires a configuration object containing essential Contentpass credentials and settings. ```tsx import React from 'react'; import { ContentpassSdkProvider } from '@contentpass/react-native-contentpass'; import type { ContentpassConfig } from '@contentpass/react-native-contentpass'; const contentpassConfig: ContentpassConfig = { propertyId: 'your-property-uuid', planId: 'your-plan-uuid', issuer: 'https://my.contentpass.net', apiUrl: 'https://cp.cmp-sourcepoint.contentpass.net', redirectUrl: 'com.yourapp://oauth', samplingRate: 0.05, // Optional: 5% sampling rate for anonymous impressions logLevel: 'info', // Optional: 'debug' | 'info' | 'warn' | 'error' }; const App = () => { return ( ); }; export default App; ``` -------------------------------- ### Run Unit Tests Source: https://github.com/contentpass/react-native-contentpass/blob/main/docs/CONTRIBUTING.md Command to execute the project's unit tests. It is recommended to add tests for any new changes made to the library. ```shell yarn test ``` -------------------------------- ### Initiate OAuth 2.0 Authentication with authenticate() Source: https://context7.com/contentpass/react-native-contentpass/llms.txt Explains how to trigger the OAuth 2.0 authentication flow using the authenticate() method. This method opens a modal browser for user login, validates subscriptions upon success, and updates the SDK's internal state. It returns a Promise that resolves on completion or rejects on error. ```tsx import { useContentpassSdk } from '@contentpass/react-native-contentpass'; const LoginButton = () => { const { authenticate } = useContentpassSdk(); const handleAuthenticate = async () => { try { await authenticate(); // Authentication successful // State will automatically update to AUTHENTICATED // Check hasValidSubscription in observer callback } catch (error) { if (error.message.includes('cancelled')) { console.log('User cancelled authentication'); } else { console.error('Authentication error:', error); } } }; return