Authenticate users with Connect
This guide demonstrates how to connect to users' wallets and authenticate using Stacks.js packages.
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.
The @stacks/connect
package from Stacks.js provides the tools needed to integrate this functionality seamlessly into your web app.
In this guide, you will learn how to:
- Install and set up the
@stacks/connect
package - Initiate a
userSession
with specific permission scopes - Trigger the authentication flow with the
showConnect
function - Handle pending authentication states and manage user data
The examples in this guide are designed to be minimalistic, allowing you to easily adapt and customize them to fit your application's specific needs.
Install and set up the @stacks/connect
package
First, install the @stacks/connect
package in your project.
Initiate a userSession
with specific permission scopes
After installing the @stacks/connect
package, initiate a userSession
with the following 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
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 that's used to encrypt their private data.
Handle Pending Authentication
After initiating the authentication process, you will most likely need to handle cases where the user hasn't completed the authentication flow.
- Check if there is a pending sign-in using the
isSignInPending
method of theuserSession
object. - If there is a pending sign-in, handle it by calling
handlePendingSignIn
which processes the sign-in and then utilizes theuserData
returned in a promise.
For users already signed in, you can directly access their session information and 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 steps
Last updated on