NEAR Protocol Sign-In Plugin for Better Auth

0.5.2 · active · verified Wed Apr 22

better-near-auth is a plugin for the Better Auth library, enabling 'Sign in with NEAR' (SIWN) functionality for web3 applications. It abstracts the complexities of interacting with the NEAR Protocol's authentication mechanisms, allowing developers to integrate NEAR login flows into their existing Better Auth setups. The library currently stands at version 0.5.2 and shows an active development cadence with regular patch and minor releases, indicated by recent updates addressing bug fixes and introducing new features like single-step authentication and improved wallet selection logic. Key differentiators include its tight integration with Better Auth, support for modern NEAR authentication patterns (including `signMessage`), and handling of wallet connection and account resolution within the authentication flow.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Better Auth with the BetterNearAuthPlugin and then initiate a 'Sign in with NEAR' flow. It configures the NEAR network parameters and calls `signIn.near()`.

import { init } from 'better-auth';
import { BetterNearAuthPlugin } from 'better-near-auth';

const betterAuth = init({
  plugins: [
    new BetterNearAuthPlugin({
      networkId: 'testnet', // or 'mainnet'
      nodeUrl: 'https://rpc.testnet.near.org', // or 'https://rpc.mainnet.near.org'
      walletUrl: 'https://wallet.testnet.near.org', // or 'https://wallet.near.org'
      helperUrl: 'https://helper.testnet.near.org', // or 'https://helper.near.org'
      explorerUrl: 'https://explorer.testnet.near.org' // or 'https://explorer.near.org'
    })
  ],
  // other better-auth configurations
});

async function authenticateWithNear() {
  try {
    console.log('Attempting to sign in with NEAR...');
    const user = await betterAuth.signIn.near();
    console.log('Successfully signed in:', user);
    // You can now access user.id, user.walletId, etc.
  } catch (error) {
    console.error('NEAR authentication failed:', error);
    // Handle specific errors, e.g., user canceled login
  }
}

// Call the authentication function (e.g., on a button click)
authenticateWithNear();

view raw JSON →