NEAR Protocol Sign-In Plugin for Better Auth
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
-
Error: No accounts found. Please connect a NEAR wallet.
cause The `signIn.near()` method was called without a user having previously connected or selected a NEAR wallet, or the wallet selector failed to appear/resolve.fixEnsure the wallet selection UI is correctly presented to the user before or during the `signIn.near()` call. Upgrade to `better-near-auth@0.5.1` or newer, which includes fixes for this specific scenario. -
TypeError: betterAuth.signIn.near is not a function
cause The `BetterNearAuthPlugin` was not correctly initialized and registered with your `better-auth` instance, or the `better-auth` instance itself was not properly created.fixVerify that `new BetterNearAuthPlugin(...)` is included in the `plugins` array during the `better-auth.init()` call. Also, ensure `betterAuth` is the correct instance returned from `init()` or `useAuth()`. -
Wallet transaction failed: User rejected signature request.
cause The user explicitly declined the authentication or signature request presented by their NEAR wallet.fixHandle this error gracefully in your application's UI, informing the user that the sign-in process was canceled. Provide options for them to retry authentication.
Warnings
- breaking The underlying NEAR wallet connection and signing libraries were replaced in v0.3.0. If you were directly interacting with `fastintear`, you will need to update your code to use the new `near-kit` and `hot-connect` abstractions through `better-near-auth`'s API.
- gotcha When calling `signIn.near()` without a prior wallet connection, the system might fail to properly show the wallet selector or determine the authentication flow, potentially leading to 'No accounts found' errors. This was partially addressed in v0.5.1.
- gotcha Version 0.5.0 introduced a 'single-step authentication flow' for wallets supporting `signMessage`. This changes the internal authentication process, which might behave differently than previous versions for certain wallets or setups.
- breaking The package manager was migrated from Bun to pnpm in v0.5.0, which might affect development environments that rely on Bun's specific package resolution or lockfile format.
Install
-
npm install better-near-auth -
yarn add better-near-auth -
pnpm add better-near-auth
Imports
- BetterNearAuthPlugin
import { BetterNearAuthPlugin } from 'better-near-auth'; - init
import { init } from 'better-auth'; - signIn
const { signIn } = useAuth(); // or similar access from better-auth instance
Quickstart
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();