搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

暂无相关数据。