authentication
solana wallet-based authentication for the sdk
overview
neuranet uses solana wallet-based authentication. the flow is:
- request a nonce for your wallet address
- sign the message with your wallet
- verify the signature to get access tokens
get nonce
// Step 1: Get nonce for wallet address
const { nonce, message, expiresAt } = await client.getNonce(walletAddress);
// Returns:
// {
// nonce: "abc123...",
// message: "Sign this message to authenticate with NeuraNET...",
// expiresAt: 1234567890
// }the nonce expires after 5 minutes. request a new one if expired.
sign message
// Step 2: Sign the message with your Solana wallet
// Using @solana/wallet-adapter-react:
const { signMessage } = useWallet();
const signature = await signMessage(new TextEncoder().encode(message));
// Or with Phantom directly:
const { signature } = await window.solana.signMessage(
new TextEncoder().encode(message),
'utf8'
);authenticate
// Step 3: Authenticate with the signature
const credentials = await client.authenticate(walletAddress, signature, nonce);
// The client automatically stores credentials after authentication
// Returns:
// {
// user: { id, walletAddress, isOperator, ... },
// accessToken: "eyJhbG...",
// refreshToken: "eyJhbG...",
// expiresAt: 1234567890
// }manage credentials
// Manually set credentials (e.g., from localStorage)
client.setCredentials({
accessToken: 'your-access-token',
refreshToken: 'your-refresh-token',
expiresAt: Date.now() + 3600000,
});
// Refresh tokens when needed
await client.refreshAuth();
// Clear credentials (logout)
client.clearCredentials();
// Check if authenticated
const isAuth = client.isAuthenticated();complete example
import { NeuraNETClient } from '@neuranet/sdk';
import { useWallet } from '@solana/wallet-adapter-react';
const client = new NeuraNETClient({
baseUrl: 'https://api.neuranet.network',
});
async function authenticate() {
const { publicKey, signMessage } = useWallet();
const walletAddress = publicKey.toBase58();
// Get nonce
const { nonce, message } = await client.getNonce(walletAddress);
// Sign message
const signature = await signMessage(
new TextEncoder().encode(message)
);
// Authenticate
const { user } = await client.authenticate(
walletAddress,
signature,
nonce
);
console.log('Authenticated as:', user.id);
}