搜索插件、代码仓与 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 交互的项目
  • 寻求减少认证样板代码的开发者
  • 带有会话管理的移动和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 项

源码仓库

使用此插件的项目

暂无相关数据。