1# Managing Distributed Accounts (for System Applications Only) 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 [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. 11 12## Before You Start 13 141. 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). 15 162. Import the **distributedAccount** module. 17 18 ```ts 19 import { distributedAccount, BusinessError } from '@kit.BasicServicesKit'; 20 ``` 21 223. Obtain a **DistributedAccountAbility** instance. 23 24 ```ts 25 const distributedAccountAbility = distributedAccount.getDistributedAccountAbility(); 26 ``` 27 28## Logging In to a Distributed Account from the Current System Account 29 30**Procedure** 31 321. Specify the distributed account to be logged in. Set **event** to **Ohos.account.event.LOGIN**. 33 34 ```ts 35 let distributedInfo: distributedAccount.DistributedInfo = { 36 name: 'ZhangSan', 37 id: '12345', 38 event: 'Ohos.account.event.LOGIN', 39 }; 40 ``` 41 422. Use [setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9) to log in to the distributed account. 43 44 ```ts 45 distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { 46 console.log('setOsAccountDistributedInfo successfully'); 47 }).catch((err: BusinessError) => { 48 console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 49 }); 50 ``` 51 523. After the login, use [getOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#getosaccountdistributedinfo9) to obtain information of the distributed account. 53 54 ```ts 55 distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => { 56 console.log('distributed information: ' + JSON.stringify(data)); 57 }).catch((err: BusinessError) => { 58 console.error('getOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 59 }); 60 ``` 61 62## Logging Out of a Distributed Account to the Current System Account 63 64**Procedure** 65 661. Specify the distributed account to be logged out. Set **event** to **Ohos.account.event.LOGOUT**. 67 68 ```ts 69 let distributedInfo: distributedAccount.DistributedInfo = { 70 name: 'ZhangSan', 71 id: '12345', 72 event: 'Ohos.account.event.LOGOUT', 73 }; 74 ``` 75 762. Use [setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9) to log out of the specified distributed account. 77 78 ```ts 79 distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { 80 console.log('setOsAccountDistributedInfo successfully'); 81 }).catch((err: BusinessError) => { 82 console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 83 }); 84 ``` 85 86## Logging In to a Distributed Account from a System Account 87 88**Procedure** 89 901. Specify the system account and the distributed account to be logged in. Set **event** to **Ohos.account.event.LOGIN**. 91 92 ```ts 93 let localId: number = 100; 94 let distributedInfo: distributedAccount.DistributedInfo = { 95 name: 'ZhangSan', 96 id: '12345', 97 event: 'Ohos.account.event.LOGIN', 98 }; 99 ``` 100 1012. 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. 102 103 ```ts 104 distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { 105 console.log('setOsAccountDistributedInfoByLocalId successfully'); 106 }).catch((err: BusinessError) => { 107 console.error('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 108 }); 109 ``` 110 1113. After the login, use [getOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#getosaccountdistributedinfobylocalid10) to obtain information of the distributed account. 112 113 ```ts 114 distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data: distributedAccount.DistributedInfo) => { 115 console.log('distributed information: ' + JSON.stringify(data)); 116 }).catch((err: BusinessError) => { 117 console.error('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 118 }); 119 ``` 120 121## Logging Out of a Distributed Account to a System Account 122 123**Procedure** 124 1251. Specify the system account and the distributed account to be logged out. Set **event** to **Ohos.account.event.LOGOUT**. 126 127 ```ts 128 let localId: number = 100; 129 let distributedInfo: distributedAccount.DistributedInfo = { 130 name: 'ZhangSan', 131 id: '12345', 132 event: 'Ohos.account.event.LOGOUT', 133 }; 134 ``` 135 1362. 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. 137 138 ```ts 139 distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { 140 console.log('setOsAccountDistributedInfoByLocalId successfully'); 141 }).catch((err: BusinessError) => { 142 console.error('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 143 }); 144 ``` 145