@@ -128,15 +128,16 @@ because different options exist for each platform.
128128For Flutter apps, there's two popular approaches:
129129
1301301 . Launch a browser using [ url_launcher] [ ] and listen for a redirect using
131- [ uni_links ] [ ] .
131+ [ app_links ] [ ] .
132132
133133 ``` dart
134134 if (await canLaunch(authorizationUrl.toString())) {
135135 await launch(authorizationUrl.toString()); }
136136
137137 // ------- 8< -------
138138
139- final linksStream = getLinksStream().listen((Uri uri) async {
139+ final appLinks = AppLinks();
140+ final linksStream = appLinks.uriLinkStream.listen((Uri uri) async {
140141 if (uri.toString().startsWith(redirectUrl)) {
141142 responseUrl = uri;
142143 }
@@ -161,6 +162,46 @@ For Flutter apps, there's two popular approaches:
161162 );
162163 ```
163164
165+
166+ 1. To handle redirect on Flutter Web you would need to add an html file to the web folder with some
167+ additional JS code to handle the redirect back to the app (in this example the code will be saved
168+ and passed through localStorage).
169+
170+ ```html
171+ <!DOCTYPE html>
172+ <html>
173+ <head>
174+ <meta charset="UTF-8" />
175+ <title>OAuth Callback</title>
176+ </head>
177+ <body>
178+ <script>
179+ const urlParams = new URLSearchParams(window.location.search);
180+ const code = urlParams.get('code');
181+
182+ // Store them in localStorage (or sessionStorage).
183+ if (code) {
184+ localStorage.setItem('oauth_code', code);
185+ }
186+
187+ // Redirect back to app
188+ window.location.replace('/#/');
189+ </script>
190+ </body>
191+ </html>
192+ ```
193+
194+ After redirect to the application the code can be extracted and processed using the dart.html
195+ package
196+
197+ ```dart
198+ import 'dart:html' as html;
199+ ...
200+ if(html.window.localStorage.containsKey('oauth_code')
201+ code = html.window.localStorage.remove('oauth_code')
202+ ...
203+ ```
204+
164205For Dart apps, the best approach depends on the available options for accessing
165206a browser. In general, you'll need to launch the authorization URL through the
166207client's browser and listen for the redirect URL.
@@ -255,6 +296,6 @@ File('~/.myapp/credentials.json').writeAsString(client.credentials.toJson());
255296[ resourceOwnerPasswordGrantDocs ] : https://oauth.net/2/grant-types/password/
256297[ resourceOwnerPasswordGrantMethod ] : https://pub.dev/documentation/oauth2/latest/oauth2/resourceOwnerPasswordGrant.html
257298[ resourceOwnerPasswordGrantSection ] : #resource-owner-password-grant
258- [ uni_links ] : https://pub.dev/packages/uni_links
299+ [ app_links ] : https://pub.dev/packages/app_links
259300[ url_launcher ] : https://pub.dev/packages/url_launcher
260301[ webview_flutter ] : https://pub.dev/packages/webview_flutter
0 commit comments