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

套件詳情v0.8.0

fresh_graphql

用於令牌刷新的 GraphQL 連結。基於 package:graphql 建構,並透明地管理驗證令牌。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
3410
喜歡數
38
Pub 分數
160

安裝套件

flutter pub add fresh_graphql

套件介紹

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

Fresh GraphQL 是一個 Flutter/Dart 套件,透過提供 GraphQL 請求的自動權杖刷新機制,擴展了 graphql 套件的功能。它無縫處理驗證權杖,確保在請求過期前進行刷新。建構於核心 graphql 套件之上,簡化了需要持續驗證的應用程式中的安全 API 通訊。非常適合使用 JWT 或類似基於權杖的驗證系統的應用程式。

適用情境

  • 使用自動刷新安全存取GraphQL API
  • 無需使用者干預即可處理過期權杖
  • 與基於 JWT 的驗證整合
  • 跨應用重啟管理已認證會話
  • 簡化身份驗證令牌的狀態管理

主要特色

  • 自動令牌刷新
  • 與 graphql 包的透明整合
  • 支援自定義權杖刷新邏輯
  • 內建的失敗請求重試機制
  • 跨平台相容性

適合對象

  • 使用 JWT 或 OAuth2 的應用
  • 尋求無縫驗證處理的開發者
  • 頻繁呼叫 GraphQL 的專案
  • 注重開發者體驗的團隊
  • 需要強大會話管理的應用程式

使用建議

  • 需要正確設置令牌刷新端點
  • 依賴外部認證服務的可用性
  • 可能需要為非標準驗證流程進行自訂
  • 不適用於無令牌的狀態無關 API
  • 僅限與 graphql 套件一起使用

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

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。