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 In to a Distributed Account from a System Account 56 57**Procedure** 58 591. Specify the system account and the distributed account to be logged in. Set **event** to **Ohos.account.event.LOGIN**. 60 61 ```ts 62 let localId: number = 100; 63 let distributedInfo: distributedAccount.DistributedInfo = { 64 name: 'ZhangSan', 65 id: '12345', 66 event: 'Ohos.account.event.LOGIN', 67 }; 68 ``` 69 702. Use [setOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10) to bind the specified distributed account to the current system account. 71 72 ```ts 73 distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { 74 console.log('setOsAccountDistributedInfoByLocalId successfully'); 75 }).catch((err: BusinessError) => { 76 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 77 }); 78 ``` 79 803. After the login, use [getOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#getosaccountdistributedinfobylocalid10) to obtain information of the distributed account. 81 82 ```ts 83 distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data: distributedAccount.DistributedInfo) => { 84 console.log('distributed information: ' + JSON.stringify(data)); 85 }).catch((err: BusinessError) => { 86 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 87 }); 88 ``` 89 90## Logging Out of a Distributed Account to the Current System Account 91 92**Procedure** 93 941. Specify the distributed account to be logged out. Set **event** to **Ohos.account.event.LOGOUT**. 95 96 ```ts 97 let distributedInfo: distributedAccount.DistributedInfo = { 98 name: 'ZhangSan', 99 id: '12345', 100 event: 'Ohos.account.event.LOGOUT', 101 }; 102 ``` 103 1042. Use [setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9) to log out of the specified distributed account. 105 106 ```ts 107 distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { 108 console.log('setOsAccountDistributedInfo successfully'); 109 }).catch((err: BusinessError) => { 110 console.log('setOsAccountDistributedInfo 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