1# Managing Application Accounts 2 3<!--Kit: Basic Services Kit--> 4<!--Subsystem: Account--> 5<!--Owner: @steven-q--> 6<!--Designer: @JiDong-CS1--> 7<!--Tester: @zhaimengchao--> 8<!--Adviser: @zengyawen--> 9 10You can use the [application account SDK](../../reference/apis-basic-services-kit/js-apis-appAccount.md) to manage application accounts. 11 12When 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. 13 14## Before You Start 15 161. Import the **appAccount** module. 17 18 ```ts 19 import { appAccount, BusinessError } from '@kit.BasicServicesKit'; 20 ``` 21 222. Obtain an **AppAccountManager** instance. 23 24 ```ts 25 const appAccountManager = appAccount.createAppAccountManager(); 26 ``` 27 28## Creating an Application Account 29 30Create an application account for an application user. 31 32**Procedure** 33 341. Specify the account name and optional parameters. 35 36 ```ts 37 let name: string = "ZhangSan"; 38 let options: appAccount.CreateAccountOptions = { 39 customData: { 40 age: '10' 41 } 42 }; 43 ``` 44 452. Use [createAccount](../../reference/apis-basic-services-kit/js-apis-appAccount.md#createaccount9) to create an application account based on the specified parameters. 46 47 ```ts 48 appAccountManager.createAccount(name, options).then(()=>{ 49 console.info('createAccount successfully'); 50 }).catch((err: BusinessError)=>{ 51 console.error(`createAccount failed, error: code is ${err.code}, message is ${err.message}`); 52 }); 53 ``` 54 55## Obtaining Application Account List 56 57**Procedure** 58 59 60Use [getAllAccounts](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getallaccounts9) to obtain the application account list. 61 62 ```ts 63 appAccountManager.getAllAccounts().then((data: appAccount.AppAccountInfo[]) => { 64 console.log('getAllAccounts successfully, data: ' + JSON.stringify(data)); 65 }).catch((err: BusinessError) => { 66 console.error('getAllAccounts failed, error: ' + JSON.stringify(err)); 67 }); 68 ``` 69 70## Accessing Account Credentials 71 72**Procedure** 73 741. Specify the account name, credential type, and credential. 75 76 ```ts 77 let name: string = 'ZhangSan'; 78 let credentialType: string = 'PIN_SIX'; 79 let credential: string = 'xxxxxx'; 80 ``` 81 822. Use [getCredential](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getcredential9) to obtain the account credential. 83 84 ```ts 85 appAccountManager.getCredential(name, credentialType).then((data: string) => { 86 console.log('getCredential successfully, data: ' + data); 87 }).catch((err: BusinessError) => { 88 console.error('getCredential failed, error: ' + JSON.stringify(err)); 89 }); 90 ``` 91 923. Use [setCredential](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setcredential9) to set the account credential. 93 94 ```ts 95 appAccountManager.setCredential(name, credentialType, credential).then(() => { 96 console.log('setCredential successfully'); 97 }).catch((err: BusinessError) => { 98 console.error('setCredential failed: ' + JSON.stringify(err)); 99 }); 100 ``` 101 102## Accessing Custom Account Data 103 104**Procedure** 105 1061. Specify the account name and custom data. 107 108 ```ts 109 let name: string = 'ZhangSan'; 110 let key: string = 'age'; 111 let value: string = '12'; 112 ``` 113 1142. Use [setCustomData](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setcustomdata9) to customize account data. 115 116 ```ts 117 appAccountManager.setCustomData(name, key, value).then(() => { 118 console.log('setCustomData successfully'); 119 }).catch((err: BusinessError) => { 120 console.error('setCustomData failed: ' + JSON.stringify(err)); 121 }); 122 ``` 123 1243. Use [getCustomData](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getcustomdata9) to obtain the custom account data. 125 126 ```ts 127 appAccountManager.getCustomData(name, key).then((data: string) => { 128 console.log('getCustomData successfully, data: ' + data); 129 }).catch((err: BusinessError) => { 130 console.error('getCustomData failed, error: ' + JSON.stringify(err)); 131 }); 132 ``` 133 134## Accessing the Account Authentication Token 135 136**Procedure** 137 1381. Specify the account name, account owner, authorization type, and authentication token. 139 140 ```ts 141 let name: string = 'ZhangSan'; 142 let owner: string = 'com.example.accountjsdemo'; 143 let authType: string = 'getSocialData'; 144 let token: string = 'xxxxxx'; 145 ``` 146 1472. Use [setAuthToken](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setauthtoken9) to set an authorization token for the specified authentication type. 148 149 ```ts 150 appAccountManager.setAuthToken(name, authType, token).then(() => { 151 console.log('setAuthToken successfully'); 152 }).catch((err: BusinessError) => { 153 console.error('setAuthToken failed: ' + JSON.stringify(err)); 154 }); 155 ``` 156 1573. Use [getAuthToken](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getauthtoken9) to obtain the authentication token of the specified authentication type. 158 159 ```ts 160 appAccountManager.getAuthToken(name, owner, authType).then((data: string) => { 161 console.log('getAuthToken successfully, data: ' + data); 162 }).catch((err: BusinessError) => { 163 console.error('getAuthToken failed, error: ' + JSON.stringify(err)); 164 }); 165 ``` 166 167## Removing an Application Account 168 169Remove the application account after the user logs out of the system. 170 171**Procedure** 172 173Use [removeAccount](../../reference/apis-basic-services-kit/js-apis-appAccount.md#removeaccount9) to remove the application account. 174 175 ```ts 176 let name: string = 'ZhangSan'; 177 appAccountManager.removeAccount(name).then(() => { 178 console.log('removeAccount successfully'); 179 }).catch((err: BusinessError) => { 180 console.error('removeAccount failed, error: ' + JSON.stringify(err)); 181 }); 182 ``` 183 184<!--RP1--> 185<!--RP1End--> 186