simple_auth_generator는 source_gen을 활용하여 컴파일 타임 코드 생성을 수행하는 Flutter/Dart 코드 생성기입니다. Retrofit과 Chopper에서 영감을 받아, 타입 안전한 엔드포인트, 요청 처리 및 직렬화를 자동으로 생성함으로써 API 통합을 간소화합니다. 개발자는 어노테이션을 사용해 API를 정의하고, 생성기는 깔끔하고 유지보수 가능한 코드를 생성합니다. JSON 직렬화를 지원하며, macOS, Windows, Linux 플랫폼에서 작동합니다.
사용 사례
타입 안전한 HTTP 클라이언트 생성
API 엔드포인트 보일러플레이트 자동화
최소한의 코드로 RESTful 서비스 통합
JSON 직렬화 간소화
Flutter 앱에서 재사용 가능한 API 클라이언트 만들기
주요 기능
주석 기반의 API 정의
컴파일 시 코드 생성
타입 안전한 HTTP 요청
내장된 JSON 직렬화
크로스 플랫폼 지원 (macOS, Windows, Linux)
적합한 대상
Flutter 개발자가 REST API 구축 중
보일러플레이트를 줄이려는 팀
유지보수 가능한 HTTP 클라이언트가 필요한 프로젝트
Retrofit/Chopper 패턴에 익숙한 개발자
효율적인 JSON 처리가 필요한 앱
고려 사항
source_gen 및 어노테이션에 대한 지식이 필요합니다
HTTP 클라이언트 생성에 제한됨
런타임 구성 옵션 없음
코드 생성을 위해 build_runner에 의존합니다
사용자 정의 없이 REST 이외의 API에는 적합하지 않습니다
원본 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.