Fresh Dio 是一個基於 Dio 的輕量級 HTTP 拦截器,可在 API 請求期間自動刷新權杖。它透明地處理驗證權杖,確保安全且無間斷地存取受保護的端點。透過攔截請求並順暢刷新過期權杖,可減少樣板程式碼並提升使用者體驗。非常適合需要強大驗證工作流程且設定簡單的應用程式。
適用情境
API 呼叫期間自動刷新令牌
處理過期的JWT令牌
保護經過驗證的API端點
減少手動令牌管理
提高登入會話持久性
簡化驗證狀態處理
主要特色
基於 Dio 建構
透明的令牌刷新
支援 JWT 和類似令牌
所需最小設定
自動攔截請求
處理 401 錯誤的重試邏輯
適合對象
使用 JWT 驗證的應用程式
頻繁進行 API 互動的專案
尋求減少認證樣板程式碼的開發者
具備會話管理的行動與網頁應用程式
優先考慮乾淨、可維護的驗證代碼的團隊
使用建議
需要與現有的 Dio 設定進行整合
僅限基於令牌的驗證流程
可能需要自定義邏輯來處理非標準令牌格式
依賴外部網路可用性進行刷新
不適用於無狀態或無令牌的 API
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
Fresh 🍋
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