Simple Auth 是一個輕量級的 Flutter 套件,旨在簡化應用程式和 API 的使用者驗證。它提供了一個簡單且一致的介面,用於處理登入流程、權杖管理以及跨多個平台的會話持續性。此套件著重於易於整合,支援常見的驗證方式,如 OAuth2、JWT 和自訂登入工作流程。該套件抽象了複雜的實作細節,非常適合尋求無需樣板程式碼的最小但有效驗證解決方案的開發者。
適用情境
使用者以 OAuth2 登入
API金鑰管理
會話持久性
自定義驗證流程
跨平台驗證
快速原型設計
主要特色
最小設定
支援多種驗證類型
跨平台相容性
令牌存儲工具
乾淨的 API 設計
內建會話處理
適合對象
初學者Flutter開發者
需要基本驗證的應用程式
快速應用開發
多平台專案
注重簡潔的團隊
使用建議
有限的高級功能
不適合企業規模的驗證
無內建 UI 組件
依賴外部身分驗證提供方
對底層邏輯的控制較少
僅適用於簡單使用案例
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
Most apps need to make API calls. Every API needs authentication, yet no developer wants to deal with authentication. Simple Auth embeds authentication into the API so you dont need to deal with it.
And of course any standard OAuth2/Basic Auth server.
Usage
var api = new simpleAuth.GoogleApi(
"google", "client_id",clientSecret: "clientSecret",
scopes: [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile"
]);
var request = newRequest(HttpMethod.Get, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
var userInfo = await api.send<UserInfo, UserInfo>(request);
That's it! If the user is not logged in, they will automatically be prompted. If their credentials are cached from a previous session, the api call proceeds! Expired tokens even automatically refresh.
Flutter Setup
Call SimpleAuthFlutter.init(); in your Main.Dart. Now Simple Auth can automatically present your login UI
Redirect
Google requires the following redirect: com.googleusercontent.apps.YOUR_CLIENT_ID
Simple Auth by default uses SFSafari on iOS and Chrome Tabs on Android.
This means normal http redirects cannot work. You will need to register a custom scheme for your app as a redirect. For most providers, you can create whatever you want. i.e. com.myapp.foo:/redirct
Android Manifest
you would then add the following to your Android manifest
Note, if you want to avoid Apples mandatory user consent dialog
"foo" Wants to use "bar.com" to Sign In
This allows the app and website to share information about you.
add the lines above and set FooAuthenticator.useSSO = false; which will not use SFAuthenticationSession on iOS, and ASWebAuthenticationSession on macOS. This is the default behavior for the Keycloak provider.
Serialization
Json objects will automatically serialize if you conform to JsonSerializable
If you use the generator and you objects have the factory factory JsonSerializable.fromJson(Map<String, dynamic> json) your api calls will automatically Serialize/Deserialize
Or you can pass your own Converter to the api and handle conversion yourself.