flutter simple auth
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 don’t need to deal with it.
this is a port of clancey.simpleauth for dart and flutter
the network/api part including the generator was based off of chopper by hadrien lejard
current built in providers
- azure active directory
- amazon
- dropbox
- linked in
- microsoft live connect
- 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 = new request(httpmethod.get, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
var userinfo = await api.send<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 authentication can automatically present your login ui.
redirect
google requires the following redirect: com.googleusercontent.apps.your_client_id
simple authentication 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
<activity android:name="clancey.simpleauth.simpleauthflutter.simpleauthcallbackactivity" >
<intent-filter android:label="simple_auth">
<action android:name="android.intent.action.view" />
<category android:name="android.intent.category.default" />
<category android:name="android.intent.category.browsable" />
<data android:scheme="com.myapp.foo" />
</intent-filter>
</activity>
ios
on ios you need the following in your app delegate.
- (bool)application:(uiapplication *)application openurl:(nsurl *)url sourceapplication:(nsstring *)sourceapplication annotation:(id)annotation{
return [simpleauthflutterplugin checkurl:url];
}
for ios 11 and higher, you don’t need to do anything else. on older ios versions the following is required in the info.plist
<key>cfbundleurltypes</key>
<array>
<dict>
<key>cfbundleurlschemes</key>
<array>
<string>com.myapp.foo</string>
</array>
<key>cfbundleurlname</key>
<string>myappredirect</string>
</dict>
</array>
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.
generator
dart
pub run build_runner build
flutter
flutter packages pub run build_runner build
add the following to your pubspec.yaml
dev_dependencies:
simple_auth_generator:
build_runner: ^0.8.0
the generator is not required, however it will make things magical.
@googleapideclaration("googletestapi","client_id",clientsecret: "client_secret", scopes: ["testscope", "scope2"])
abstract class googletestdefinition {
@get(url: "https://www.googleapis.com/oauth2/v1/userinfo?alt=json")
future<response<googleuser>> getcurrentuserinfo();
}
will generate a new api for you that is easy to use!
var api = new googletestapi("google");
var user = await api.getcurrentuserinfo();
Comments are closed.