Better Auth Plugin for Solana Sign-In

1.0.0 · active · verified Wed Apr 22

better-auth-solana is a specialized plugin for the Better Auth authentication framework, designed to facilitate "Sign in With Solana" (SIWS) functionality. Currently at version 1.0.0, it provides a stable, initial release enabling Solana wallet integration into Better Auth applications. The package includes server-side endpoints for managing the SIWS flow (nonce generation, signature verification, wallet linking) and client-side utilities for constructing SIWS messages and interacting with the Better Auth client. It natively handles session establishment and account creation/linking within the Better Auth ecosystem, using its dedicated `siws` plugin namespace. The package is published as ESM-only, aligning with better-auth's modern module strategy.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a complete client-side 'Sign in With Solana' (SIWS) flow, including nonce retrieval, message creation, wallet signing (mocked), and session verification with Better Auth.

import { createAuthClient } from 'better-auth/client'
import { createSIWSInput, siwsClient } from 'better-auth-solana/client'

// Placeholder for your wallet's sign function
async function signWithYourWallet(siwsInput: any): Promise<{ message: string; signature: string }> {
  console.log('Signing message with wallet:', siwsInput)
  // In a real app, this would involve connecting to a Solana wallet (e.g., Phantom, Solflare)
  // and using its sign message functionality. For demonstration, we'll return a mock.
  const mockSignature = 'mock_signature_' + Date.now()
  return { message: JSON.stringify(siwsInput), signature: mockSignature }
}

// Assume 'address' is the connected Solana wallet address
const address = 'GMtGgT8d6R3FwYxK2c3L4p5Q6r7S8t9U0v1W2x3Y4z5'

const authClient = createAuthClient({
  plugins: [siwsClient()],
})

async function performSolanaSignIn() {
  try {
    const nonceResult = await authClient.siws.nonce({
      walletAddress: address,
    })

    if (!nonceResult.data) {
      throw new Error('Failed to request SIWS nonce')
    }

    const siwsInput = createSIWSInput({
      address,
      challenge: nonceResult.data,
      statement: 'Sign in to Example',
      domain: 'example.com', // Must match the domain configured on the server
      uri: 'https://example.com/siws', // The current application URI
      chainId: 'solana:mainnet', // Solana network chain ID
      version: '1' // SIWS version
    })

    const signed = await signWithYourWallet(siwsInput)

    await authClient.siws.verify({
      message: signed.message,
      signature: signed.signature,
      walletAddress: address,
    })

    const session = await authClient.getSession()
    console.log('Successfully signed in:', session)
  } catch (error) {
    console.error('SIWS sign-in failed:', error)
  }
}

performSolanaSignIn()

view raw JSON →