Skip to content

zigzag-js/better-siwp

Repository files navigation

@zig-zag/better-siwp

Sign In With Polkadot (SIWP) plugin for Better-Auth. Add wallet-based authentication to any Polkadot application with a single plugin.

Built on the SIWS standard by Talisman. Works with LunoKit, Dedot, @polkadot/extension-dapp, or any Polkadot wallet framework.

Installation

npm i @zig-zag/better-siwp

Setup

Server

import { betterAuth } from "better-auth";
import { siwp } from "@zig-zag/better-siwp";

export const auth = betterAuth({
  plugins: [
    siwp({ domain: "example.com" }),
  ],
});

Client

import { createAuthClient } from "better-auth/client";
import { siwpClient } from "@zig-zag/better-siwp/client";

export const authClient = createAuthClient({
  plugins: [siwpClient()],
});

Using with LunoKit

LunoKit provides wallet connection and message signing out of the box:

import { useAccount, useSignMessage } from "@luno-kit/react";
import { useConnectModal } from "@luno-kit/ui";
import { SiwsMessage } from "@talismn/siws";

function SignInButton() {
  const { account } = useAccount();
  const { signMessageAsync } = useSignMessage();
  const { open: openConnectModal } = useConnectModal();

  const handleSignIn = async () => {
    // Get nonce from server
    const { data } = await authClient.siwp.nonce({ walletAddress: account.address });

    // Build and sign the SIWS message
    const message = new SiwsMessage({ domain: location.host, address: account.address, nonce: data.nonce, ... }).prepareMessage();
    const { signature } = await signMessageAsync({ message });

    // Verify — session cookie set automatically
    await authClient.siwp.verify({ message, signature, walletAddress: account.address });
  };

  if (!account) return <button onClick={openConnectModal}>Connect Wallet</button>;
  return <button onClick={handleSignIn}>Sign In</button>;
}

See the full API reference for all configuration options, usage with @polkadot/extension-dapp, and detailed documentation.

Examples

Example Stack Description
examples/nextjs-lunokit Next.js + LunoKit + Dedot Wallet connection via LunoKit, signing via useSignMessage
examples/nextjs-app Next.js + @polkadot/extension-dapp Direct wallet extension API usage

Both include a polished dark-themed UI with Polkadot identicons, session persistence, and sign-out.

Development

pnpm install
pnpm build:package          # Build the plugin
pnpm dev:lunokit            # Run the LunoKit example
pnpm dev                    # Run the extension-dapp example
pnpm test                   # Run tests (25 tests, 96% coverage)

Part of the ZigZag Ecosystem

License

MIT - Yogesh Kumar

About

Sign In With Polkadot (SIWP) plugin for Better-Auth. Wallet authentication made simple.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors