Easy third party authentication (OAuth 2.0) for Flutter apps.

Overview

visa

Buy Me A Coffee

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, Github, and Spotify, auth. It also provides support for adding new OAuth providers. You can read this medium article for a brief introduction.

Demo

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;

Happy OAuthing!

Reference:

Comments
  • Webview not Launching?

    Webview not Launching?

    So I tried using this library but everytime I try to connect it to my button it doesnt launch. I put it inside a function with async and button click it should call that function.

    opened by Bambalow28 69
  • Returns null for the values in authData,Like email,clientId

    Returns null for the values in authData,Like email,clientId

    Returns null for the values in authData,Like email,clientId and other fields in authData class when done method is called This happens only with the user not having two-factor authentication to their discord account. For those who have the fields of AuthData are received and don't remain null

    opened by thepranays 2
  • Need to update dependencies

    Need to update dependencies

    First of all thank you for your amazing package!

    There are some dependencies that would be better if you could update them: visa depends on http ^0.12.2 and the one that i use is http >=0.13.0 so if you could update it please it would be great.

    Thank you!

    opened by VladRoscaDev 1
  • Migrate package to null-safety

    Migrate package to null-safety

    I have gone ahead and migrated the package and the test app to null-safety.

    When I tested the package, I tried signing in with GitHub, Google, and Discord. GitHub worked, however, when I tried signing with Google and Discord I received this error and I'm not quite sure how to fix it:

    E/flutter (22831): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type '_HashMap<String, String?>' is not a subtype of type 'Map<St 'oauthData'
    E/flutter (22831): #0      _TypeError._throwNew (dart:core-patch/errors_patch.dart:98:34)
    E/flutter (22831): #1      SimpleAuth.authenticate.<anonymous closure> (package:visa/engine/simple-auth.dart:60:48)
    E/flutter (22831): #2      SimpleAuth.authenticate.<anonymous closure> (package:visa/engine/simple-auth.dart:57:17)
    E/flutter (22831): #3      OAuth._getNavigationDelegate.<anonymous closure> (package:visa/engine/oauth.dart:95:17)
    E/flutter (22831): #4      _PlatformCallbacksHandler.onNavigationRequest (package:webview_flutter/webview_flutter.dart:571:42)
    E/flutter (22831): #5      MethodChannelWebViewPlatform._onMethodCall (package:webview_flutter/src/webview_method_channel.dart:36:48)
    E/flutter (22831): #6      MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:435:55)
    E/flutter (22831): #7      MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:382:34)
    E/flutter (22831): #8      _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:284:33)
    E/flutter (22831): #9      _invoke3.<anonymous closure> (dart:ui/hooks.dart:221:15)
    E/flutter (22831): #10     _rootRun (dart:async/zone.dart:1354:13)
    E/flutter (22831): #11     _CustomZone.run (dart:async/zone.dart:1258:19)
    E/flutter (22831): #12     _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
    E/flutter (22831): #13     _invoke3 (dart:ui/hooks.dart:220:10)
    E/flutter (22831): #14     PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:457:7)
    E/flutter (22831): #15     _dispatchPlatformMessage (dart:ui/hooks.dart:90:31)
    E/flutter (22831):
    

    I was not able to test Twitch or Facebook OAuth.

    opened by Tetracyl 1
  • Invalid scope when login with Twitch

    Invalid scope when login with Twitch

    ` Scaffold( appBar: AppBar(), body: TwitchAuth().visa.authenticate(clientID: LLSocialMedia.ClientID_Twitch, redirectUri: LLSocialMedia.redirectURI_Twitch, state: "twitchAuth", scope: "email", onDone: (AuthData authData){

           }),
     );`
    
    opened by panchalAlpit 0
  • Discord Login not working

    Discord Login not working

    Hello, first of all nice package, it is really easy to understand & to implement in the app. I just wanted to inform you that the Discord Login isn't working. I tried your test_app to see if I'm doing something wrong, but in the test app only the twitch login was working for me (I changed id/redirect_url and so on for discord login). All it did was redirecting me to the redirect url in the WebView. Am I doing something wrong? Or is the package broken at the moment?

    opened by KitsuneYokai 1
  • Would null safety be on its way?

    Would null safety be on its way?

    Props on the simplicity and easiness of the package but with the migration of flutter going to Null safety it stops me and more people to use it, is there a null safety for or any null safety version ready so far?

    opened by tiagocdr 0
  • Possible improvement: A way to select account before performing login

    Possible improvement: A way to select account before performing login

    This isn't a bug but would be nice to have.

    Some users would like to get the screen that shows the current accounts on the device. Example you have two google accounts on the device and you want to choose one in order to login.

    It would be nice to be able to add this functionality.

    Great job with the package it is a real lifesaver!

    enhancement 
    opened by VladRoscaDev 3
  • Support more platforms and more authentication options

    Support more platforms and more authentication options

    First of all, thank you for your awesome plugin :)

    More platforms

    Currently, the plugin only supports Android and iOS. It'd be great if other platforms could be supported:

    • [x] Android
    • [x] iOS
    • [ ] Web
    • [ ] macOS
    • [ ] Windows
    • [ ] Linux

    More authentication methods

    good first issue Feature Request 
    opened by bdlukaa 10
Releases(1.0.3)
Owner
Emmanuel Olaojo
RIT Alumni. Codist.
Emmanuel Olaojo
A Flutter OAuth package for performing user authentication for your apps.

Flutter OAuth A Flutter OAuth package for performing user authentication for your apps. I've tested this with a small collection of APIs (Buffer, Stra

Joe Birch 173 Dec 9, 2022
WebView OAuth flows for desktop flutter apps

desktop_webview_auth A new flutter plugin project. Getting Started This project is a starting point for a Flutter plug-in package, a specialized packa

Invertase 22 Dec 17, 2022
Email and Password Authentication In Flutter & Firebase in Flutter 2.2

email_password_flutter_firebase Email and Password Authentication In Flutter & Firebase in Flutter 2.2 Overview This email and password authentication

null 0 Mar 24, 2022
This project is an example of Firebase authentication in a flutter

This project is an example of Firebase authentication in a flutter. This project shows how to implement a full authentication flow in Flutter, using sign Up with email and password, sign in with email and password, and reset password.

Nittin 4 Mar 11, 2022
Authentication pages I made with Flutter and Firebase

Auth-with-Flutter-and-Firebase Authentication pages I made with Flutter and Firebase Overview This email and password authentication is implemented wi

Said Mirzayev 3 Jul 24, 2022
An example of JWT authentication with flutter.

flutter-jwt-auth-template An example of JWT authentication with flutter. Getting Started Clone this repository, and inside its folder, run: flutter pu

Enzo Di Tizio 23 Jan 4, 2023
By using Flutter Local Auth users can authenticate with Fingerprint & Touch ID in Flutter.

Flutter Tutorial - Fingerprint & Touch ID - Local Auth By using Flutter Local Auth users can authenticate with Fingerprint & Touch ID in Flutter. Soci

Johannes Milke 37 Dec 15, 2022
Flutter Password Validator package helps you to validate user-entered passwords in your flutter app.

Flutter Password Validator Flutter Password Validator package helps you to validate sign-in user-entered passwords with your rules.

Aref Mozafari 26 Dec 14, 2022
A Flutter plugin for allowing users to authenticate with native Android & iOS Facebook login SDKs.

flutter_facebook_login A Flutter plugin for using the native Facebook Login SDKs on Android and iOS. AndroidX support if you want to avoid AndroidX, u

Iiro Krankka 404 Nov 26, 2022
Flutter Plugin for Sign In with Apple

Apple Sign In - Flutter Plugin Access Sign In with Apple from Flutter. Platform support This plugin currently only supports iOS. There's a JavaScript

Tom Gilder 156 Dec 28, 2022
The Simplest way to Authenticate in Flutter

Most apps need to make API calls. Every API needs authentication, yet no developer wants to deal with authentication. Simple Auth embeds authenticatio

James Clancey 340 Dec 25, 2022
A Flutter wrapper for AppAuth iOS and Android SDKs

flutter_appauth A Flutter plugin that provides a wrapper for native AppAuth SDKs (https://appauth.io) used authenticating and authorizing users. The r

Michael Bui 230 Dec 21, 2022
Flutter implementation of the Quickstart Supabase User Management app.

Supabase Flutter User Management This repo is a quick sample of how you can get started building apps using Flutter and Supabase. You can find a step

Supabase Community 56 Nov 14, 2022
A Flutter package that implements Google Sign In in pure Dart.

A Flutter package that implements Google Sign In in pure Dart. This package is compatible with google_sign_in plugin and works on all platforms Flutter supports but it's intended to be mainly used on Desktop.

Razvan Lung 10 Oct 26, 2022
In this Application Built Using Flutter🎯 along with FireBase 🔥for Managing the data

In this Application Built Using Flutter?? along with FireBase ??for Managing the data, I have Curated a simple?? Login/Sign Up Space along with Authentication!

Aashvi Kothari 4 Oct 9, 2022
An OAuth authentication system built in Flutter using Google OAuth.

An OAuth authentication system built in Flutter using Google OAuth.

Kaushal 0 Sep 20, 2021
Tour guide App UI in Flutter Consist of Three Pages. First Screen is Splash Screen , Second is welcome screen routing to third Screen and Third screen consist of details , navigation, places, and responsive UI.

Tour Guide // Tourism App Ui in Flutter. Tour Guid App Ui in Flutter. Visit Website Features State Management Navigation Bar Responsive Design Hybrid

Habib ullah 2 Nov 14, 2022
We created Flappy Bird, a straightforward game based on flutter animation, from scratch using only Dart & Flutter and no third-party games or animation components.

Flappy-Bird Description A ridiculous game created by Flutter, all you have to do is touch the screen to make the bird leap as long as you avoid the ba

hab 15 Dec 25, 2022
Multi-language support in Flutter without using any third-party library

flutter_localization_app A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get

Gulshan Yadav 1 Oct 30, 2021
A simple crypto tracker Flutter app with cero third party package

Crypto Tracker A simple crypto tracker Flutter app with cero third party package, that incorparates the Nomics api. This application collects the hist

null 28 Oct 10, 2022