visa
this is an oauth 2.0 package that makes it super easy to add third party authentication to flutter apps. it has support for fb, google, linkedin, discord, twitch, <strong/strong>, and spotify, auth. it also provides support for adding new oauth providers. you can read this medium article for a brief introduction.
demo
reference
getting started
install visa:
- open your pubspec.yaml file and add
visa:
under dependencies. - from the terminal: run flutter pub get.
- add the relevant import statements in the dart code.
// possible imports:
import 'package:visa/fb.dart';
import 'package:visa/google.dart';
import 'package:visa/github.dart';
import 'package:visa/linkedin.dart';
import 'package:visa/discord.dart';
import 'package:visa/twitch.dart';
import 'package:visa/spotify.dart';
import 'package:visa/auth-data.dart';
import 'package:visa/engine/oauth.dart';
import 'package:visa/engine/simple-auth.dart';
import 'package:visa/engine/visa.dart';
basic usage
step 1 – get a provider.
there are 7 default oauth providers at the moment:
facebookauth()
googleauth({ string personfields })
twitchauth()
discordauth()
githubauth()
linkedinauth()
spotifyauth()
create a new instance:
facebookauth fbauth = facebookauth();
simpleauth visa = fbauth.visa;
step 2 – authenticate.
as shown above, each provider contains a simpleauth instance called visa.
the simpleauth class has only one public function: authenticate(). it takes
in all the necessary oauth credentials and returns a webview that’s been set
up for authentication.
simpleauth.authenticate({ params })
webview authenticate({
bool newsession=false // if true, user has to reenter username and password even if they've logged in before
string clientsecret, // some providers (github for instance) require the oauth client secret (from developer portal).
@required string clientid, // oauth client id (from developer portal)
@required string redirecturi, // oauth redirect url (from developer portal)
@required string state, // oauth state string can be whatever you want.
@required string scope, // oauth scope (check provider's api docs for allowed scopes)
@required function ondone, // callback function which expects an authdata object.
});
here’s how you would use this function:
import 'package:visa/auth-data.dart';
import 'package:visa/fb.dart';
class authpage extends statelesswidget {
@override
widget build(buildcontext context) {
return scaffold(
/// simply provide all the necessary credentials
body: facebookauth().visa.authenticate(
clientid: '139732240983759',
redirecturi: 'https://www.e-oj.com/oauth',
scope: 'public_profile,email',
state: 'fbauth',
ondone: done
)
);
}
}
in the sample above, the named parameter ondone is a callback which recieves a single arument: an authdata object, which contains all the authentication info. we’ll look at authdata in the next section but here’s how you would pass an authdata object to the next screen via the ondone callback.
done(authdata authdata){
print(authdata);
/// you can pass the [authdata] object to a
/// post-authentication screen. it contaions
/// all the user and oauth data collected during
/// the authentication process. in this example,
/// our post-authentication screen is "complete-profile".
navigator.pushreplacementnamed(
context, '/complete-profile', arguments: authdata
);
}
step 3 – use authdata.
the authdata object contains all the information collected throughout the authentication process. it contains both user data and authentication metadata. as shown in the code sample above, this object is passed to the “authenticate” callback function. let’s have a look at it’s structure:
class authdata {
final string userid; // user's profile id
final string firstname; // user's first name
final string lastname; // user's last name
final string email; // user's email
final string profileimgurl; // user's profile image url
final map<string, dynamic> userjson; // full returned user json
final map<string, string> response; // full returned auth response.
final string clientid; // oauth client id
final string accesstoken; // oauth access token
}
it provides shortcuts to access common user properties (userid, name, email, profile image) as well as the accesstoken. the complete, returned user json can be accessed through the userjson property and you can access the full authentication response (the response in which we recieved an api access token) through the response property.
step 4 – rejoice!
you have successfully implemented third party auth in your app! you’re one step closer to launch. rejoice!
debugging
to get debug logs printed to the console, simply set the debug parameter on a provider to true
. like this:
var fbauth = facebookauth()
fbauth.debug = true;
Comments are closed.