Archive post
ππ§βπ» ReactJS, AIOHA & Hive Blockchain: Switch Accounts Like a Pro π§βπ«
β’ sagarkothari88
Seamlessly switch Hive accounts in your ReactJS app with AIOHA, auto-refresh dashboards & toast your bugs away! π
### π₯³ **Hello Awesome Hive People!**
Hey everyone! π Iβm back to sprinkle some fun & code magic on your Hive dev journey. Grab your popcorn πΏ, hold onto your avatars, because today we tackle the *mighty account switcheroo*!
---
### π **Epic Learning Journey Recap**
*Follow along my caffeine-fueled development progress:*
- [π **Day One**](https://peakd.com/hive-139531/@sagarkothari88/integrate-aioha-with-your-react-app-from-scratch-vite--tailwind--daisyui): Fresh React app + AIOHA integration!
- [π§ **Day Two**](https://peakd.com/hive-139531/@sagarkothari88/integrating-react-router-my-routing-adventures-with-react): Routing drama & rebellious NavBar π€
- [π οΈ **Day Three**](https://peakd.com/hive-139531/@sagarkothari88/from-navbar-mess-to-react-router-success-fixing-layouts-routing-and-aioha-in-one-go): Fixed Layouts, Routing & AIOHA π₯
- [π§ **Day Four**](https://peakd.com/hive-139531/@sagarkothari88/day-4-reactjs-adventures--distriator-progress-usestate-useeffect-and-env-chaos): useState, useEffect, and .env headaches π
- [π§Ή **Day Five**](https://peakd.com/hive-139531/@sagarkothari88/level-up-your-imports-using-path-aliases-for-cleaner-react--typescript-projects): Path Aliases to clean up relative path spaghetti π
- [π‘ **Day Six**](https://peakd.com/hive-139531/@sagarkothari88/day-6-learning-reactjs-calling-apis-and-creating-context-providers-or-distriator-feature-update): Create Context Provider & call API
- [π **Day Seven**](https://peakd.com/hive-139531/@sagarkothari88/learning-reactjs--vite--ts--aioha-day-7-show-a-toast-message): Show a toast message
- [π **Day Eight**](https://peakd.com/hive-139531/@sagarkothari88/day-8-secure-login-with-aioha-and-localstorage-magic): Login with AIOHA & Distriator & LocalStorage magic
- [π **Day Nine**](https://peakd.com/hive-139531/@sagarkothari88/day-9-toasts-laughs-and-custom-magic): Toasts, Laughs & Custom Magic!
---
### π€Ή **Today's Mission: Account Switching!**
Ever wished you could toggle Hive accounts in your app like flipping TV channels? πΊβ¨ With AIOHAβs React UI, account switching is as easy as meme-making!
#### **Scenario**
Youβre chilling with your-account-x dashboard, then suddenly the _urge to switch_ hits. Boom! Now youβre your-account-y. πΊ The dashboard should **instantly reload**!
---
#### 
---
### 1οΈβ£ Add necessary imports
```
import { useAioha } from β@aioha/react-uiβ;β¨
import { KeyTypes } from β@aioha/aiohaβ;β¨
import { useEffect, useState } from βreactβ;β¨
import { useNavigate } from βreact-router-domβ;
```
### 2οΈβ£ Declare your variables
```
const DashboardPage = () => {β¨
const { aioha } = useAioha();β¨
const navigate = useNavigate();
```
### 3οΈβ£ Define handler functions
```
const disconnectHandler = () => {β¨
console.log(`User Logged out`);β¨
navigate(β/β);β¨
};
const accountChangedHandler = async () => {β¨
console.log(`User switched`);
}
```
### 4οΈβ£ Spice up accountChangedHandler with ninja logic
```
const accountChangedHandler = async () => {
console.log(`User changed`);
const date = new Date();
const proof = date.toISOString();
const result = await aioha.signMessage(proof, KeyTypes.Posting);
if (result.success) {
await performLogin(
proof,
result.result,
aioha.getCurrentUser() || "",
result.publicKey || ""
);
console.log(`We have logged in with switched user`);
console.log(`now reload data using other apis`);
}
}
```
### 5οΈβ£ Add the legendary performLogin function
[Reference how `loginApi` works in this post.](https://peakd.com/hive-139531/@sagarkothari88/day-8-secure-login-with-aioha-and-localstorage-magic)
```
async function performLogin(
proof: string,
challenge: string,
username: string,
pubkey: string
) {
const loginResult = await loginApi(challenge, proof, pubkey, username);
if (loginResult == null) {
console.log("Logged in");
} else {
console.log(`Login failed: ${loginResult}`);
}
}
```
### 6οΈβ£ Hook handlers using useEffect. Like a boss! π
```
useEffect(() => {
aioha.on("disconnect", disconnectHandler);
aioha.on("account_changed", accountChangedHandler);
return () => {
aioha.off("disconnect", disconnectHandler);
aioha.off("account_changed", accountChangedHandler);
};
}, []);
```
---
## π **Final Thoughts**
Thatβs it! With just six steps, your Hive dAppβs dashboard becomes a seamless, magic-filled switching experience. β¨ If only real life worked this smoothly... π₯²
> **More power to developers on Hive! More power to Hive community! More power to Hive blockchain!**
>
> Hive to the moon, and beyond! ππ
Yay! Thanks for reading, sharing code joy, and laughing with me. If this post made you chuckle (or facepalm π€¦), let me know below!
---
### π Final Note
- I asked perplexity to help optimize this post to make it more readable and viewer-friendly.
- Here is the link where you can find both original content & improvements made by AI
- https://www.perplexity.ai/search/d631f732-c260-4a0e-b4be-6e20707434ff
---
### π My Contributions to β¦οΈ Hive Ecosystem
| Contribution | To | Hive | Ecosystem |
|--------------|----|------|-----------|
| Hive Witness Node | Hive API Node (in progress) | 3Speak Video Encoder Node Operator (highest number of nodes) | 3Speak Mobile App Developer |
| 3Speak Podcast App Developer | 3Speak Shorts App Developer | 3Speak Support & Maintenance Team | [Distriator Developer](https://distriator.com/) |
| [CheckinWithXYZ](https://checkinwith.xyz/) | [Hive Inbox](https://hive-inbox.the-hive-mobile.app/) | [HiFind](https://hifind.the-hive-mobile.app/) | [Hive Donate App](https://donate.the-hive-mobile.app/) |
| Contributed to HiveAuth Mobile App | Ecency β 3Speak Integration | Ecency β InLeo Integration | Ecency β Actifit Integration |
| [Hive Stats App](https://stats.the-hive-mobile.app/) | [Vote for Witness App](https://witness.the-hive-mobile.app/) | [HiveFlutterKit](https://hiveflutterkit.sagarkothari88.one/docs/intro) | [New 3Speak App](https://3speak.sagarkothari88.one/#/?tab=home) |
---
### π Support Back
β€οΈ **Appreciate my work? Consider supporting @threespeak & @sagarkothari88!** β€οΈ
| Vote | For | Witness |
|------|-----|---------|
| [](https://witness.the-hive-mobile.app/#/witnesses/@sagarkothari88) | [sagarkothari88](https://witness.the-hive-mobile.app/#/witnesses/@sagarkothari88) | @sagarkothari88 |
| [](https://witness.the-hive-mobile.app/#/witnesses/@threespeak) | [threespeak](https://witness.the-hive-mobile.app/#/witnesses/@threespeak) | @threespeak |
---