1# Managing Distributed Accounts (for System Applications Only) 2 3You can use the [distributed account SDK](../../reference/apis-basic-services-kit/js-apis-distributed-account.md) to implement smooth switchover between a distributed account and a system account. 4 5## Before You Start 6 71. Request the ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS permission. For details, see [Requesting Permissions for system_basic Applications](../../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 8 92. Import the **distributedAccount** module. 10 11 ```ts 12 import { distributedAccount, BusinessError } from '@kit.BasicServicesKit'; 13 ``` 14 153. Obtain a **DistributedAccountAbility** instance. 16 17 ```ts 18 const distributedAccountAbility = distributedAccount.getDistributedAccountAbility(); 19 ``` 20 21## Logging In to a Distributed Account from the Current System Account 22 23**Procedure** 24 251. Specify the distributed account to be logged in. Set **event** to **Ohos.account.event.LOGIN**. 26 27 ```ts 28 let distributedInfo: distributedAccount.DistributedInfo = { 29 name: 'ZhangSan', 30 id: '12345', 31 event: 'Ohos.account.event.LOGIN', 32 }; 33 ``` 34 352. Use [setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9) to log in to the distributed account. 36 37 ```ts 38 distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { 39 console.log('setOsAccountDistributedInfo successfully'); 40 }).catch((err: BusinessError) => { 41 console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 42 }); 43 ``` 44 453. After the login, use [getOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#getosaccountdistributedinfo9) to obtain information of the distributed account. 46 47 ```ts 48 distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => { 49 console.log('distributed information: ' + JSON.stringify(data)); 50 }).catch((err: BusinessError) => { 51 console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 52 }); 53 ``` 54 55## Logging Out of a Distributed Account to the Current System Account 56 57**Procedure** 58 591. Specify the distributed account to be logged out. Set **event** to **Ohos.account.event.LOGOUT**. 60 61 ```ts 62 let distributedInfo: distributedAccount.DistributedInfo = { 63 name: 'ZhangSan', 64 id: '12345', 65 event: 'Ohos.account.event.LOGOUT', 66 }; 67 ``` 68 692. Use [setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9) to log out of the specified distributed account. 70 71 ```ts 72 distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { 73 console.log('setOsAccountDistributedInfo successfully'); 74 }).catch((err: BusinessError) => { 75 console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 76 }); 77 ``` 78 79## Logging In to a Distributed Account from a System Account 80 81**Procedure** 82 831. Specify the system account and the distributed account to be logged in. Set **event** to **Ohos.account.event.LOGIN**. 84 85 ```ts 86 let localId: number = 100; 87 let distributedInfo: distributedAccount.DistributedInfo = { 88 name: 'ZhangSan', 89 id: '12345', 90 event: 'Ohos.account.event.LOGIN', 91 }; 92 ``` 93 942. Call [setOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10) to bind the specified distributed account to the current system account. 95 96 ```ts 97 distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { 98 console.log('setOsAccountDistributedInfoByLocalId successfully'); 99 }).catch((err: BusinessError) => { 100 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 101 }); 102 ``` 103 1043. After the login, use [getOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#getosaccountdistributedinfobylocalid10) to obtain information of the distributed account. 105 106 ```ts 107 distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data: distributedAccount.DistributedInfo) => { 108 console.log('distributed information: ' + JSON.stringify(data)); 109 }).catch((err: BusinessError) => { 110 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 111 }); 112 ``` 113 114## Logging Out of a Distributed Account to a System Account 115 116**Procedure** 117 1181. Specify the system account and the distributed account to be logged out. Set **event** to **Ohos.account.event.LOGOUT**. 119 120 ```ts 121 let localId: number = 100; 122 let distributedInfo: distributedAccount.DistributedInfo = { 123 name: 'ZhangSan', 124 id: '12345', 125 event: 'Ohos.account.event.LOGOUT', 126 }; 127 ``` 128 1292. Use [setOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10) to log out of the specified distributed account to the target system account. 130 131 ```ts 132 distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { 133 console.log('setOsAccountDistributedInfoByLocalId successfully'); 134 }).catch((err: BusinessError) => { 135 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 136 }); 137 ``` 138