You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: packages/sdk-react-native/README.md
+56-55
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@
4
4
5
5
The `@turnkey/sdk-react-native` package simplifies the integration of the Turnkey API into React Native applications. It provides secure session management, authentication, and cryptographic operations using [`react-native-keychain`](https://github.com/oblador/react-native-keychain), [`@turnkey/crypto`](../crypto/), [`@turnkey/api-key-stamper`](../api-key-stamper/), and [`@turnkey/http`](../http/).
6
6
7
+
---
8
+
7
9
## **Installation**
8
10
9
11
- Install the following dependencies in your React Native project:
To enable secure authentication, the following storage keys are used:
67
+
68
+
-`@turnkey/embedded-key`: Stores the private key that corresponds to the public key used when initiating the session request to Turnkey.
69
+
-`@turnkey/session`: Default session storage key, storing the session credentials, including the private key, public key, and expiry time, which are decrypted from the credential bundle after a session is created.
70
+
-`@turnkey/session-keys`: Stores the list of stored session keys.
71
+
-`@turnkey/selected-session`: Stores the currently selected session key.
72
+
73
+
---
74
+
59
75
## **Functions Provided by the Turnkey Provider**
60
76
61
77
### **Session Management**
62
78
63
79
-`createEmbeddedKey()`: Generates a new embedded key pair and securely stores the private key.
64
-
-`createSession(bundle, expiry?)`: Creates a session from a given credential bundle with an optional expiry time.
65
-
-`clearSession()`: Clears the current session, removing all stored credentials and session data.
80
+
-`createSession({ bundle, expirationSeconds?, sessionKey? })`: Creates a session. [(API Docs)](https://docs.turnkey.com/api#tag/Sessions/operation/CreateReadWriteSession)
81
+
- If `sessionKey` is provided, the session will be stored under that key in secure storage.
82
+
- If no session exists, the first session created is **automatically selected**.
83
+
- If a session with the same `sessionKey` already exists in secure storage, an error is thrown.
84
+
-`setSelectedSession({ sessionKey })`: Selects a session by its key (Used when handling multiple sessions).
85
+
-`clearSession({ sessionKey? })`: Removes the specified session from secure storage. If no `sessionKey` is provided, the currently selected session is removed.
86
+
-`clearAllSessions()`: Clears all sessions from secure storage.
87
+
88
+
---
66
89
67
90
### **User Management**
68
91
69
-
-`updateUser()`: Updates the user's email and/or phone number.
70
-
-`refreshUser()`: Fetches the latest user data and updates the session state.
console.error("Error during passkey login:", error);
116
-
}
117
-
};
118
-
```
111
+
Most users won't need multiple sessions, but if your app requires switching between multiple sessions, here’s what you need to know:
119
112
120
-
---
113
+
This SDK supports **multiple sessions**, allowing you to create and switch between different session keys using `setSelectedSession({ sessionKey })`. When a session is selected, the client, user, and session information are updated accordingly, so that all subsequent function calls (like `updateUser` or `createWallet`) apply to the selected session.
121
114
122
-
### **Session Storage**
115
+
-**Creating a Session with a Custom Id**: You can pass a `sessionKey` when calling `createSession`. If provided, the session will be stored in secure storage under that ID, allowing for multiple sessions.
116
+
-**Switching Sessions**: Use `setSelectedSession({ sessionKey })` to switch between stored sessions. The client, user, and session information will automatically update.
117
+
-**Session Expiry Management**: Each session has an expiry time, and expired sessions will be automatically cleared.
118
+
-**Callbacks for Session Events**:
119
+
-`onSessionCreated`: Called when a session is created.
120
+
-`onSessionSelected`: Called when a session is selected.
121
+
-`onSessionExpired`: Called when a session expires.
122
+
-`onSessionCleared`: Called when a session is cleared.
123
123
124
-
To enable secure authentication, two separate keychain entries are used:
124
+
**When are multiple sessions useful?**
125
125
126
-
-`turnkey-embedded-key`: Stores the private key that corresponds to the public key used when initiating the session request to Turnkey.
127
-
-`turnkey-session`: Stores the session credentials, including the private key, public key, and expiry time, which are decrypted from the credential bundle after a session is created.
126
+
Using multiple sessions can be beneficial when enabling different authentication methods for various operations. For example, you might authenticate a user with OTP for login while using a passkey-based session for signing transactions.
128
127
129
128
---
130
129
131
130
## **Demo App**
132
131
133
-
Check out [this repository](https://github.com/tkhq/react-native-demo-wallet) for a full working example of session management in React Native.
132
+
Check out [this repository](https://github.com/tkhq/react-native-demo-wallet) for a full working example.
0 commit comments