Managing App Accounts
You can use the application account SDK to manage app accounts.
When an app is uninstalled, the account data of the app will be automatically deleted. When a local account is deleted, the account data of all apps of the local account will be automatically deleted.
Before You Start
-
Import the appAccount module.
import account_appAccount from '@ohos.account.appAccount';
-
Obtain an AppAccountManager instance.
const appAccountManager = account_appAccount.createAppAccountManager();
Creating an App Account
Create an app account for an application user.
Procedure
-
Specify the account name and optional parameters.
let name: string = "ZhangSan"; let options: account_appAccount.CreateAccountOptions = { customData: { age: '10' } };
-
Use createAccount to create an app account based on the specified parameters.
try { await appAccountManager.createAccount(name, options); console.log('createAccount successfully'); } catch (err: BusinessError) { console.log('createAccount failed, error: ' + JSON.stringify(err)); }
Obtaining App Account List
Procedure
-
Specify the account owner.
let owner: string = 'com.example.accountjsdemo2';
-
Use getAllAccounts to obtain the app account list.
appAccountManager.getAllAccounts().then((data: account_appAccount.AppAccountInfo[]) => { console.debug('getAllAccounts successfully, data: ' + JSON.stringify(data)); }).catch((err: BusinessError) => { console.debug('getAllAccounts failed, error: ' + JSON.stringify(err)); });
Accessing Account Credentials
Procedure
-
Specify the account name, credential type, and credential.
let name: string = 'ZhangSan'; let credentialType: string = 'PIN_SIX'; let credential: string = 'xxxxxx';
-
Use getCredential to obtain the account credential.
appAccountManager.getCredential(name, credentialType).then((data: string) => { console.log('getCredential successfully, data: ' + data); }).catch((err: BusinessError) => { console.log('getCredential failed, error: ' + JSON.stringify(err)); });
-
Use setCredential to set the account credential.
appAccountManager.setCredential(name, credentialType, credential).then(() => { console.log('setCredential successfully'); }).catch((err: BusinessError) => { console.log('setCredential failed: ' + JSON.stringify(err)); });
Accessing Custom Account Data
Procedure
-
Specify the account name and custom data.
let name: string = 'ZhangSan'; let key: string = 'age'; let value: string = '12';
-
Use setCustomData to customize account data.
appAccountManager.setCustomData(name, key, value).then(() => { console.log('setCustomData successfully'); }).catch((err: BusinessError) => { console.log('setCustomData failed: ' + JSON.stringify(err)); });
-
Use getCustomData to obtain the custom account data.
appAccountManager.getCustomData(name, key).then((data: string) => { console.log('getCustomData successfully, data: ' + data); }).catch((err: BusinessError) => { console.log('getCustomData failed, error: ' + JSON.stringify(err)); });
Accessing the Account Authentication Token
Procedure
-
Specify the account name, account owner, authorization type, and authentication token.
let name: string = 'ZhangSan'; let owner: string = 'com.example.accountjsdemo'; let authType: string = 'getSocialData'; let token: string = 'xxxxxx';
-
Use setAuthToken to set an authorization token for the specified authentication type.
appAccountManager.setAuthToken(name, authType, token).then(() => { console.log('setAuthToken successfully'); }).catch((err: BusinessError) => { console.log('setAuthToken failed: ' + JSON.stringify(err)); });
-
Use getAuthToken to obtain the authentication token of the specified authentication type.
appAccountManager.getAuthToken(name, owner, authType).then((data: string) => { console.log('getAuthToken successfully, data: ' + data); }).catch((err: BusinessError) => { console.log('getAuthToken failed, error: ' + JSON.stringify(err)); });
Deleting an App Account
Delete the app account after the user logs out of the system.
Procedure
-
Use removeAccount to delete the app account.
let name: string = 'Zhangsan'; appAccountManager.removeAccount(name).then(() => { console.log('removeAccount successfully'); }).catch((err: BusinessError) => { console.log('removeAccount failed, error: ' + JSON.stringify(err)); });