• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5## Before You Start
6
71. Import the **appAccount** module.
8
9   ```ts
10   import account_appAccount from '@ohos.account.appAccount';
11   ```
12
132. Obtain an **AppAccountManager** instance.
14
15   ```ts
16   const appAccountManager = account_appAccount.createAppAccountManager();
17   ```
18
19## Creating an App Account
20
21Create an app account for an application user.
22
23**Procedure**
24
251. Specify the account name and optional parameters.
26
27   ```ts
28   let name: string = "ZhangSan";
29   let options: account_appAccount.CreateAccountOptions = {
30     customData: {
31       age: '10'
32     }
33   };
34   ```
35
362. Use [createAccount](../reference/apis-basic-services-kit/js-apis-appAccount.md#createaccount9) to create an app account based on the specified parameters.
37
38   ```ts
39   try {
40     await appAccountManager.createAccount(name, options);
41     console.log('createAccount successfully');
42   } catch (err: BusinessError) {
43     console.log('createAccount failed, error: ' + JSON.stringify(err));
44   }
45   ```
46
47## Obtaining App Account List
48
49**Procedure**
50
511. Specify the account owner.
52
53   ```ts
54   let owner: string = 'com.example.accountjsdemo2';
55   ```
56
572. Use [getAllAccounts](../reference/apis-basic-services-kit/js-apis-appAccount.md#getallaccounts9) to obtain the app account list.
58
59   ```ts
60   appAccountManager.getAllAccounts().then((data: account_appAccount.AppAccountInfo[]) => {
61       console.debug('getAllAccounts successfully, data: ' + JSON.stringify(data));
62   }).catch((err: BusinessError) => {
63       console.debug('getAllAccounts failed, error: ' + JSON.stringify(err));
64   });
65   ```
66
67## Accessing Account Credentials
68
69**Procedure**
70
711. Specify the account name, credential type, and credential.
72
73   ```ts
74   let name: string = 'ZhangSan';
75   let credentialType: string = 'PIN_SIX';
76   let credential: string = 'xxxxxx';
77   ```
78
792. Use [getCredential](../reference/apis-basic-services-kit/js-apis-appAccount.md#getcredential9) to obtain the account credential.
80
81   ```ts
82   appAccountManager.getCredential(name, credentialType).then((data: string) => {
83       console.log('getCredential successfully, data: ' + data);
84   }).catch((err: BusinessError) => {
85       console.log('getCredential failed, error: ' + JSON.stringify(err));
86   });
87   ```
88
893. Use [setCredential](../reference/apis-basic-services-kit/js-apis-appAccount.md#setcredential9) to set the account credential.
90
91   ```ts
92   appAccountManager.setCredential(name, credentialType, credential).then(() => {
93       console.log('setCredential successfully');
94   }).catch((err: BusinessError) => {
95       console.log('setCredential failed: ' + JSON.stringify(err));
96   });
97   ```
98
99## Accessing Custom Account Data
100
101**Procedure**
102
1031. Specify the account name and custom data.
104
105   ```ts
106   let name: string = 'ZhangSan';
107   let key: string = 'age';
108   let value: string = '12';
109   ```
110
1112. Use [setCustomData](../reference/apis-basic-services-kit/js-apis-appAccount.md#setcustomdata9) to customize account data.
112
113   ```ts
114   appAccountManager.setCustomData(name, key, value).then(() => {
115       console.log('setCustomData successfully');
116   }).catch((err: BusinessError) => {
117       console.log('setCustomData failed: ' + JSON.stringify(err));
118   });
119   ```
120
1213. Use [getCustomData](../reference/apis-basic-services-kit/js-apis-appAccount.md#getcustomdata9) to obtain the custom account data.
122
123   ```ts
124   appAccountManager.getCustomData(name, key).then((data: string) => {
125       console.log('getCustomData successfully, data: ' + data);
126   }).catch((err: BusinessError) => {
127       console.log('getCustomData failed, error: ' + JSON.stringify(err));
128   });
129   ```
130
131## Accessing the Account Authentication Token
132
133**Procedure**
134
1351. Specify the account name, account owner, authorization type, and authentication token.
136
137   ```ts
138   let name: string = 'ZhangSan';
139   let owner: string = 'com.example.accountjsdemo';
140   let authType: string = 'getSocialData';
141   let token: string = 'xxxxxx';
142   ```
143
1442. Use [setAuthToken](../reference/apis-basic-services-kit/js-apis-appAccount.md#setauthtoken9) to set an authorization token for the specified authentication type.
145
146   ```ts
147   appAccountManager.setAuthToken(name, authType, token).then(() => {
148       console.log('setAuthToken successfully');
149   }).catch((err: BusinessError) => {
150       console.log('setAuthToken failed: ' + JSON.stringify(err));
151   });
152   ```
153
1543. Use [getAuthToken](../reference/apis-basic-services-kit/js-apis-appAccount.md#getauthtoken9) to obtain the authentication token of the specified authentication type.
155
156   ```ts
157   appAccountManager.getAuthToken(name, owner, authType).then((data: string) => {
158       console.log('getAuthToken successfully, data: ' + data);
159   }).catch((err: BusinessError) => {
160       console.log('getAuthToken failed, error: ' + JSON.stringify(err));
161   });
162   ```
163
164## Deleting an App Account
165
166Delete the app account after the user logs out of the system.
167
168**Procedure**
169
1701. Use [removeAccount](../reference/apis-basic-services-kit/js-apis-appAccount.md#removeaccount9) to delete the app account.
171
172   ```ts
173   let name: string = 'Zhangsan';
174   appAccountManager.removeAccount(name).then(() => {
175       console.log('removeAccount successfully');
176   }).catch((err: BusinessError) => {
177       console.log('removeAccount failed, error: ' + JSON.stringify(err));
178   });
179   ```
180