Orientação gerada a partir de informações públicas. Apenas para referência.
simple_auth_generator é um gerador de código Flutter/Dart que cria clientes HTTP a partir de interfaces anotadas, aproveitando o source_gen para geração de código em tempo de compilação. Inspirado no Retrofit e no Chopper, simplifica a integração com APIs ao gerar automaticamente classes de cliente com pontos finais tipados, tratamento de requisições e serialização. Os desenvolvedores definem APIs usando anotações, e o gerador produz código limpo e sustentável. Ele suporta serialização JSON e funciona em plataformas macOS, Windows e Linux.
Casos de uso
Gerando clientes HTTP com segurança de tipo
Automatizando o boilerplate de endpoint da API
Integrando serviços RESTful com código mínimo
Simplificando a serialização JSON
Criando clientes de API reutilizáveis em aplicativos Flutter
Principais recursos
Definição de API baseada em anotações
Geração de código em tempo de compilação
Requisições HTTP seguras por tipo
Serialização JSON embutida
Suporte multiplataforma (macOS, Windows, Linux)
Indicado para
Desenvolvedores Flutter construindo APIs REST
Equipes procurando reduzir o código boilerplate
Projetos que exigem clientes HTTP sustentáveis
Desenvolvedores familiarizados com padrões Retrofit/Chopper
Aplicativos que precisam de manipulação eficiente de JSON
Considerações
Requer conhecimento sobre source_gen e anotações
Limitado à geração de cliente HTTP
Nenhuma opção de configuração em tempo de execução
Depende do build_runner para geração de código
Não adequado para APIs não-REST sem personalização
README original
Snapshot do README original do repositório-fonte vinculado. Consulte o repositório para o conteúdo mais recente.
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.