Skip to content

Commit 70c5ba1

Browse files
committed
feat: add observer for notifying about changes in state
1 parent c2e8229 commit 70c5ba1

File tree

10 files changed

+274
-76
lines changed

10 files changed

+274
-76
lines changed

sharedExample/src/ContentpassUsage.tsx

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useContentpassSdk } from './ContentpassContext';
22
import { Button, ScrollView, StyleSheet, Text, View } from 'react-native';
3-
import { useCallback, useEffect, useRef, useState } from 'react';
4-
import type { AuthenticateResult } from 'react-native-contentpass';
3+
import { useEffect, useRef, useState } from 'react';
4+
import type { ContentpassState } from 'react-native-contentpass';
55
import {
66
SPConsentManager,
77
type SPUserData,
@@ -13,6 +13,11 @@ const styles = StyleSheet.create({
1313
height: 400,
1414
flexGrow: 0,
1515
},
16+
buttonsContainer: {
17+
display: 'flex',
18+
gap: 4,
19+
marginTop: 10,
20+
},
1621
logsView: {
1722
marginTop: 10,
1823
},
@@ -40,21 +45,13 @@ const setupSourcepoint = (hasValidSubscription: boolean) => {
4045
};
4146

4247
export default function ContentpassUsage() {
43-
const [authResult, setAuthResult] = useState<
44-
AuthenticateResult | undefined
45-
>();
48+
const [authResult, setAuthResult] = useState<ContentpassState | undefined>();
4649
const contentpassSdk = useContentpassSdk();
4750
const spConsentManager = useRef<SPConsentManager | null>();
4851
const [sourcepointUserData, setSourcepointUserData] = useState<
4952
SPUserData | undefined
5053
>();
5154

52-
const authenticate = useCallback(async () => {
53-
spConsentManager.current?.dispose();
54-
const result = await contentpassSdk.authenticate();
55-
setAuthResult(result);
56-
}, [contentpassSdk]);
57-
5855
useEffect(() => {
5956
spConsentManager.current = setupSourcepoint(
6057
authResult?.hasValidSubscription ?? false
@@ -66,7 +63,7 @@ export default function ContentpassUsage() {
6663

6764
spConsentManager.current?.onAction((action) => {
6865
if (action.customActionId === "cp('login')") {
69-
authenticate();
66+
contentpassSdk.authenticate();
7067
}
7168
});
7269

@@ -75,7 +72,18 @@ export default function ContentpassUsage() {
7572
return () => {
7673
spConsentManager.current?.dispose();
7774
};
78-
}, [authResult, authenticate]);
75+
}, [authResult, contentpassSdk]);
76+
77+
useEffect(() => {
78+
const onContentpassStateChange = (state: ContentpassState) => {
79+
setAuthResult(state);
80+
};
81+
contentpassSdk.registerObserver(onContentpassStateChange);
82+
83+
return () => {
84+
contentpassSdk.unregisterObserver(onContentpassStateChange);
85+
};
86+
}, [contentpassSdk]);
7987

8088
const clearSourcepointData = () => {
8189
spConsentManager.current?.clearLocalData();
@@ -85,7 +93,13 @@ export default function ContentpassUsage() {
8593

8694
return (
8795
<>
88-
<Button title={'Clear sourcepoint data'} onPress={clearSourcepointData} />
96+
<View style={styles.buttonsContainer}>
97+
<Button
98+
title={'Clear sourcepoint data'}
99+
onPress={clearSourcepointData}
100+
/>
101+
<Button title={'Logout'} onPress={contentpassSdk.logout} />
102+
</View>
89103
<View style={styles.logsView}>
90104
<Text>Authenticate result:</Text>
91105
<Text>{JSON.stringify(authResult, null, 2)}</Text>

sharedExample/src/contentpassConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Config } from 'react-native-contentpass';
1+
import type { ContentpassConfig } from 'react-native-contentpass';
22

3-
export const contentpassConfig: Config = {
3+
export const contentpassConfig: ContentpassConfig = {
44
propertyId: 'cc3fc4ad-cbe5-4d09-bf85-a49796603b19',
55
redirectUrl: 'de.contentpass.demo://oauth',
66
issuer: 'https://my.contentpass.dev',

src/OidcAuthStateStorage.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import EncryptedStorage from 'react-native-encrypted-storage';
2-
import type { AuthorizeResult } from 'react-native-app-auth';
32

43
const AUTH_STATE_KEY = 'OIDCAuthState';
54

5+
export type OidcAuthState = {
6+
accessToken: string;
7+
accessTokenExpirationDate: string;
8+
idToken: string;
9+
refreshToken: string | null;
10+
tokenType: string;
11+
};
12+
613
export default class OidcAuthStateStorage {
714
private readonly key;
815

916
constructor(clientId: string) {
1017
this.key = `de.contentpass.${clientId}-${AUTH_STATE_KEY}`;
1118
}
1219

13-
public async storeOidcAuthState(authState: AuthorizeResult) {
20+
public async storeOidcAuthState(authState: OidcAuthState) {
1421
await EncryptedStorage.setItem(this.key, JSON.stringify(authState));
1522
}
1623

17-
public async getOidcAuthState() {
24+
public async getOidcAuthState(): Promise<OidcAuthState | undefined> {
1825
const oidcAuthStateString = await EncryptedStorage.getItem(this.key);
1926

2027
return oidcAuthStateString ? JSON.parse(oidcAuthStateString) : undefined;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const SCOPES = ['openid', 'offline_access', 'contentpass'];
22
export const TOKEN_ENDPOINT = `/auth/oidc/token`;
3+
export const REFRESH_TOKEN_RETRIES = 6;

0 commit comments

Comments
 (0)