Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/oauth2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.5

* Updated README

## 2.0.4-wip
Comment on lines +1 to 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just stay on v.2.0.4 and remove the -wip so we can release

Suggested change
## 2.0.5
* Updated README
## 2.0.4-wip
## 2.0.4
* Updated README with example for Flutter Web.


* Require Dart 3.4
Expand Down
47 changes: 44 additions & 3 deletions pkgs/oauth2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ 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())) {
await launch(authorizationUrl.toString()); }

// ------- 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;
}
Expand All @@ -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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>OAuth Callback</title>
</head>
<body>
<script>
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');

// Store them in localStorage (or sessionStorage).
if (code) {
localStorage.setItem('oauth_code', code);
}

// Redirect back to app
window.location.replace('/#/');
</script>
</body>
</html>
```

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.
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion pkgs/oauth2/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: oauth2
version: 2.0.4-wip
version: 2.0.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version: 2.0.5
version: 2.0.4

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
Expand Down