更好的 Auth Tauri 插件
安装
# 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
先决条件
此插件需要下列 Tauri 插件 :
@tauri-apps/plugin-deep-link@tauri-apps/plugin-http@tauri-apps/plugin-os
请在您的 Tauri 应用程序中安装并正确配置 .
服务器设置
1. 在您中配置 Tauri 插件 auth.ts 文件
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
}),
],
});
2. 登记Tauri的深层联系计划
在你身边 tauri.conf.json 文件,添加您的深链接方案 :
{
"plugins": {
"deep-link": {
"desktop": {
"schemes": ["my-app"]
}
}
}
}
客户端设置
选项1:标准 JavaScript/ TypeScript 设置
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
},
});
备选案文2:反应设置
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
);
}
备选案文3: Svelte 设置
<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>
社会标志
首先安装正式的 Tauri 打开器插件 : https://v2.tauri.app/plugin/opener/ 页面存档备份,存于互联网档案馆
要在 Opener 插件中正确使用 Social Sign , 我们提供了一个帮助器功能 :
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>
)
}
饼干
您必须更新您的认证客户端. t , 以便在生产中使用 Tauri HTTP 插件 macOS , 以便 cookie 工作 :
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)
}
})
如何运作
此插件可以在 Tauri apps 中实现无缝认证 :
- 处理深链接: 通过您的应用程序的自定义 URL 方案正确处理认证重定向 .
- 管理认证状态:确保您的应用程序的认证状态在整个认证流程中保持一致.
- 简化Tauri一体化:移除桌面应用程序中使认证工作发挥作用所需的锅炉板.
许可证
麻省理工学院