1# Managing Application Accounts 2 3You can use the [application account SDK](../../reference/apis-basic-services-kit/js-apis-appAccount.md) to manage application accounts. 4 5When an application is uninstalled, the account data of the application will be automatically deleted. When a local account is deleted, the account data of all applications of the local account will be automatically deleted. 6 7## Before You Start 8 91. Import the **appAccount** module. 10 11 ```ts 12 import { appAccount, BusinessError } from '@kit.BasicServicesKit'; 13 ``` 14 152. Obtain an **AppAccountManager** instance. 16 17 ```ts 18 const appAccountManager = appAccount.createAppAccountManager(); 19 ``` 20 21## Creating an Application Account 22 23Create an application account for an application user. 24 25**Procedure** 26 271. Specify the account name and optional parameters. 28 29 ```ts 30 let name: string = "ZhangSan"; 31 let options: appAccount.CreateAccountOptions = { 32 customData: { 33 age: '10' 34 } 35 }; 36 ``` 37 382. Use [createAccount](../../reference/apis-basic-services-kit/js-apis-appAccount.md#createaccount9) to create an application account based on the specified parameters. 39 40 ```ts 41 try { 42 await appAccountManager.createAccount(name, options); 43 console.log('createAccount successfully'); 44 } catch (err) { 45 console.log('createAccount failed, error: ' + JSON.stringify(err)); 46 } 47 ``` 48 49## Obtaining Application Account List 50 51**Procedure** 52 53 54Use [getAllAccounts](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getallaccounts9) to obtain the application account list. 55 56 ```ts 57 appAccountManager.getAllAccounts().then((data: appAccount.AppAccountInfo[]) => { 58 console.debug('getAllAccounts successfully, data: ' + JSON.stringify(data)); 59 }).catch((err: BusinessError) => { 60 console.debug('getAllAccounts failed, error: ' + JSON.stringify(err)); 61 }); 62 ``` 63 64## Accessing Account Credentials 65 66**Procedure** 67 681. Specify the account name, credential type, and credential. 69 70 ```ts 71 let name: string = 'ZhangSan'; 72 let credentialType: string = 'PIN_SIX'; 73 let credential: string = 'xxxxxx'; 74 ``` 75 762. Use [getCredential](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getcredential9) to obtain the account credential. 77 78 ```ts 79 appAccountManager.getCredential(name, credentialType).then((data: string) => { 80 console.log('getCredential successfully, data: ' + data); 81 }).catch((err: BusinessError) => { 82 console.log('getCredential failed, error: ' + JSON.stringify(err)); 83 }); 84 ``` 85 863. Use [setCredential](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setcredential9) to set the account credential. 87 88 ```ts 89 appAccountManager.setCredential(name, credentialType, credential).then(() => { 90 console.log('setCredential successfully'); 91 }).catch((err: BusinessError) => { 92 console.log('setCredential failed: ' + JSON.stringify(err)); 93 }); 94 ``` 95 96## Accessing Custom Account Data 97 98**Procedure** 99 1001. Specify the account name and custom data. 101 102 ```ts 103 let name: string = 'ZhangSan'; 104 let key: string = 'age'; 105 let value: string = '12'; 106 ``` 107 1082. Use [setCustomData](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setcustomdata9) to customize account data. 109 110 ```ts 111 appAccountManager.setCustomData(name, key, value).then(() => { 112 console.log('setCustomData successfully'); 113 }).catch((err: BusinessError) => { 114 console.log('setCustomData failed: ' + JSON.stringify(err)); 115 }); 116 ``` 117 1183. Use [getCustomData](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getcustomdata9) to obtain the custom account data. 119 120 ```ts 121 appAccountManager.getCustomData(name, key).then((data: string) => { 122 console.log('getCustomData successfully, data: ' + data); 123 }).catch((err: BusinessError) => { 124 console.log('getCustomData failed, error: ' + JSON.stringify(err)); 125 }); 126 ``` 127 128## Accessing the Account Authentication Token 129 130**Procedure** 131 1321. Specify the account name, account owner, authorization type, and authentication token. 133 134 ```ts 135 let name: string = 'ZhangSan'; 136 let owner: string = 'com.example.accountjsdemo'; 137 let authType: string = 'getSocialData'; 138 let token: string = 'xxxxxx'; 139 ``` 140 1412. Use [setAuthToken](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setauthtoken9) to set an authorization token for the specified authentication type. 142 143 ```ts 144 appAccountManager.setAuthToken(name, authType, token).then(() => { 145 console.log('setAuthToken successfully'); 146 }).catch((err: BusinessError) => { 147 console.log('setAuthToken failed: ' + JSON.stringify(err)); 148 }); 149 ``` 150 1513. Use [getAuthToken](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getauthtoken9) to obtain the authentication token of the specified authentication type. 152 153 ```ts 154 appAccountManager.getAuthToken(name, owner, authType).then((data: string) => { 155 console.log('getAuthToken successfully, data: ' + data); 156 }).catch((err: BusinessError) => { 157 console.log('getAuthToken failed, error: ' + JSON.stringify(err)); 158 }); 159 ``` 160 161## Removing an Application Account 162 163Remove the application account after the user logs out of the system. 164 165**Procedure** 166 167Use [removeAccount](../../reference/apis-basic-services-kit/js-apis-appAccount.md#removeaccount9) to remove the application account. 168 169 ```ts 170 let name: string = 'Zhangsan'; 171 appAccountManager.removeAccount(name).then(() => { 172 console.log('removeAccount successfully'); 173 }).catch((err: BusinessError) => { 174 console.log('removeAccount failed, error: ' + JSON.stringify(err)); 175 }); 176 ``` 177 178<!--RP1--> 179<!--RP1End--> 180