---
title: Get User Account Function
slug: get-user-account-function
createdAt: 2025-01-07T10:16:38.409Z
---

This documentation provides a detailed explanation of the getAccount function, which handles user account retrieval, login authentication, and interaction with the Ethereum provider. It includes asynchronous operations to ensure smooth and non-blocking user experience.

## Function: getAccount

### Description

The getAccount function checks if a user is logged in. If not, it initiates the login process. Once the user is logged in, it retrieves the user's Ethereum account. The function also handles loading indicators and emits events upon successful account retrieval.

### Code

:::BlockQuote
async function getUserInfo() \{
&#x20;   try \{

&#x20;       // Create a new User instance
&#x20;       let user = new User();

&#x20;       // Check if the user is logged in
&#x20;       const isLogin\_status = await user.islogin();
&#x20;       console.log(isLogin\_status, 'isLogin\_status');
&#x20;       if (!isLogin\_status) \{
&#x20;           console.log("login failed, please try again later");
&#x20;           return;
&#x20;       }

&#x20;       // If user is already logged in, interact with Ethereum provider to get account
&#x20;       let account = await user.getAccount();
&#x20;       console.log("getWeb3 account", account);

&#x20;       let userId = await user.getUserId();
&#x20;       console.log("getWeb3 userId", userId);

&#x20;       let result = await user.balance()
&#x20;       if (result && result.\_balances && result.\_balances.length) \{
&#x20;           let temp = result.\_balances\[0]
&#x20;           let balance = temp / 1000000000000000000n
&#x20;           console.log("getWeb3 balance:",balance)
&#x20;       }

&#x20;   } catch (error) \{
&#x20;       console.log(error, "getUserInfo error");
&#x20;       if (error && typeof error == 'string') \{
&#x20;           console.log(error);
&#x20;       } else \{
&#x20;           console.log(error.message);
&#x20;       }
&#x20;   }
}
:::

### Detailed Explanation

1. **User Object Initialization:**
   - Creates a new instance of the User object.
2. **Check Login Status:**
   - Asynchronously checks if the user is logged in.
   - Logs the login status.
3. **Handle Not Logged In Status:**
   - Shows a loading toast if the user is not logged in.
4. **User Info Process:**
   - Logs the account, userId, balance and any error.
5. **Error Handling:**
   - Logs the error.

This setup ensures that the user account status is checked and handled as soon as the component is ready, providing a seamless experience for the user.
