@daveyplate/better-auth-tauri

中文文档
0.1.9发布于 2026/06/04MITTypeScriptESM + CJS零依赖近 30 天下载 1万

Better Auth Tauri Plugin

原始使用文档
文档目录13 节
  1. Installation
  2. Prerequisites
  3. Server Setup
  4. 1. Configure the Tauri Plugin in your auth.ts file
  5. 2. Register the Deep Link Scheme in Tauri
  6. Client Setup
  7. Option 1: Standard JavaScript/TypeScript Setup
  8. Option 2: React Setup
  9. Option 3: Svelte Setup
  10. Social Sign In
  11. macOS Cookies
  12. How It Works
  13. License

Better Auth Tauri Plugin

A plugin for Better Auth that provides seamless integration with Tauri applications.

Installation

# npm
npm install @daveyplate/better-auth-tauri

# pnpm
pnpm add @daveyplate/better-auth-tauri

# yarn
yarn add @daveyplate/better-auth-tauri

# bun
bun add @daveyplate/better-auth-tauri

Prerequisites

This plugin requires the following Tauri plugins:

  • @tauri-apps/plugin-deep-link
  • @tauri-apps/plugin-http
  • @tauri-apps/plugin-os

Make sure these are installed and properly configured in your Tauri application.

Server Setup

1. Configure the Tauri Plugin in your auth.ts file

import { betterAuth } from "better-auth";
import { tauri } from "@daveyplate/better-auth-tauri/plugin";

export const auth = betterAuth({
  // Your existing Better Auth configuration
  plugins: [
    // Your existing plugins
    tauri({
      scheme: "your-app", // Your app's deep link scheme
      callbackURL: "/", // Optional: Where to redirect after auth (default: "/")
      successText: "Authentication successful! You can close this window.", // Optional
      successURL: "/auth/success", // Optional: Custom success page URL that will receive a ?redirectTo search parameter
      debugLogs: false, // Optional: Enable debug logs
    }),
  ],
});

In your tauri.conf.json file, add your deep link scheme:

{
    "plugins": {
        "deep-link": {
            "desktop": {
                "schemes": ["my-app"]
            }
        }
    }
}

Client Setup

Option 1: Standard JavaScript/TypeScript Setup

import { setupBetterAuthTauri } from "@daveyplate/better-auth-tauri";
import { authClient } from "./your-auth-client";

// Initialize in your app's entry point
setupBetterAuthTauri({
  authClient, // Your Better Auth client instance
  scheme: "your-app", // Must match the scheme in your server config
  debugLogs: false, // Optional: Enable debug logs
  mainWindowLabel: "main", // Optional: Your main window label (default: "main")
  onRequest: (href) => {
    console.log("Auth request:", href);
  },
  onSuccess: (callbackURL) => {
    console.log("Auth successful, callback URL:", callbackURL);
    // Handle successful authentication
    window.location.href = callbackURL
  },
  onError: (error) => {
    console.error("Auth error:", error);
    // Handle authentication error
  },
});

Option 2: React Setup

import { useBetterAuthTauri } from "@daveyplate/better-auth-tauri/react";
import { authClient } from "./your-auth-client";

function App() {
  useBetterAuthTauri({
    authClient,
    scheme: "your-app",
    debugLogs: false,
    onRequest: (href) => {
      console.log("Auth request:", href);
    },
    onSuccess: (callbackURL) => {
      console.log("Auth successful");
      // Navigate or update UI as needed
    },
    onError: (error) => {
      console.error("Auth error:", error);
      // Show error notification
    },
  });

  return (
    // Your app components
  );
}

Option 3: Svelte Setup

<script>
  import { onMount, onDestroy } from "svelte";
  import { setupBetterAuthTauri } from "@daveyplate/better-auth-tauri";
  import { authClient } from "./your-auth-client";
  
  let cleanup;
  
  onMount(() => {
    cleanup = setupBetterAuthTauri({
      authClient,
      scheme: "your-app",
      debugLogs: false,
      onRequest: (href) => {
        console.log("Auth request:", href);
      },
      onSuccess: (callbackURL) => {
        console.log("Auth successful");
        // Update your app state

        goto(callbackURL)
      },
      onError: (error) => {
        console.error("Auth error:", error);
        // Show error notification
      },
    });
  });
  
  onDestroy(() => {
    cleanup?.();
  });
</script>

Social Sign In

First install the official Tauri Opener plugin: https://v2.tauri.app/plugin/opener/

To properly use Social Sign in with the Opener plugin we provide a helper function:

import { signInSocial } from "@daveyplate/better-auth-tauri"
import { authClient } from "@/lib/auth-client"

export function Page() {
  return (
    <button onClick={() => {
      signInSocial({
        authClient,
        provider: "google"
      })
    }}>
      Sign in with Google
    </button>
  )
}

macOS Cookies

You must update your auth-client.ts to use Tauri HTTP plugin for macOS in production in order for cookies to work:

import { isTauri } from "@tauri-apps/api/core"
import { fetch as tauriFetch } from "@tauri-apps/plugin-http"
import { platform } from "@tauri-apps/plugin-os"
import { createAuthClient } from "better-auth/react"

export const authClient = createAuthClient({
    fetchOptions: {
        customFetchImpl: (...params) =>
            isTauri() && platform() === "macos" && window.location.protocol === "tauri:"
                ? tauriFetch(...params)
                : fetch(...params)
    }
})

How It Works

This plugin enables seamless authentication in Tauri apps by:

  1. Handling Deep Links: Properly processes authentication redirects through your app's custom URL scheme.
  2. Managing Authentication State: Ensures that your app's auth state remains consistent across the authentication flow.
  3. Simplifying Tauri Integration: Removes the boilerplate needed to make authentication work in desktop applications.

License

MIT

版本与发布记录

@daveyplate/better-auth-tauri 共发布 33 个版本,首次发布于 2025/04/26;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.1.9latest2026/06/045 个月前
0.1.82026/06/035 个月前
0.1.62025/09/231.1 年前
0.1.52025/09/231.1 年前
0.1.42025/09/231.1 年前
0.1.32025/09/231.1 年前

关于 @daveyplate/better-auth-tauri 的常见问题

@daveyplate/better-auth-tauri 是什么?

Better Auth Tauri Plugin

如何安装 @daveyplate/better-auth-tauri?

在项目目录运行 npm install @daveyplate/better-auth-tauri,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@tauri-apps/api(>=2.5.0)、@tauri-apps/plugin-deep-link(>=2.2.1)、@tauri-apps/plugin-http(>=2.4.3)、@tauri-apps/plugin-opener(>=2.2.6)、@tauri-apps/plugin-os(>=2.2.1)、better-auth(>=1.2.7)、react(>=18.0.0)、react-dom(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@daveyplate/better-auth-tauri 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@daveyplate/better-auth-tauri 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@daveyplate/better-auth-tauri 可以商用吗?

@daveyplate/better-auth-tauri 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@daveyplate/better-auth-tauri 的下载量有多少?

根据 npm 官方下载统计,@daveyplate/better-auth-tauri 在 2026/10/12 – 2026/11/10 这 30 天内被下载 10,078 次,日均约 336 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@daveyplate/better-auth-tauri 还在维护吗?

最新版本 0.1.9 发布于 2026/06/04(5 个月前);至今共发布 33 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/04/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。