Example Codes in React & TypeScript
In this Section, you will see examples of how to use the functions provided by the SDK to integrate authentication, user management, and Aptos transaction execution into your React project. All the example uses TypeScript. However, the SDK can also be used with React and JavaScript in the same way, as shown in the examples.
Functions Overview
1. Authenticating user with Google Sign-In
This function allows users to sign in with Google using OAuthProvider.
Parameters:
logEnabled (Optional)
: Enable/disable logging. The default is true.
Return Type:
Promise<SignInWithGoogleReturn>
:{ success: true; user: UserData; } if successful.
{ success: false; message: string; } if unsuccessful.
Errors:
The function can throw an error if the sign-in operation fails. The error message is logged if logging is enabled.
Example:
Here is an example of a React component that imports and uses the signInWithGoogle
function:
2. Signing Out a User
This function is used to sign out a user from the application. It uses Firebase's signOut
function to sign out the user.
Parameters:
logEnabled (Optional)
: Enable/disable logging. The default is true.
Return Type:
Promise<SignOutUserReturn>
:{ success: true; } if successful.
{ success: false; message: string; } if unsuccessful.
Errors:
The function can throw an error if the sign-out operation fails. The error message is logged if logging is enabled.
Example:
Here is an example of a React component that imports and uses the signOutUser
function:
3. Get the details of a User
This function is an asynchronous function that fetches a user's profile from the server.
Parameters:
email
: The email of the user.logEnabled (Optional)
: Enable/disable logging. The default is true.
Return Type:
Promise<GetUserProfileReturn>:
{ success: true; data: UserData; } if successful.
{ success: false; data: string; } if unsuccessful.
Errors:
The function can throw an error if the fetch operation fails. The error message is logged if logging is enabled.
Example:
Here is an example of a React component that imports and uses the getUserProfile
function:
4. Generic Transactions on Aptos Testnet
This is an asynchronous function that executes a generic Aptos transaction.
Parameters:
args (AptosTransactionFuncArgs)
: The arguments for the generic Aptos transaction. It is an object that includes the following properties:email (string)
: The email of the user.walletAddress (string)
: The wallet address of the currently signed-in user.contractAddress (string)
: The contract address.contractName (string)
: The contract name.functionName (string)
: The function name.arguments (Array)
: The arguments for the transaction. Each argument is an object that includes the following properties:argument (string)
: The argument used by the function.type (string)
: The type of the argument. It can bestring
,number
, orbyte_array
.
usePaymaster (Optional)
: Enable/disable the use of paymaster. The default is false. If you enable paymaster the transaction will be sponsored. Also, if the wallet doesn't exist, it will be created automatically before the transaction.logEnabled (Optional)
: Enable/disable logging. The default is true.
Return Type:
Promise<AptosTransactionReturn>:
{ success: true; data: AptosTransactionData; } if successful.
{ success: false; data: string; } if unsuccessful.
Errors:
The function can throw an error if the transaction fails. The error message is logged if logging is enabled.
Example:
Here is an example of a React component that imports and uses the aptosTransaction
function to perform a simple transfer transaction:
Last updated