Skip to content

Commit dffe5ac

Browse files
committed
Update to oauth2 package README for Flutter Web info
1 parent 54b8998 commit dffe5ac

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

pkgs/oauth2/README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,16 @@ because different options exist for each platform.
128128
For Flutter apps, there's two popular approaches:
129129

130130
1. 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,45 @@ 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 a 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+
window.location.replace('/#/');
188+
</script>
189+
</body>
190+
</html>
191+
```
192+
193+
After redirect to the application the code can be extracted and processed using the dart.html
194+
package
195+
196+
```dart
197+
import 'dart:html' as html;
198+
...
199+
if(html.window.localStorage.containsKey('oauth_code')
200+
code = html.window.localStorage.remove('oauth_code')
201+
...
202+
```
203+
164204
For Dart apps, the best approach depends on the available options for accessing
165205
a browser. In general, you'll need to launch the authorization URL through the
166206
client's browser and listen for the redirect URL.

0 commit comments

Comments
 (0)