パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細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操作を伴うプロジェクト
  • 認証のボイラープレートを減らしたい開発者
  • セッション管理を備えたモバイルおよびWebアプリケーション
  • クリーンで保守しやすい認証コードを重視するチーム

注意事項

  • 既存の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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。