Hello Hive community members! ๐
hive-authentication package in earlier posts, but never with this much jazz.โ ๏ธ Quick Note:
This is strictly for ReactJS projects! No plain vanilla, no Angular, no Vueโjust pure ReactJS flavor.
Now you know the โwhyโโletโs jump in!
npm install hive-authentication
Open up App.tsx and set up your authentication flow! ๐ฅ
import { initAioha } from '@aioha/aioha'
import { AiohaProvider } from '@aioha/react-provider'
const aioha = initAioha(
{
hivesigner: {
app: 'hive-auth-demo.app',
callbackURL: window.location.origin + '/hivesigner.html',
scope: ['login', 'vote']
},
hiveauth: {
name: 'Hive Authentication Demo',
description: 'A demo app for testing Hive authentication'
}
}
)
function App() {
return (
<AiohaProvider aioha={aioha}>
<div>Your App goes here</div>
</AiohaProvider>
);
}
import { AuthButton, useAuthStore } from 'hive-authentication';
import 'hive-authentication/build.css';
import { useAioha } from "@aioha/react-provider";
function YourComponent() {
const { currentUser, loggedInUsers } = useAuthStore();
const { aioha } = useAioha();
useEffect(() => {
const unsubscribe = useAuthStore.subscribe((state) => {
console.log('Store state changed:', state);
});
return unsubscribe;
}, []);
return (
<div>
<h1>My App</h1>
<AuthButton
onAuthenticate={handleAuthenticate}
aioha={aioha}
shouldShowSwitchUser = {true} // Optional true
onClose={() => {
console.log("AuthButton dialog closed");
}}
/>
{currentUser && (
<p>Welcome, {currentUser.username}!</p>
)}
</div>
);
}
const handleAuthenticate = async (hiveResult: HiveAuthResult): Promise<string> => {
console.log('Hive authentication result:', hiveResult);
try {
const response = await fetch("https://beta-api.distriator.com/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
challenge: hiveResult.challenge,
username: hiveResult.username,
pubkey: hiveResult.publicKey,
proof: hiveResult.proof,
}),
});
if (!response.ok) {
throw new Error(`Server authentication failed: ${response.status} ${response.statusText}`);
}
const data = await response.json();
console.log('Server response:', data);
// Return your server response as JSON string
return JSON.stringify(data);
} catch (error) {
console.error("Authentication error:", error);
throw error;
}
};
Wait for the useEffect state change messageโvoila, it works!
Yep, thatโs pretty much it. Easy as pie. ๐ฅง
Give it a spin and let me know how it powers up your Hive projects!
| Contribution | To | Hive | Ecosystem |
|---|---|---|---|
| Hive Witness Node | Hive API Node | 3Speak Video Encoder Node Operator (highest number of nodes) | 3Speak Mobile App Developer |
| Podcast App Developer | 3Shorts App Developer | 3Speak Support & Maintenance Team | Distriator Developer |
| CheckinWithXYZ | Hive Inbox | HiFind | Hive Donate App |
| Contributed to HiveAuth Mobile App | Ecency โ 3Speak Integration | Ecency โ InLeo Integration | Ecency โ Actifit Integration |
| Hive Stats App | Vote for Witness App | HiveFlutterKit | New 3Speak App |
โค๏ธ Appreciate my work? Consider supporting &
! โค๏ธ
| Vote | For | Witness |
|---|---|---|
| sagarkothari88 | ||
| threespeak |