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
Authentication is a fundamental part of many web applications, ensuring that users are who they claim to be and that their data is secure. With the Stacks blockchain, user authentication involves connecting to users' wallets and managing their sessions securely.
10
10
11
-
The `@stacks/connect` package from Stacks.js provides the tools needed to integrate this functionality seamlessly into your web app.
11
+
The `@stacks/connect` package provides the tools needed to integrate this functionality seamlessly into your web app.
12
12
13
13
In this guide, you will learn how to:
14
14
15
15
1.[Install the `@stacks/connect` package](#install-the-stacksconnect-package).
16
-
2.[Initiate a `UserSession` with specific permission scopes](#initiate-a-usersession).
17
-
3.[Trigger the authentication flow with the `showConnect` function](#trigger-the-authentication-flow-with-the-showconnect-function).
18
-
4.[Handle pending authentication states and manage user data](#handle-pending-authentication).
16
+
2.[Connect to a user's wallet](#connect-to-a-users-wallet).
{/* <Callout>To see this guide in action, check out a full example [here](https://github.com/hiro-so/stacks-connect-example).</Callout> */}
21
21
@@ -27,32 +27,19 @@ In this guide, you will learn how to:
27
27
@stacks/connect
28
28
```
29
29
30
-
## Initiate a UserSession
30
+
## Connect to a user's wallet
31
31
32
-
After installing the `@stacks/connect` package, initiate a `userSession` with the following permission scopes.
32
+
After installing the `@stacks/connect` package, you can use the `connect` function to initiate a wallet connection. This will trigger a popup that allows users to select and connect their wallet.
|`store_write`| Read and write data to the user's Gaia hub in an app-specific storage bucket. |
48
-
|`publish_data`| Publish data so other users of the app can discover and interact with the user. |
49
-
50
-
For more detailed information on these scopes and other API references, visit the [Connect API](/stacks/connect/packages/connect) reference page.
51
-
52
-
## Trigger the authentication flow with the showConnect function
53
-
54
-
Create an `authenticate` function that will call `showConnect`, triggering the popup that initiates the authentication process for users. They will authenticate with a [secret key](/stacks/stacks.js/concepts/private-keys) that's used to encrypt their private data.
@@ -64,66 +51,72 @@ Create an `authenticate` function that will call `showConnect`, triggering the p
64
51
</div>
65
52
</div>
66
53
67
-
```ts
68
-
function authenticate() {
69
-
showConnect({
70
-
appDetails: {
71
-
name: 'My App',
72
-
icon: window.location.origin+'/my-app-logo.svg',
73
-
},
74
-
redirectTo: '/',
75
-
onFinish: () => {
76
-
let userData =userSession.loadUserData();
77
-
},
78
-
userSession,
79
-
});
80
-
}
81
-
```
82
-
83
-
The `showConnect` function accepts an object with the following properties:
54
+
The `connect` function stores the user's addresses in local storage by default, making it easy to persist the user's session across page reloads and browser sessions.
84
55
85
-
- The app's `name` and `icon`: provided as strings comprising the `appDetails` object property.
86
-
- The `redirectTo` string: used to provide a URL to which the user should be redirected upon successful authentication.
87
-
- The `onFinish` callback serves a similar purpose by handling successful authentication within a context of a popup window.
88
-
- The `userSession` object initiated above.
56
+
<Callout>You can customize the connection behavior by passing options to the `connect` function, such as forcing wallet selection or specifying default providers. See the [advanced usage](/stacks/connect/packages/connect#advanced-usage) section for more details.</Callout>
89
57
90
-
## Handle pending authentication
58
+
## Manage authentication state
91
59
92
-
After initiating the authentication process, you will most likely need to handle cases where the user hasn't completed the authentication flow.
60
+
You can manage the user's authentication state using the following functions:
93
61
94
-
1. Check if there is a pending sign-in using the `isSignInPending` method of the `userSession` object.
95
-
2. If there is a pending sign-in, handle it by calling `handlePendingSignIn` which processes the sign-in and then utilizes the `userData` returned in a promise.
<Callouttitle="Note">Implementing `handlePendingSignIn` is particularly important in mobile app contexts to ensure a smooth user experience across all device types.</Callout>
109
+
<Callouttitle="Note">For a list of all available methods, see the [reference](/stacks/connect/packages/connect) page.</Callout>
117
110
118
111
---
119
112
120
113
## Next steps
121
114
122
115
<Cards>
123
116
<Card
124
-
href="/stacks/connect/guides/sign-transactions"
125
-
title="Sign transactions"
126
-
description="Learn how to interact with your smart contracts using Stacks.js."
0 commit comments