@fabianlars/tauri-plugin-oauth

中文文档
2.1.0发布于 2026/07/07MIT OR Apache-2.0TypeScriptESM + CJS近 30 天下载 9.1万

A Tauri plugin for spawning a localhost server. Needed for some oauth flows (Login with X).

原始使用文档
文档目录10 节
  1. Why This Plugin?
  2. Installation
  3. Usage
  4. Rust
  5. TypeScript
  6. Configuration
  7. Redirecting the browser after the callback
  8. Security Considerations
  9. Contributing
  10. License

Tauri Plugin OAuth

A minimalistic Rust library and Tauri plugin for handling browser-based OAuth flows in desktop applications. This plugin spawns a temporary localhost server to capture OAuth redirects, solving the challenge of using OAuth with desktop apps.

Why This Plugin?

Many OAuth providers (like Google and GitHub) don't allow custom URI schemes ("deep links") as redirect URLs. This plugin provides a solution by:

  1. Spawning a temporary local server
  2. Capturing the OAuth redirect
  3. Passing the authorization data back to your app

Note: For an alternative approach using deep linking, see tauri-plugin-deep-link. The deep-link plugin can automatically start your app if there's no open instance.

Installation

# Cargo.toml
[dependencies]
tauri-plugin-oauth = "2"

For Tauri projects using npm or yarn:

npm install @fabianlars/tauri-plugin-oauth@2
# or
yarn add @fabianlars/tauri-plugin-oauth@2

Usage

Rust

use tauri::{command, Emitter, Window};
use tauri_plugin_oauth::start;

#[command]
async fn start_server(window: Window) -> Result<u16, String> {
    start(move |url| {
        // Because of the unprotected localhost port, you must verify the URL here.
        // Preferebly send back only the token, or nothing at all if you can handle everything else in Rust.
        let _ = window.emit("redirect_uri", url);
    })
        .map_err(|err| err.to_string())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()

        .plugin(tauri_plugin_oauth::init())
        .invoke_handler(tauri::generate_handler![start_server])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

TypeScript

import {
  start,
  cancel,
  onUrl,
  onInvalidUrl,
} from "@fabianlars/tauri-plugin-oauth";

async function startOAuthFlow() {
  try {
    const port = await start();
    console.log(`OAuth server started on port ${port}`);

    // Set up listeners for OAuth results
    await onUrl((url) => {
      console.log("Received OAuth URL:", url);
      // Handle the OAuth redirect
    });

    // Initiate your OAuth flow here
    // ...
  } catch (error) {
    console.error("Error starting OAuth server:", error);
  }
}

// Don't forget to stop the server when you're done
async function stopOAuthServer() {
  try {
    await cancel(port);
    console.log("OAuth server stopped");
  } catch (error) {
    console.error("Error stopping OAuth server:", error);
  }
}

Configuration

You can configure the plugin behavior using the OauthConfig struct:

use tauri_plugin_oauth::OauthConfig;

let config = OauthConfig {
    ports: Some(vec![8000, 8001, 8002]),
    response: Some("OAuth process completed. You can close this window.".into()),
    ..Default::default()
};

start_with_config(config, |url| {
    // Handle OAuth URL
})
.expect("Failed to start OAuth server");

Redirecting the browser after the callback

If you set the redirect_uri field, the plugin will respond to the OAuth callback with a 302 Found to that URL instead of serving an HTML response. This is useful when you want the browser to land on a real page of your app (e.g. a Tauri window URL) once the provider has hit the localhost server.

let config = OauthConfig {
    redirect_uri: Some("http://tauri.localhost/oauth/done".into()),
    ..Default::default()
};

Note: When redirect_uri is set, the handler closure receives the localhost URL the provider hit (e.g. http://127.0.0.1:<port>/?code=...&state=...) — not the full URL with the fragment (#...) that only the browser sees. If your OAuth flow returns the token in a URL fragment (implicit flow), parse window.location on the redirected page and forward the result back to your app yourself; the default (no redirect_uri) behavior uses an inline script that already does this for you.

Security Considerations

  • Always validate the received OAuth URL on your server-side before considering it authentic.
  • Use HTTPS for your OAuth flow to prevent man-in-the-middle attacks.
  • Implement proper token storage and refresh mechanisms in your application.

Contributing

Contributions are always welcome! Please feel free to submit a Pull Request.

License

This project is dual-licensed under either of the following licenses, at your option:

版本与发布记录

@fabianlars/tauri-plugin-oauth 共发布 2 个版本,首次发布于 2024/11/05;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.1.0latest2026/07/074 个月前
2.0.02024/11/052 年前

关于 @fabianlars/tauri-plugin-oauth 的常见问题

@fabianlars/tauri-plugin-oauth 是什么?

A Tauri plugin for spawning a localhost server. Needed for some oauth flows (Login with X).

如何安装 @fabianlars/tauri-plugin-oauth?

在项目目录运行 npm install @fabianlars/tauri-plugin-oauth,或使用 pnpm add / yarn add / bun add。

@fabianlars/tauri-plugin-oauth 支持 TypeScript 吗?

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

@fabianlars/tauri-plugin-oauth 支持 ESM 还是 CommonJS?

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

@fabianlars/tauri-plugin-oauth 可以商用吗?

@fabianlars/tauri-plugin-oauth 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fabianlars/tauri-plugin-oauth 的下载量有多少?

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

@fabianlars/tauri-plugin-oauth 还在维护吗?

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