Overview
Explore how to set up and work with the Credenza Web SDK, from creating an SDK instance to implementing its core capabilities for efficient application integration.
Create SDK Instance
Configure SDK Options
To configure the CredenzaSDK instance, you need to set up options like clientId and env.
clientId: This is your application ID. It uniquely identifies your app when making requests.env: This sets the environment for the SDK. Options includeLOCAL,STAGING, andPROD.
Choosing the correct env option is essential for proper SDK operation during development and production stages.
LOCAL: Use this for local development.STAGING: Use this for pre-production testing.PROD: Use this for the live production environment.
Choose the Correct Environment
Selecting the right environment (LOCAL, STAGING, PROD) is essential for the SDK to work properly and provide consistent user experiences. Make this choice carefully based on your development stage.
Set Up Extensions
Extensions greatly enhance the SDK's functionality by integrating various features and capabilities. Here's how you can add extensions like EvmExtension, OAuthExtension, and SuiExtension to the SDK.
Initialize SDK
Execute Initialization
To prepare the CredenzaSDK for use, you need to initialize it. This step sets up the extensions you've configured and makes the SDK ready for operations. Without initialization, the SDK components won't function as expected.
Use the initialize method after creating the SDK instance. This method initializes all specified extensions and triggers the INIT event.
Example:
// Create the SDK instance
const sdk = new CredenzaSDK({
clientId: <CLIENT_ID>,
env: CredenzaSDK.SDK_ENV.STAGING, // LOCAL | STAGING | PROD
extensions: [
new EvmExtension({chainConfig, provider),
new OAuthExtension(),
new AccountExtension(),
new SuiExtension({ suiNetwork: suiNetworkName, extensions: [new ZkLoginExtension()] })
]
});
// Initialize the SDK
await sdk.initialize();
Use SDK Features
Retrieve Access Token
To retrieve the access token, use the getAccessToken method as follows:
const token = sdk.getAccessToken(): string
The access token is used to authenticate API requests. It ensures the server can validate the user's identity and permissions.
Identify Login Provider
To determine which login provider a user is authenticated with, use the getLoginProvider method. This method returns a string identifying the login provider, such as "oauth", "evm". Knowing the login provider is important as it influences the user authentication flow, helping manage different authentication states and processes.
Check User Login Status
To check if a user is logged in, use the isLoggedIn() method. This returns a boolean value indicating the user's login status. Knowing whether a user is logged in helps you manage user sessions effectively.
If the method returns
true, the user is logged in.If the method returns
false, the user is not logged in.
To manage user sessions, check the login status periodically or upon specific events. Log out users when they are inactive or when they choose to log out.
Periodic checks
Event-based checks
Manual logout
Implementing these checks ensures that you maintain a secure and efficient user session management system.
Execute Logout
To logout, you'll use the logout method of the SDK instance. This action will destroy the access token and emit a LOGOUT event.
Call the
logoutmethod on your SDK instance:sdk.logout().The
logoutmethod destroys the current access token.A
LOGOUTevent is emitted after the token is destroyed.
Logging out is important for maintaining security, as it ensures the access token is invalidated, preventing unauthorized actions.