From a322ac89a71b39dce2643626a51da6ca80657ab7 Mon Sep 17 00:00:00 2001 From: AbgarSim Date: Fri, 10 Jan 2025 15:57:29 +0200 Subject: [PATCH] Update to oauth2 package README for Flutter Web info --- pkgs/oauth2/CHANGELOG.md | 4 ++++ pkgs/oauth2/README.md | 47 +++++++++++++++++++++++++++++++++++++--- pkgs/oauth2/pubspec.yaml | 2 +- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/pkgs/oauth2/CHANGELOG.md b/pkgs/oauth2/CHANGELOG.md index ff39de3fb4..efc3a30431 100644 --- a/pkgs/oauth2/CHANGELOG.md +++ b/pkgs/oauth2/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.5 + +* Updated README + ## 2.0.4-wip * Require Dart 3.4 diff --git a/pkgs/oauth2/README.md b/pkgs/oauth2/README.md index 07a5976b6c..8ffcba9d5b 100644 --- a/pkgs/oauth2/README.md +++ b/pkgs/oauth2/README.md @@ -128,7 +128,7 @@ because different options exist for each platform. For Flutter apps, there's two popular approaches: 1. Launch a browser using [url_launcher][] and listen for a redirect using - [uni_links][]. + [app_links][]. ```dart if (await canLaunch(authorizationUrl.toString())) { @@ -136,7 +136,8 @@ For Flutter apps, there's two popular approaches: // ------- 8< ------- - final linksStream = getLinksStream().listen((Uri uri) async { + final appLinks = AppLinks(); + final linksStream = appLinks.uriLinkStream.listen((Uri uri) async { if (uri.toString().startsWith(redirectUrl)) { responseUrl = uri; } @@ -161,6 +162,46 @@ For Flutter apps, there's two popular approaches: ); ``` + +1. To handle redirect on Flutter Web you would need to add an html file to the web folder with some +additional JS code to handle the redirect back to the app (in this example the code will be saved +and passed through localStorage). + + ```html + + + + + OAuth Callback + + + + + + ``` + + After redirect to the application the code can be extracted and processed using the dart.html + package + + ```dart + import 'dart:html' as html; + ... + if(html.window.localStorage.containsKey('oauth_code') + code = html.window.localStorage.remove('oauth_code') + ... + ``` + For Dart apps, the best approach depends on the available options for accessing a browser. In general, you'll need to launch the authorization URL through the client's browser and listen for the redirect URL. @@ -255,6 +296,6 @@ File('~/.myapp/credentials.json').writeAsString(client.credentials.toJson()); [resourceOwnerPasswordGrantDocs]: https://oauth.net/2/grant-types/password/ [resourceOwnerPasswordGrantMethod]: https://pub.dev/documentation/oauth2/latest/oauth2/resourceOwnerPasswordGrant.html [resourceOwnerPasswordGrantSection]: #resource-owner-password-grant -[uni_links]: https://pub.dev/packages/uni_links +[app_links]: https://pub.dev/packages/app_links [url_launcher]: https://pub.dev/packages/url_launcher [webview_flutter]: https://pub.dev/packages/webview_flutter diff --git a/pkgs/oauth2/pubspec.yaml b/pkgs/oauth2/pubspec.yaml index ab705ba401..b60d537a07 100644 --- a/pkgs/oauth2/pubspec.yaml +++ b/pkgs/oauth2/pubspec.yaml @@ -1,5 +1,5 @@ name: oauth2 -version: 2.0.4-wip +version: 2.0.5 description: >- A client library for authenticating with a remote service via OAuth2 on behalf of a user, and making authorized HTTP requests with the user's