Skip to content

Commit b7a4743

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

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

pkgs/oauth2/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.5
2+
3+
* Updated README
4+
15
## 2.0.4-wip
26

37
* Require Dart 3.4

pkgs/oauth2/README.md

+43-2
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,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 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+
// 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+
164205
For Dart apps, the best approach depends on the available options for accessing
165206
a browser. In general, you'll need to launch the authorization URL through the
166207
client's browser and listen for the redirect URL.

pkgs/oauth2/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: oauth2
2-
version: 2.0.4-wip
2+
version: 2.0.5
33
description: >-
44
A client library for authenticating with a remote service via OAuth2 on
55
behalf of a user, and making authorized HTTP requests with the user's

0 commit comments

Comments
 (0)