Skip to content

Commit

Permalink
Update to oauth2 package README for Flutter Web info
Browse files Browse the repository at this point in the history
  • Loading branch information
AbgarSim committed Jan 10, 2025
1 parent 54b8998 commit dffe5ac
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 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,45 @@ For Flutter apps, there's two popular approaches:
);
```
1. To handle redirect on Flutter Web you would need to add a 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);
}
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

0 comments on commit dffe5ac

Please sign in to comment.