tp_flutter_web_auth version: 10.0.11

Published 2024-03-27

n/a

sdk | dart flutter
repository | svn
platform | n/a
status | deprecated
  • Readme
  • Changelog
  • Versions

Web Auth for Flutter

A Flutter plugin for authenticating a user with a web service, even if the web service is run by a third party. Most commonly used with OAuth2, but can be used with any web flow that can redirect to a custom scheme.

In the background, this plugin uses ASWebAuthenticationSession on iOS 12+ and macOS 10.15+, SFAuthenticationSession on iOS 11, Chrome Custom Tabs on Android and opens a new window on Web. You can build it with iOS 8+, but it is currently only supported by iOS 11 or higher.

iOS Android
iOS Android
macOS
macOS

Usage

To authenticate against your own custom site:

import 'package:flutter_web_auth/flutter_web_auth.dart';

// Present the dialog to the user
final result = await FlutterWebAuth.authenticate(url: "https://my-custom-app.com/connect", callbackUrlScheme: "my-custom-app");

// Extract token from resulting url
final token = Uri.parse(result).queryParameters['token']

To authenticate the user using Google's OAuth2:

import 'package:flutter_web_auth/flutter_web_auth.dart';

import 'dart:convert' show jsonDecode;
import 'package:http/http.dart' as http;

// App specific variables
final googleClientId = 'XXXXXXXXXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
final callbackUrlScheme = 'com.googleusercontent.apps.XXXXXXXXXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// Construct the url
final url = Uri.https('accounts.google.com', '/o/oauth2/v2/auth', {
  'response_type': 'code',
  'client_id': googleClientId,
  'redirect_uri': '$callbackUrlScheme:/',
  'scope': 'email',
});

// Present the dialog to the user
final result = await FlutterWebAuth.authenticate(url: url.toString(), callbackUrlScheme: callbackUrlScheme);

// Extract code from resulting url
final code = Uri.parse(result).queryParameters['code'];

// Use this code to get an access token
final response = await http.post('https://www.googleapis.com/oauth2/v4/token', body: {
  'client_id': googleClientId,
  'redirect_uri': '$callbackUrlScheme:/',
  'grant_type': 'authorization_code',
  'code': code,
});

// Get the access token from the response
final accessToken = jsonDecode(response.body)['access_token'] as String;

Setup

Setup works as for any Flutter plugin, expect the Android and Web caveats outlined below:

Android

In order to capture the callback url, the following activity needs to be added to your AndroidManifest.xml. Be sure to relpace YOUR_CALLBACK_URL_SCHEME_HERE with your actual callback url scheme.

<manifest>
  <application>

    <activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
      <intent-filter android:label="flutter_web_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="YOUR_CALLBACK_URL_SCHEME_HERE" />
      </intent-filter>
    </activity>

  </application>
</manifest>

Web

On the Web platform an endpoint needs to be created that captures the callback URL and sends it to the application using the JavaScript postMessage() method. In the ./web folder of the project, create an HTML file with the name e.g. auth.html with content:

<!DOCTYPE html>
<title>Authentication complete</title>
<p>Authentication is complete. If this does not happen automatically, please
close the window.
<script>
  window.opener.postMessage({
    'flutter-web-auth': window.location.href
  }, window.location.origin);
  window.close();
</script>

Redirection URL passed to the authentication service must be the same as the URL on which the application is running (schema, host, port if necessary) and the path must point to created HTML file, /auth.html in this case. The callbackUrlScheme parameter of the authenticate() method does not take into account, so it is possible to use a schema for native platforms in the code.

For the Sign in with Apple in web_message response mode, postMessage from https://appleid.apple.com is also captured, and the authorization object is returned as a URL fragment encoded as a query string (for compatibility with other providers).

Changelog

10.0.11 - 2024-03-27

Changed

  • Version bump from 10.0.10 to 10.0.11 (Deprecated this package).

10.0.10 - 2023-02-09

Changed

  • Version bump from 10.0.9 to 10.0.10 (Version confusion after compiler bug chaos).

10.0.9 - 2023-01-31

Changed

  • Version bump from 10.0.8 to 10.0.9.

10.0.8 - 2023-01-31

Changed

  • Version bump from 10.0.7 to 10.0.8.

10.0.7 - 2022-12-22

Changed

  • Version bump from 10.0.6 to 10.0.7.

10.0.6 - 2022-12-22

Changed

  • Version bump from 10.0.5 to 10.0.6 (Recompile due to installer optimizations).

10.0.5 - 2022-10-11

Changed

  • Version bump from 10.0.4 to 10.0.5 (Recompile due to wrong GetIt.instance syntax.).

10.0.4 - 2022-10-05

Changed

  • Version bump from 10.0.3 to 10.0.4 (fixed pdflib searchpath macos crash).

10.0.3 - 2022-10-01

Changed

  • Version bump from 10.0.2 to 10.0.3 (pkg_pdflib bundle macos dylib).

10.0.2 - 2022-09-29

Changed

  • Version bump from 10.0.1 to 10.0.2 (dependencies upgraded).

10.0.1 - 2022-08-15

Changed

  • Removed one deprecated call.

10.0.0 - 2022-08-15

0.1.0

  • πŸŽ‰ Add initial implementation

0.1.1

  • πŸ› Add swift_version to the Podspec
  • πŸ› Update Gradle and Kotlin versions
  • πŸ› Add missing link in readme

0.1.2

  • πŸŽ‰ Add support for iOS 13

0.1.3

  • πŸŽ‰ Update the kotlin plugin version to 1.3.61

0.2.0

  • πŸ’₯ Add macOS support (#20)

    Migration guide:

    This version drops support for Flutter 1.9 and older, please upgrade to Flutter 1.12 for continued support.

0.2.1

  • πŸ› Fix AndroidX build issues (#27)

0.2.2

  • πŸ› Fix propagation of "CANCELED" error on iOS (#31)

0.2.3

  • πŸ› Remove NoHistory flag (#33)
  • πŸ› Fix building on iOS 8, 9, and 10 (#29)
  • πŸ› Always terminate 'authenticate' callbacks on Android (#28)

0.2.4

  • πŸ› Fix building on iOS (#36)

0.3.0

  • πŸ’₯ Add null safety support (#60)

    Migration guide:

    This version drops support for Flutter 1.x, please upgrade to Flutter 2 for continued support.

0.3.1

  • πŸŽ‰ Add support for Android Plugin API v2 (#67)
  • πŸŽ‰ Add support for ephemeral sessions (#64)
  • 🌹 Avoid deprecated RaisedButton in example (#75)
  • 🌹 Cleanup metadata

0.3.2

  • πŸŽ‰ Add Web support (#77)

0.4.0

  • πŸ’₯ Upgrade to Android V2 embedding (#87)

    Migration guide:

    Make sure that you are running a recent version of Flutter before upgrading.

0.4.1

  • πŸŽ‰ Add support for Flutter "add to app" (#106)

Changed

  • @ruud copied from 0.4.1. Made local so tp_appwrite can use this specific verion.

10.0.11

2024-03-27 download

10.0.10

2023-02-09 download

10.0.9

2023-01-31 download

10.0.7

2022-12-22 download

10.0.6

2022-12-22 download

10.0.5

2022-10-11 download

10.0.4

2022-10-05 download

10.0.3

2022-10-01 download

10.0.2

2022-09-29 download

10.0.1

2022-08-15 download

10.0.0

2022-08-15 download

0.4.1

-- download