@openfort/react-native 0.1.9 → 0.1.11
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.
- package/dist/components/AuthBoundary.js +7 -8
- package/dist/components/index.js +3 -6
- package/dist/constants/config.js +2 -2
- package/dist/core/client.js +31 -10
- package/dist/core/context.js +41 -4
- package/dist/core/provider.js +35 -4
- package/dist/core/storage.js +17 -7
- package/dist/hooks/auth/index.js +1 -1
- package/dist/hooks/auth/useCreateWalletPostAuth.js +9 -1
- package/dist/hooks/auth/useEmailAuth.js +26 -0
- package/dist/hooks/auth/useGuestAuth.js +21 -0
- package/dist/hooks/auth/useOAuth.js +15 -21
- package/dist/hooks/auth/useSignOut.js +32 -0
- package/dist/hooks/auth/useWalletAuth.js +25 -0
- package/dist/hooks/core/index.js +1 -1
- package/dist/hooks/core/useOpenfort.js +24 -27
- package/dist/hooks/core/useOpenfortClient.js +8 -13
- package/dist/hooks/core/useUser.js +28 -0
- package/dist/hooks/index.js +7 -6
- package/dist/hooks/wallet/index.js +1 -1
- package/dist/hooks/wallet/useWallet.js +27 -0
- package/dist/hooks/wallet/useWallets.js +21 -19
- package/dist/index.js +5 -6
- package/dist/lib/hookConsistency.js +42 -0
- package/dist/native/oauth.js +38 -1
- package/dist/native/storage.js +23 -8
- package/dist/types/components/AuthBoundary.d.ts +7 -8
- package/dist/types/components/index.d.ts +3 -6
- package/dist/types/constants/config.d.ts +2 -2
- package/dist/types/core/client.d.ts +22 -6
- package/dist/types/core/context.d.ts +56 -19
- package/dist/types/core/provider.d.ts +55 -29
- package/dist/types/core/storage.d.ts +7 -4
- package/dist/types/hooks/auth/index.d.ts +1 -1
- package/dist/types/hooks/auth/useCreateWalletPostAuth.d.ts +9 -0
- package/dist/types/hooks/auth/useEmailAuth.d.ts +26 -0
- package/dist/types/hooks/auth/useGuestAuth.d.ts +16 -18
- package/dist/types/hooks/auth/useOAuth.d.ts +15 -21
- package/dist/types/hooks/auth/useSignOut.d.ts +32 -0
- package/dist/types/hooks/auth/useWalletAuth.d.ts +25 -3
- package/dist/types/hooks/core/index.d.ts +1 -1
- package/dist/types/hooks/core/useOpenfort.d.ts +24 -24
- package/dist/types/hooks/core/useOpenfortClient.d.ts +8 -16
- package/dist/types/hooks/core/useUser.d.ts +28 -0
- package/dist/types/hooks/index.d.ts +7 -6
- package/dist/types/hooks/wallet/index.d.ts +1 -1
- package/dist/types/hooks/wallet/useWallet.d.ts +27 -0
- package/dist/types/hooks/wallet/useWallets.d.ts +21 -19
- package/dist/types/index.d.ts +5 -6
- package/dist/types/lib/hookConsistency.d.ts +42 -0
- package/dist/types/native/oauth.d.ts +38 -1
- package/dist/types/native/storage.d.ts +21 -9
- package/dist/types/predicates.js +20 -1
- package/dist/types/types/index.d.ts +8 -8
- package/dist/types/types/predicates.d.ts +55 -1
- package/package.json +2 -2
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { useOpenfort } from '../hooks/core/useOpenfort';
|
|
3
3
|
/**
|
|
4
|
-
* Authentication boundary component that
|
|
5
|
-
* the user's authentication status and SDK readiness.
|
|
4
|
+
* Authentication boundary component that renders content based on authentication state.
|
|
6
5
|
*
|
|
7
|
-
* This component simplifies protecting routes and content based on authentication state
|
|
8
|
-
*
|
|
9
|
-
* 1. Loading
|
|
10
|
-
* 2. Error
|
|
11
|
-
* 3. Unauthenticated
|
|
12
|
-
* 4. Authenticated
|
|
6
|
+
* This component simplifies protecting routes and content based on authentication state and
|
|
7
|
+
* handles four distinct scenarios:
|
|
8
|
+
* 1. **Loading** – the SDK is still initialising.
|
|
9
|
+
* 2. **Error** – the SDK encountered an initialisation error.
|
|
10
|
+
* 3. **Unauthenticated** – the user is not logged in.
|
|
11
|
+
* 4. **Authenticated** – the user is logged in and the SDK is ready.
|
|
13
12
|
*
|
|
14
13
|
* @example
|
|
15
14
|
* ```tsx
|
package/dist/components/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Openfort React Native SDK
|
|
2
|
+
* Openfort React Native SDK components.
|
|
3
3
|
*
|
|
4
|
-
* This
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* The components are organized into the following categories:
|
|
8
|
-
* - Authentication boundaries and guards
|
|
4
|
+
* This barrel currently exposes the authentication boundary component used to gate
|
|
5
|
+
* access to protected UI based on SDK readiness and user state.
|
|
9
6
|
*/
|
|
10
7
|
export { AuthBoundary } from './AuthBoundary';
|
package/dist/constants/config.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SDK package
|
|
2
|
+
* Static metadata describing the React Native SDK package.
|
|
3
3
|
*/
|
|
4
4
|
export const SDK_INFO = {
|
|
5
5
|
name: '@openfort/react-native',
|
|
6
|
-
version: '0.1.
|
|
6
|
+
version: '0.1.9',
|
|
7
7
|
description: 'React Native client for Openfort',
|
|
8
8
|
homepage: 'https://openfort.io/docs',
|
|
9
9
|
};
|
package/dist/core/client.js
CHANGED
|
@@ -4,16 +4,25 @@ import { applicationId } from 'expo-application';
|
|
|
4
4
|
import { SecureStorageAdapter, createNormalizedStorage } from './storage';
|
|
5
5
|
import { logger } from '../lib/logger';
|
|
6
6
|
/**
|
|
7
|
-
* Creates an
|
|
7
|
+
* Creates an {@link OpenfortClient} configured for Expo and React Native environments.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* The helper ensures Expo-specific utilities like secure storage and the crypto digest
|
|
10
|
+
* implementation are wired into the underlying Openfort SDK.
|
|
11
|
+
*
|
|
12
|
+
* @param options - {@link OpenfortSDKConfiguration} containing the base configuration,
|
|
13
|
+
* overrides, and optional Shield configuration.
|
|
14
|
+
* @returns A fully configured {@link OpenfortClient} instance ready for React Native apps.
|
|
11
15
|
*
|
|
12
16
|
* @example
|
|
17
|
+
* ```ts
|
|
13
18
|
* const client = createOpenfortClient({
|
|
19
|
+
* baseConfiguration: new OpenfortConfiguration({ publishableKey }),
|
|
20
|
+
* overrides: { logLevel: 'debug' },
|
|
21
|
+
* shieldConfiguration: new ShieldConfiguration({ shieldPublishableKey })
|
|
14
22
|
* });
|
|
15
23
|
*
|
|
16
|
-
* const
|
|
24
|
+
* const accessToken = await client.getAccessToken();
|
|
25
|
+
* ```
|
|
17
26
|
*/
|
|
18
27
|
export function createOpenfortClient({ baseConfiguration, overrides, shieldConfiguration, }) {
|
|
19
28
|
const nativeAppId = getNativeApplicationId();
|
|
@@ -43,8 +52,11 @@ export function createOpenfortClient({ baseConfiguration, overrides, shieldConfi
|
|
|
43
52
|
});
|
|
44
53
|
}
|
|
45
54
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
55
|
+
* Resolves the native application identifier from the Expo runtime.
|
|
56
|
+
*
|
|
57
|
+
* @returns The native bundle identifier reported by Expo.
|
|
58
|
+
* @throws {Error} Thrown when the identifier cannot be determined, typically because the
|
|
59
|
+
* `expo-application` package is not installed or the native bundle identifier is missing.
|
|
48
60
|
*/
|
|
49
61
|
function getNativeApplicationId() {
|
|
50
62
|
if (typeof applicationId !== 'string') {
|
|
@@ -53,12 +65,18 @@ function getNativeApplicationId() {
|
|
|
53
65
|
return applicationId;
|
|
54
66
|
}
|
|
55
67
|
/**
|
|
56
|
-
* Default
|
|
57
|
-
*
|
|
68
|
+
* Default {@link OpenfortClient} instance used when an application does not supply its own.
|
|
69
|
+
*
|
|
70
|
+
* Applications should generally manage their own instance via {@link createOpenfortClient}
|
|
71
|
+
* instead of relying on this singleton.
|
|
58
72
|
*/
|
|
59
73
|
let defaultClient = null;
|
|
60
74
|
/**
|
|
61
|
-
*
|
|
75
|
+
* Retrieves the lazily initialised default {@link OpenfortClient} instance.
|
|
76
|
+
*
|
|
77
|
+
* @param options - Optional configuration used to create the client when it does not yet exist.
|
|
78
|
+
* @returns The cached {@link OpenfortClient} instance.
|
|
79
|
+
* @throws {Error} When the client has not been initialised and no configuration was provided.
|
|
62
80
|
* @internal
|
|
63
81
|
*/
|
|
64
82
|
export function getDefaultClient(options) {
|
|
@@ -71,7 +89,10 @@ export function getDefaultClient(options) {
|
|
|
71
89
|
return defaultClient;
|
|
72
90
|
}
|
|
73
91
|
/**
|
|
74
|
-
*
|
|
92
|
+
* Overrides the global default {@link OpenfortClient} instance that will be returned by
|
|
93
|
+
* {@link getDefaultClient}.
|
|
94
|
+
*
|
|
95
|
+
* @param client - The client instance to set as the default.
|
|
75
96
|
* @internal
|
|
76
97
|
*/
|
|
77
98
|
export function setDefaultClient(client) {
|
package/dist/core/context.js
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
/**
|
|
3
|
-
* React context for sharing Openfort SDK state throughout the component tree
|
|
3
|
+
* React context for sharing Openfort SDK state throughout the component tree.
|
|
4
4
|
*/
|
|
5
5
|
export const OpenfortContext = React.createContext(null);
|
|
6
6
|
/**
|
|
7
7
|
* Hook to access the Openfort context
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
|
+
* This hook provides access to the Openfort SDK context including client instance,
|
|
10
|
+
* user state, and authentication methods. Must be used within an OpenfortProvider.
|
|
11
|
+
*
|
|
12
|
+
* @returns The {@link OpenfortContextValue} from the nearest provider
|
|
13
|
+
* @throws {Error} When used outside an {@link OpenfortProvider}
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const { client, user, isReady, logout } = useOpenfortContext();
|
|
18
|
+
*
|
|
19
|
+
* // Check if SDK is ready
|
|
20
|
+
* if (isReady && user) {
|
|
21
|
+
* console.log('User authenticated:', user.id);
|
|
22
|
+
*
|
|
23
|
+
* // Access client methods
|
|
24
|
+
* const token = await client.getAccessToken();
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
9
27
|
*/
|
|
10
28
|
export function useOpenfortContext() {
|
|
11
29
|
const context = React.useContext(OpenfortContext);
|
|
@@ -17,13 +35,32 @@ export function useOpenfortContext() {
|
|
|
17
35
|
}
|
|
18
36
|
/**
|
|
19
37
|
* Hook to safely access the Openfort context
|
|
20
|
-
*
|
|
38
|
+
*
|
|
39
|
+
* This hook provides safe access to the Openfort SDK context without throwing errors.
|
|
40
|
+
* Returns null when used outside of an OpenfortProvider instead of throwing.
|
|
41
|
+
*
|
|
42
|
+
* @returns The {@link OpenfortContextValue} when available, otherwise `null`
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```tsx
|
|
46
|
+
* const context = useOpenfortContextSafe();
|
|
47
|
+
*
|
|
48
|
+
* if (context) {
|
|
49
|
+
* const { user, isReady } = context;
|
|
50
|
+
* console.log('Context available:', { user: !!user, isReady });
|
|
51
|
+
* } else {
|
|
52
|
+
* console.log('No Openfort context found');
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
21
55
|
*/
|
|
22
56
|
export function useOpenfortContextSafe() {
|
|
23
57
|
return React.useContext(OpenfortContext);
|
|
24
58
|
}
|
|
25
59
|
/**
|
|
26
|
-
*
|
|
60
|
+
* Runtime type guard that checks whether a value satisfies {@link OpenfortContextValue}.
|
|
61
|
+
*
|
|
62
|
+
* @param value - Value to check.
|
|
63
|
+
* @returns `true` when the value matches the {@link OpenfortContextValue} shape.
|
|
27
64
|
*/
|
|
28
65
|
export function isOpenfortContextValue(value) {
|
|
29
66
|
return (typeof value === 'object' &&
|
package/dist/core/provider.js
CHANGED
|
@@ -5,8 +5,12 @@ import { createOpenfortClient, setDefaultClient } from './client';
|
|
|
5
5
|
import { EmbeddedWalletWebView, WebViewUtils } from '../native';
|
|
6
6
|
import { logger, getEmbeddedStateName } from '../lib/logger';
|
|
7
7
|
/**
|
|
8
|
-
* Starts polling the embedded wallet state and
|
|
9
|
-
*
|
|
8
|
+
* Starts polling the embedded wallet state and invokes the callback when transitions occur.
|
|
9
|
+
*
|
|
10
|
+
* @param client - The Openfort client to query for embedded wallet state.
|
|
11
|
+
* @param onChange - Callback invoked whenever the state changes.
|
|
12
|
+
* @param intervalMs - Polling interval in milliseconds. Defaults to 1000ms.
|
|
13
|
+
* @returns A function that stops polling when called.
|
|
10
14
|
*/
|
|
11
15
|
function startEmbeddedStatePolling(client, onChange, intervalMs = 1000) {
|
|
12
16
|
let lastState = null;
|
|
@@ -34,8 +38,35 @@ function startEmbeddedStatePolling(client, onChange, intervalMs = 1000) {
|
|
|
34
38
|
};
|
|
35
39
|
}
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
41
|
+
* Provider component that initialises the Openfort SDK and exposes its state via {@link OpenfortContext}
|
|
42
|
+
*
|
|
43
|
+
* This component must wrap your React Native app to provide Openfort functionality to all child components.
|
|
44
|
+
* It initializes the SDK with the provided configuration and manages authentication state.
|
|
45
|
+
*
|
|
46
|
+
* @param props - Provider configuration including the publishable key and optional overrides
|
|
47
|
+
* @returns A React element that provides the Openfort context to its children
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```tsx
|
|
51
|
+
* import { OpenfortProvider } from '@openfort/react-native';
|
|
52
|
+
* import { polygon, polygonMumbai } from 'viem/chains';
|
|
53
|
+
*
|
|
54
|
+
* function App() {
|
|
55
|
+
* return (
|
|
56
|
+
* <OpenfortProvider
|
|
57
|
+
* publishableKey="pk_test_..."
|
|
58
|
+
* supportedChains={[polygon, polygonMumbai]}
|
|
59
|
+
* walletConfig={{
|
|
60
|
+
* shieldPublishableKey: "shield_pk_...",
|
|
61
|
+
* getEncryptionSession: () => fetchEncryptionSession(),
|
|
62
|
+
* }}
|
|
63
|
+
* verbose={true}
|
|
64
|
+
* >
|
|
65
|
+
* <YourAppContent />
|
|
66
|
+
* </OpenfortProvider>
|
|
67
|
+
* );
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
39
70
|
*/
|
|
40
71
|
export const OpenfortProvider = ({ children, publishableKey, customAuth, supportedChains, walletConfig, overrides, thirdPartyAuth, verbose = false, }) => {
|
|
41
72
|
// Prevent multiple OpenfortProvider instances
|
package/dist/core/storage.js
CHANGED
|
@@ -16,8 +16,9 @@ var StorageKeys;
|
|
|
16
16
|
StorageKeys["PKCE_VERIFIER"] = "openfort.pkce_verifier";
|
|
17
17
|
})(StorageKeys || (StorageKeys = {}));
|
|
18
18
|
/**
|
|
19
|
-
* Storage adapter
|
|
20
|
-
*
|
|
19
|
+
* Storage adapter backed by {@link SecureStore} that matches the {@link Storage} interface expected by `@openfort/openfort-js`.
|
|
20
|
+
*
|
|
21
|
+
* The adapter normalises the keys provided by the Openfort SDK so they can be safely persisted via Expo Secure Store.
|
|
21
22
|
*/
|
|
22
23
|
export const SecureStorageAdapter = {
|
|
23
24
|
async get(key) {
|
|
@@ -77,15 +78,19 @@ export const SecureStorageAdapter = {
|
|
|
77
78
|
},
|
|
78
79
|
};
|
|
79
80
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
81
|
+
* Normalises an Openfort storage key for use with Expo Secure Store.
|
|
82
|
+
*
|
|
83
|
+
* @param key - The key provided by the Openfort SDK.
|
|
84
|
+
* @returns A key that is safe to use with Expo Secure Store.
|
|
82
85
|
*/
|
|
83
86
|
function normalizeKey(key) {
|
|
84
87
|
return key.replaceAll(':', '-');
|
|
85
88
|
}
|
|
86
89
|
/**
|
|
87
|
-
* Creates a type-safe storage adapter that bridges
|
|
88
|
-
*
|
|
90
|
+
* Creates a type-safe storage adapter that bridges the Openfort SDK storage API with the React Native implementation.
|
|
91
|
+
*
|
|
92
|
+
* @param customStorage - Optional custom storage implementation. When omitted the {@link SecureStorageAdapter} is used.
|
|
93
|
+
* @returns An object that satisfies the {@link Storage} interface expected by `@openfort/openfort-js`.
|
|
89
94
|
*/
|
|
90
95
|
export function createNormalizedStorage(customStorage) {
|
|
91
96
|
const baseStorage = customStorage || SecureStorageAdapter;
|
|
@@ -119,7 +124,12 @@ export function createNormalizedStorage(customStorage) {
|
|
|
119
124
|
};
|
|
120
125
|
}
|
|
121
126
|
/**
|
|
122
|
-
* Converts
|
|
127
|
+
* Converts a key provided by the Openfort SDK to the local {@link StorageKeys} enum.
|
|
128
|
+
*
|
|
129
|
+
* @param key - Value provided by the Openfort SDK. Can be a string or an enum-like
|
|
130
|
+
* object.
|
|
131
|
+
* @returns The matching {@link StorageKeys} value.
|
|
132
|
+
* @throws {Error} When the key cannot be mapped to one of the known storage keys.
|
|
123
133
|
*/
|
|
124
134
|
function keyToStorageKeys(key) {
|
|
125
135
|
if (typeof key === 'string') {
|
package/dist/hooks/auth/index.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { useCallback } from "react";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Hook for creating wallets after user authentication.
|
|
4
|
+
*
|
|
5
|
+
* TODO: the implementation is currently a placeholder that always returns an
|
|
6
|
+
* undefined wallet. Once the post-auth wallet flow is wired up, this helper will
|
|
7
|
+
* attempt to provision or connect an embedded wallet automatically.
|
|
8
|
+
*
|
|
9
|
+
* @returns Object containing wallet creation utilities (placeholder for now).
|
|
10
|
+
*/
|
|
3
11
|
export const useCreateWalletPostAuth = () => {
|
|
4
12
|
// This would connect to the wallet and set it as active
|
|
5
13
|
// eslint-disable-next-line no-empty-pattern
|
|
@@ -11,6 +11,32 @@ const mapStatus = (status) => {
|
|
|
11
11
|
error: "error" in status ? status.error : null,
|
|
12
12
|
};
|
|
13
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Hook for email and password authentication.
|
|
16
|
+
*
|
|
17
|
+
* This hook provides email/password authentication flows including sign-in, sign-up, and
|
|
18
|
+
* account linking. Password reset and verification helpers are exposed but currently stubbed
|
|
19
|
+
* (TODOs) until the SDK wiring is complete.
|
|
20
|
+
*
|
|
21
|
+
* @param hookOptions - Optional configuration with callback functions and email verification settings.
|
|
22
|
+
* @returns Email authentication state and methods with flow status indicators.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* const { signInEmail, signUpEmail, linkEmail, isLoading, requiresEmailVerification } = useEmailAuth({
|
|
27
|
+
* onSuccess: ({ user }) => console.log('Email auth successful:', user?.id),
|
|
28
|
+
* onError: ({ error }) => console.error('Email auth failed:', error?.message),
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* await signUpEmail({ email: 'user@example.com', password: 'securePassword123' });
|
|
32
|
+
*
|
|
33
|
+
* if (requiresEmailVerification) {
|
|
34
|
+
* console.log('Check email for verification code');
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* await signInEmail({ email: 'user@example.com', password: 'securePassword123' });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
14
40
|
export const useEmailAuth = (hookOptions = {}) => {
|
|
15
41
|
const { client, setPasswordState, _internal, passwordState } = useOpenfortContext();
|
|
16
42
|
const signInEmail = useCallback(async (options) => {
|
|
@@ -3,6 +3,27 @@ import { useOpenfortContext } from '../../core/context';
|
|
|
3
3
|
import { onError, onSuccess } from '../../lib/hookConsistency';
|
|
4
4
|
import { mapStatus } from '../../types/baseFlowState';
|
|
5
5
|
import { OpenfortError, OpenfortErrorType } from '../../types/openfortError';
|
|
6
|
+
/**
|
|
7
|
+
* Hook for creating guest accounts.
|
|
8
|
+
*
|
|
9
|
+
* Guest accounts allow users to access certain features without full authentication and can later be upgraded to full accounts
|
|
10
|
+
* by linking additional authentication methods.
|
|
11
|
+
*
|
|
12
|
+
* @param hookOptions - Configuration options including success and error callbacks.
|
|
13
|
+
* @returns Current guest authentication helpers with flow status indicators.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const { signUpGuest, isLoading } = useGuestAuth({
|
|
18
|
+
* onSuccess: ({ user }) => console.log('Guest account created:', user),
|
|
19
|
+
* onError: ({ error }) => console.error('Failed to create guest account:', error),
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* if (!isLoading) {
|
|
23
|
+
* await signUpGuest();
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
6
27
|
export const useGuestAuth = (hookOptions = {}) => {
|
|
7
28
|
const { client, _internal } = useOpenfortContext();
|
|
8
29
|
const { refreshUserState: updateUser } = _internal;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Hook for OAuth-based login functionality
|
|
3
|
-
*/
|
|
4
1
|
import { OAuthProvider } from '@openfort/openfort-js';
|
|
5
2
|
import { useCallback } from 'react';
|
|
6
3
|
import { useOpenfortContext } from '../../core/context';
|
|
@@ -9,33 +6,30 @@ import { mapOAuthStatus } from "../../types/oauth";
|
|
|
9
6
|
import { OpenfortError, OpenfortErrorType } from '../../types/openfortError';
|
|
10
7
|
import { onError, onSuccess } from '../../lib/hookConsistency';
|
|
11
8
|
/**
|
|
12
|
-
* Hook for OAuth-based authentication with supported providers
|
|
9
|
+
* Hook for OAuth-based authentication with supported providers.
|
|
13
10
|
*
|
|
14
|
-
* This hook provides
|
|
15
|
-
*
|
|
11
|
+
* This hook provides helpers for starting OAuth login flows (`initOAuth`) and linking
|
|
12
|
+
* additional providers to an authenticated user (`linkOauth`). Some advanced flows may
|
|
13
|
+
* require manual credential storage via `storeCredentials`, which is currently a TODO.
|
|
16
14
|
*
|
|
17
|
-
* @param
|
|
18
|
-
* @returns
|
|
15
|
+
* @param hookOptions - Configuration options including success and error callbacks.
|
|
16
|
+
* @returns OAuth helpers and derived flow state flags.
|
|
19
17
|
*
|
|
20
18
|
* @example
|
|
21
19
|
* ```tsx
|
|
22
|
-
* const {
|
|
23
|
-
* onSuccess: (user) => console.log('OAuth
|
|
24
|
-
* onError: (error) => console.error('OAuth login failed:', error),
|
|
20
|
+
* const { initOAuth, linkOauth, isLoading, isError, error } = useOAuth({
|
|
21
|
+
* onSuccess: ({ user }) => console.log('OAuth completed for', user?.id),
|
|
25
22
|
* });
|
|
26
23
|
*
|
|
27
|
-
* //
|
|
28
|
-
*
|
|
24
|
+
* // Start a login flow
|
|
25
|
+
* await initOAuth({ provider: OAuthProvider.GOOGLE });
|
|
29
26
|
*
|
|
30
|
-
* //
|
|
31
|
-
*
|
|
32
|
-
* provider: 'apple',
|
|
33
|
-
* isLegacyAppleIosBehaviorEnabled: true
|
|
34
|
-
* });
|
|
27
|
+
* // Later, link another provider for the signed-in user
|
|
28
|
+
* await linkOauth({ provider: OAuthProvider.DISCORD });
|
|
35
29
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
30
|
+
* if (isError) {
|
|
31
|
+
* console.warn('Latest OAuth attempt failed', error);
|
|
32
|
+
* }
|
|
39
33
|
* ```
|
|
40
34
|
*/
|
|
41
35
|
export const useOAuth = (hookOptions = {}) => {
|
|
@@ -4,6 +4,38 @@ import { onError, onSuccess } from "../../lib/hookConsistency";
|
|
|
4
4
|
import { mapStatus } from "../../types/baseFlowState";
|
|
5
5
|
import { OpenfortError, OpenfortErrorType } from "../../types/openfortError";
|
|
6
6
|
import { useOpenfortClient } from "../core";
|
|
7
|
+
/**
|
|
8
|
+
* Hook for user sign out functionality
|
|
9
|
+
*
|
|
10
|
+
* This hook provides secure sign out capabilities that clear user authentication state
|
|
11
|
+
* and refresh the application context to reflect the unauthenticated state.
|
|
12
|
+
*
|
|
13
|
+
* @param hookOptions - Optional configuration with callback functions for handling success and error events
|
|
14
|
+
* @returns Sign out method with loading and error state indicators
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* const { signOut, isLoading, isError, error } = useSignOut({
|
|
19
|
+
* onSuccess: () => console.log('Successfully signed out'),
|
|
20
|
+
* onError: ({ error }) => console.error('Sign out failed:', error?.message),
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Sign out the current user
|
|
24
|
+
* if (!isLoading) {
|
|
25
|
+
* await signOut();
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // Handle loading states
|
|
29
|
+
* if (isLoading) {
|
|
30
|
+
* console.log('Signing out...');
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* // Handle errors
|
|
34
|
+
* if (isError && error) {
|
|
35
|
+
* console.error('Sign out error:', error.message);
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
7
39
|
export function useSignOut(hookOptions = {}) {
|
|
8
40
|
const client = useOpenfortClient();
|
|
9
41
|
const { _internal, user } = useOpenfortContext();
|
|
@@ -13,6 +13,31 @@ const mapStatus = (status) => {
|
|
|
13
13
|
isSubmittingSignature: status.status === 'submitting-signature',
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
|
+
/**
|
|
17
|
+
* Hook for handling Sign-In With Ethereum (SIWE) flows.
|
|
18
|
+
*
|
|
19
|
+
* This hook orchestrates SIWE message generation, signature submission, and state
|
|
20
|
+
* tracking so that external wallets can either authenticate a user (`signInWithSiwe`)
|
|
21
|
+
* or be linked to an existing account (`linkSiwe`).
|
|
22
|
+
*
|
|
23
|
+
* @param hookOptions - Optional callbacks for handling success or error events from the SIWE flows.
|
|
24
|
+
* @returns SIWE helpers for generating messages, signing in, linking wallets, and inspecting flow status.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* const { generateSiweMessage, signInWithSiwe, linkSiwe, isAwaitingSignature } = useWalletAuth({
|
|
29
|
+
* onSuccess: ({ user }) => console.log('SIWE flow completed for', user?.id),
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const { message } = await generateSiweMessage({
|
|
33
|
+
* wallet: connectedWallet.address,
|
|
34
|
+
* from: { domain: 'app.openfort.io', uri: 'https://app.openfort.io' },
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* const signature = await connectedWallet.signMessage(message);
|
|
38
|
+
* await signInWithSiwe({ walletAddress: connectedWallet.address, signature, messageOverride: message });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
16
41
|
export function useWalletAuth(hookOptions) {
|
|
17
42
|
const { client, siweState, setSiweState, _internal } = useOpenfortContext();
|
|
18
43
|
const generateSiweMessage = useCallback(async (args) => {
|
package/dist/hooks/core/index.js
CHANGED
|
@@ -1,41 +1,38 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core Openfort hook for accessing SDK state and methods
|
|
3
|
-
*/
|
|
4
1
|
import { useOpenfortContext } from '../../core/context';
|
|
5
2
|
/**
|
|
6
|
-
* Hook that exposes the core state of the Openfort SDK
|
|
3
|
+
* Hook that exposes the core state of the Openfort SDK.
|
|
7
4
|
*
|
|
8
|
-
* This hook provides access to the current authenticated user object,
|
|
9
|
-
* SDK initialization status, and core authentication methods.
|
|
5
|
+
* This hook provides access to the current authenticated user object, SDK initialization status, and core authentication methods.
|
|
10
6
|
*
|
|
11
|
-
* @returns The Openfort SDK's core state and methods
|
|
7
|
+
* @returns The Openfort SDK's core state and methods.
|
|
12
8
|
*
|
|
13
9
|
* @example
|
|
14
10
|
* ```tsx
|
|
15
|
-
*
|
|
11
|
+
* import { ActivityIndicator, Button, Text, View } from 'react-native';
|
|
12
|
+
* import { useOpenfort } from '@openfort/react-native/hooks';
|
|
16
13
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* return <LoadingSpinner />;
|
|
20
|
-
* }
|
|
14
|
+
* export function HomeScreen() {
|
|
15
|
+
* const { user, isReady, error, logout } = useOpenfort();
|
|
21
16
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* }
|
|
17
|
+
* if (!isReady) {
|
|
18
|
+
* return <ActivityIndicator size="large" />;
|
|
19
|
+
* }
|
|
26
20
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
21
|
+
* if (error) {
|
|
22
|
+
* return <Text>{`Failed to initialise: ${error.message}`}</Text>;
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* if (!user) {
|
|
26
|
+
* return <Text>Please sign in</Text>;
|
|
27
|
+
* }
|
|
31
28
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
29
|
+
* return (
|
|
30
|
+
* <View>
|
|
31
|
+
* <Text>{`Welcome, ${user.id}`}</Text>
|
|
32
|
+
* <Button title="Log out" onPress={() => void logout()} />
|
|
33
|
+
* </View>
|
|
34
|
+
* );
|
|
35
|
+
* }
|
|
39
36
|
* ```
|
|
40
37
|
*/
|
|
41
38
|
export function useOpenfort() {
|
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
import { useOpenfortContext } from '../../core/context';
|
|
2
2
|
/**
|
|
3
|
-
* Hook for accessing the Openfort client instance directly
|
|
3
|
+
* Hook for accessing the Openfort client instance directly.
|
|
4
4
|
*
|
|
5
|
-
* This hook
|
|
6
|
-
*
|
|
5
|
+
* This hook exposes the underlying {@link OpenfortClient} so that advanced consumer code can access low-level methods that are
|
|
6
|
+
* not surfaced through the convenience hooks.
|
|
7
7
|
*
|
|
8
|
-
* @returns The
|
|
8
|
+
* @returns The current {@link OpenfortClient} instance from context.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```tsx
|
|
12
12
|
* const client = useOpenfortClient();
|
|
13
13
|
*
|
|
14
|
-
* //
|
|
15
|
-
* const
|
|
14
|
+
* // Invoke a raw SDK method
|
|
15
|
+
* const token = await client.getAccessToken();
|
|
16
16
|
*
|
|
17
|
-
* // Access
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* // Check client status
|
|
21
|
-
* if (client.isInitialized) {
|
|
22
|
-
* // Perform operations that require initialization
|
|
23
|
-
* }
|
|
17
|
+
* // Access nested services
|
|
18
|
+
* await client.auth.logout();
|
|
24
19
|
* ```
|
|
25
20
|
*/
|
|
26
21
|
export function useOpenfortClient() {
|