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

套件詳情v0.6.0

fresh_dio

用於令牌刷新的 HTTP 擬截器。Fresh 建立在 package:dio 之上,並透明地管理驗證令牌。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
2.2萬
喜歡數
169
Pub 分數
160

安裝套件

flutter pub add fresh_dio

套件介紹

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

Fresh Dio 是一個基於 Dio 的輕量級 HTTP 拦截器,可在 API 請求期間自動刷新權杖。它透明地處理驗證權杖,確保安全且無間斷地存取受保護的端點。透過攔截請求並順暢刷新過期權杖,可減少樣板程式碼並提升使用者體驗。非常適合需要強大驗證工作流程且設定簡單的應用程式。

適用情境

  • API 呼叫期間自動刷新令牌
  • 處理過期的JWT令牌
  • 保護經過驗證的API端點
  • 減少手動令牌管理
  • 提高登入會話持久性
  • 簡化驗證狀態處理

主要特色

  • 基於 Dio 建構
  • 透明的令牌刷新
  • 支援 JWT 和類似令牌
  • 所需最小設定
  • 自動攔截請求
  • 處理 401 錯誤的重試邏輯

適合對象

  • 使用 JWT 驗證的應用程式
  • 頻繁進行 API 互動的專案
  • 尋求減少認證樣板程式碼的開發者
  • 具備會話管理的行動與網頁應用程式
  • 優先考慮乾淨、可維護的驗證代碼的團隊

使用建議

  • 需要與現有的 Dio 設定進行整合
  • 僅限基於令牌的驗證流程
  • 可能需要自定義邏輯來處理非標準令牌格式
  • 依賴外部網路可用性進行刷新
  • 不適用於無狀態或無令牌的 API

原始 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.

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。