🚀🧑‍💻 ReactJS, AIOHA & Hive Blockchain: Switch Accounts Like a Pro 🧑‍🏫

Words
427
Reading
2 min
Listen
Play
1y

🥳 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:


🤹 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!


account switching


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.

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


🚀 My Contributions to ♦️ Hive Ecosystem

ContributionToHiveEcosystem
Hive Witness NodeHive API Node (in progress)3Speak Video Encoder Node Operator (highest number of nodes)3Speak Mobile App Developer
3Speak Podcast App Developer3Speak Shorts App Developer3Speak Support & Maintenance TeamDistriator Developer
CheckinWithXYZHive InboxHiFindHive Donate App
Contributed to HiveAuth Mobile AppEcency ↔ 3Speak IntegrationEcency ↔ InLeo IntegrationEcency ↔ Actifit Integration
Hive Stats AppVote for Witness AppHiveFlutterKitNew 3Speak App

🙌 Support Back

❤️ Appreciate my work? Consider supporting threespeak@threespeak & sagarkothari88@sagarkothari88! ❤️

VoteForWitness
sagarkothari88sagarkothari88@sagarkothari88
threespeakthreespeak@threespeak

🚀🧑‍💻 ReactJS, AIOHA & Hive Blockchain: Switch Accounts Like a Pr... | Ecency