搜尋套件、儲存庫與 APP 分類

套件詳情v0.7.0

fresh

一個適用於 Dart 的權杖重新整理程式庫。本套件公開了各種重新整理權杖實作(REST、GraphQL 等)共用的核心元件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
8.7萬
喜歡數
157
Pub 分數
160

安裝套件

flutter pub add fresh

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Fresh 是一個 Dart 庫,專為管理 REST 和 GraphQL API 中的權杖刷新工作流程而設計。它提供可重用的元件來處理驗證權杖,確保安全且一致的權杖更新,而不需重複邏輯。該套件抽象了常見的模式,例如權杖過期檢測、自動刷新請求和錯誤處理,讓在 Flutter 及其他 Dart 應用中實現穩健的驗證流程變得更容易。此套件以模組化為設計重點,支援多種平台,包括行動裝置、網頁與桌面。

適用情境

  • 處理過期的 JWT 條碼
  • 在 REST API 中自動刷新權杖
  • 管理 GraphQL 客戶端中的驗證狀態
  • 使用有效令牌保護 API 呼叫
  • 實現彈性身分驗證流程

主要特色

  • 模組化權杖刷新架構
  • 對 REST 和 GraphQL 的支援
  • 跨平台相容性
  • 失敗請求的自動重試
  • 可設定的重新整理策略

適合對象

  • Flutter 應用程式與 API 驗證
  • 使用 JWT 或類似令牌的應用程式
  • 開發可重用的驗證系統
  • 需要安全令牌管理的項目
  • 跨平台應用程式

使用建議

  • 需要為特定的認證端點自定義實現
  • 可能需要與 HTTP 客戶端(如 dio 或 http)整合
  • 不是完整的認證解決方案,而是一個重新整理工具
  • 依賴於正確的後端權杖刷新設定
  • 僅限權杖重新整理—不包含登入或登出

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Fresh 🍋

build License: MIT GitHub stars


A collection of packages for automatic token refresh in Dart and Flutter. Fresh handles refreshing, caching, and attaching authentication tokens transparently so your API calls just work.

Why Fresh?

Token-based authentication seems simple until you handle the edge cases: tokens expire mid-session, multiple requests fail at the same time triggering duplicate refreshes, refresh tokens get revoked, and you need to route users to login when auth is lost. Fresh handles all of this so you don't have to.

  • No more manual 401 handling scattered across your codebase
  • No more race conditions when concurrent requests trigger simultaneous refreshes
  • No more stale tokens causing request failures - expired tokens are refreshed proactively before the request is even sent

Packages

Package Pub
fresh pub package
fresh_dio pub package
fresh_graphql pub package
fresh_http pub package

Features

  • Automatic token refresh on 401 / auth errors, with automatic request retry
  • Proactive refresh before requests when the token is expired
  • Single-flight refresh - concurrent requests share one refresh call instead of triggering multiple
  • authenticationStatus stream for reacting to login/logout events
  • Pluggable TokenStorage - bring your own persistence layer
  • Built-in OAuth2Token with expiresAt support

Usage

fresh_dio
final dio = Dio();

dio.interceptors.add(
  Fresh.oAuth2(
    tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
    refreshToken: (token, client) async {
      final response = await client.post(
        'https://api.example.com/auth/refresh',
        data: {'refresh_token': token?.refreshToken},
      );
      return OAuth2Token(
        accessToken: response.data['access_token'],
        refreshToken: response.data['refresh_token'],
      );
    },
  ),
);

See the fresh_dio README for full documentation.

fresh_graphql
import 'dart:convert';

final freshLink = FreshLink.oAuth2(
  tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
  refreshToken: (token, client) async {
    final response = await client.post(
      Uri.parse('https://api.example.com/auth/refresh'),
      body: jsonEncode({'refresh_token': token?.refreshToken}),
    );
    final body = jsonDecode(response.body) as Map<String, dynamic>;
    return OAuth2Token(
      accessToken: body['access_token'],
      refreshToken: body['refresh_token'],
    );
  },
  shouldRefresh: (response) =>
      response.errors?.any((e) => e.message.contains('UNAUTHENTICATED')) ?? false,
);

// HttpLink comes from package:gql_http_link
final link = Link.from([freshLink, HttpLink('https://api.example.com/graphql')]);

See the fresh_graphql README for full documentation.

fresh_http
import 'dart:convert';

final client = Fresh.oAuth2(
  tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
  refreshToken: (token, client) async {
    final response = await client.post(
      Uri.parse('https://api.example.com/auth/refresh'),
      body: jsonEncode({'refresh_token': token?.refreshToken}),
    );
    final body = jsonDecode(response.body) as Map<String, dynamic>;
    return OAuth2Token(
      accessToken: body['access_token'],
      refreshToken: body['refresh_token'],
    );
  },
);

See the fresh_http README for full documentation.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。

所在榜單