1# Managing App Accounts
2
3You can use the [application account SDK](../../reference/apis-basic-services-kit/js-apis-appAccount.md) to manage app accounts.
4
5When 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.
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 App Account
22
23Create an app 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 app 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 App Account List
50
51**Procedure**
52
531. Specify the account owner.
54
55   ```ts
56   let owner: string = 'com.example.accountjsdemo2';
57   ```
58
592. Use [getAllAccounts](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getallaccounts9) to obtain the app account list.
60
61   ```ts
62   appAccountManager.getAllAccounts().then((data: appAccount.AppAccountInfo[]) => {
63       console.debug('getAllAccounts successfully, data: ' + JSON.stringify(data));
64   }).catch((err: BusinessError) => {
65       console.debug('getAllAccounts failed, error: ' + JSON.stringify(err));
66   });
67   ```
68
69## Accessing Account Credentials
70
71**Procedure**
72
731. Specify the account name, credential type, and credential.
74
75   ```ts
76   let name: string = 'ZhangSan';
77   let credentialType: string = 'PIN_SIX';
78   let credential: string = 'xxxxxx';
79   ```
80
812. Use [getCredential](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getcredential9) to obtain the account credential.
82
83   ```ts
84   appAccountManager.getCredential(name, credentialType).then((data: string) => {
85       console.log('getCredential successfully, data: ' + data);
86   }).catch((err: BusinessError) => {
87       console.log('getCredential failed, error: ' + JSON.stringify(err));
88   });
89   ```
90
913. Use [setCredential](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setcredential9) to set the account credential.
92
93   ```ts
94   appAccountManager.setCredential(name, credentialType, credential).then(() => {
95       console.log('setCredential successfully');
96   }).catch((err: BusinessError) => {
97       console.log('setCredential failed: ' + JSON.stringify(err));
98   });
99   ```
100
101## Accessing Custom Account Data
102
103**Procedure**
104
1051. Specify the account name and custom data.
106
107   ```ts
108   let name: string = 'ZhangSan';
109   let key: string = 'age';
110   let value: string = '12';
111   ```
112
1132. Use [setCustomData](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setcustomdata9) to customize account data.
114
115   ```ts
116   appAccountManager.setCustomData(name, key, value).then(() => {
117       console.log('setCustomData successfully');
118   }).catch((err: BusinessError) => {
119       console.log('setCustomData failed: ' + JSON.stringify(err));
120   });
121   ```
122
1233. Use [getCustomData](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getcustomdata9) to obtain the custom account data.
124
125   ```ts
126   appAccountManager.getCustomData(name, key).then((data: string) => {
127       console.log('getCustomData successfully, data: ' + data);
128   }).catch((err: BusinessError) => {
129       console.log('getCustomData failed, error: ' + JSON.stringify(err));
130   });
131   ```
132
133## Accessing the Account Authentication Token
134
135**Procedure**
136
1371. Specify the account name, account owner, authorization type, and authentication token.
138
139   ```ts
140   let name: string = 'ZhangSan';
141   let owner: string = 'com.example.accountjsdemo';
142   let authType: string = 'getSocialData';
143   let token: string = 'xxxxxx';
144   ```
145
1462. Use [setAuthToken](../../reference/apis-basic-services-kit/js-apis-appAccount.md#setauthtoken9) to set an authorization token for the specified authentication type.
147
148   ```ts
149   appAccountManager.setAuthToken(name, authType, token).then(() => {
150       console.log('setAuthToken successfully');
151   }).catch((err: BusinessError) => {
152       console.log('setAuthToken failed: ' + JSON.stringify(err));
153   });
154   ```
155
1563. Use [getAuthToken](../../reference/apis-basic-services-kit/js-apis-appAccount.md#getauthtoken9) to obtain the authentication token of the specified authentication type.
157
158   ```ts
159   appAccountManager.getAuthToken(name, owner, authType).then((data: string) => {
160       console.log('getAuthToken successfully, data: ' + data);
161   }).catch((err: BusinessError) => {
162       console.log('getAuthToken failed, error: ' + JSON.stringify(err));
163   });
164   ```
165
166## Removing an App Account
167
168Remove the app account after the user logs out of the system.
169
170**Procedure**
171
1721. Use [removeAccount](../../reference/apis-basic-services-kit/js-apis-appAccount.md#removeaccount9) to remove the app account.
173
174   ```ts
175   let name: string = 'Zhangsan';
176   appAccountManager.removeAccount(name).then(() => {
177       console.log('removeAccount successfully');
178   }).catch((err: BusinessError) => {
179       console.log('removeAccount failed, error: ' + JSON.stringify(err));
180   });
181   ```
182