Authenticate Users with Connect
This guide explains how to connect to users' wallets and authenticate users with the @stacks/connect package from Stacks.js.
You will learn how to:
- Integrate user authentication in your web app using
@stacks/connect
- Initiate a
userSession
with specific permission scopes - Trigger the authentication flow with the
showConnect
function - Manage pending authentication states, user data and user sign-in status
Install @stacks/connect
First thing we need to do is install the @stacks/connect
package in our project.
Initiate a userSession
with specific permission scopes
After installing the @stacks/connect
package, the next step is to initiate a userSession
with specific permission scopes.
Scope | Definition |
---|---|
store_write | Read and write data to the user's Gaia hub in an app-specific storage bucket. |
publish_data | Publish data so other users of the app can discover and interact with the user. |
userSession
object just once in your app then reference it using imports where needed.Trigger the authentication flow with the showConnect
function
Let's create an authenticate
function that will call showConnect
, triggering the popup that initiates the authentication process for users, one in which they'll authenticate with a Secret Key that's used to encrypt their private data.
Once the user confirms, they will be redirected to the installed web wallet to authenticate with the authRequest
token.
Handle Pending Authentication
After the authentication process is initiated, it's crucial to handle cases where the user hasn't completed the authentication flow. This involves managing the pending authentication state when the user is redirected back to the app without having confirmed their authentication in the popup window.
The isSignInPending
method checks if there's a pending sign-in to process. If so, handlePendingSignIn
processes the pending sign-in, allowing you to save or use the user's data post-authentication. For users already signed in, you can directly access their session information or proceed with your app's flow.
handlePendingSignIn
is particularly important in mobile app contexts to ensure a smooth user experience across all device types.Next step: Create and broadcast transactions
Once your users are connected to your app, you will need a way to prompt them to sign transactions and broadcast them to the Stacks blockchain. This step-by-step guide has you covered.
More resources
Last updated on