-
Notifications
You must be signed in to change notification settings - Fork 51
How to listen for the callback in Flutter web? #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm fairly certain this package works on the Web, as it doesn't require dart:io or dart:mirrors. You also don't need uni_links or webview_flutter on the Web, because the API's already exist in the browser. If you call The secure way to do this involves you writing the actual OAuth-related code on your server, having the client only worry about handling an event from a child window. If you don't have a server, you can do everything client-side, but if you're not doing an |
@thosakwe . Using the html.window, you'd need separate routes to post and listen to the message, which is not happening because of flutter/flutter#33245 . You can't use # in the redirect uri. |
I don't think you would need separate routes, because the page that you open in a new window can just be a separate page that closes itself after firing an event in the parent, and won't need Flutter, or really much in the way of presentation, as it's an intermediate step. I haven't used Flutter Web, but I'm imagining you can listen for events while using it. |
@EightRice Did you ever figure out a solution for this? |
Yes, this is in high demand! We'd love to know a way out too. |
My solution was to forked the repo and changed AuthorizationCodeGrant class so it is possible to store it while the browser is doing the round-trip to the idp. |
Hey @RasmusSlothJensen can you please make your fork public? So anyone can use this package with flutter for web. Thanks! |
Hi @robinjanke |
Hi @RasmusSlothJensen , big thanks for sharing this! :) |
Hi @robinjanke I have a lot of abstraction in my app to support both mobile and web. onGenerateRoute: (RouteSettings settings) {
if (settings.name.startsWith('/callback')) {
return MaterialPageRoute(
builder: (context) => CallbackHandlerPage(
callback: Uri.base,
),
);
...
} In the final client = await grant.handleAuthorizationResponse(
callback.queryParameters,
); |
@RasmusSlothJensen Wow, thanks for sharing this with us. Love it! 👏 |
@RasmusSlothJensen Thanks for your fast answer, i already got an step further. |
@robinjanke I use https://pub.dev/packages/shared_preferences for web. final grant = AuthorizationCodeGrant(
clientID,
authorizationEndpoint,
tokenEndpoint,
);
final authorizationUrl =
grant.getAuthorizationUrl(redirectUri, scopes: scopes);
final String json = jsonEncode(
grant.toJson(),
);
await _sharedPreferences.setString('grant', json); Then when I need to handle the callback I get the grant from storage. final json = _sharedPreferences.getString('grant');
final grant = AuthorizationCodeGrant.fromJson(
jsonDecode(json),
); |
Any chance someone can post a full solution test project? |
@giiyms I am gonna write an medium article for that and post an link here when it is finished :) |
@giiyms I hope that this will help you: https://robinjanke1.medium.com/oauth2-with-flutter-web-e7a2b0dac7f3 |
@robinjanke Nice. This does the job, thanks! My only annoyance is that the API I am using automatically switches to https://localhost:8080/callback.html instead of http://localhost:8080/callback.html so debugging locally is a pain. Did you run into that? |
@giiyms You can use something like ngrok to handle this or you can get an free .tok domain and use this for locat development |
@robinjanke nice article. |
Nice!, would u mind if I ask how did u get it to work, with Nav 2.0 |
Hi @chessasuke I created a small sample to demonstrate how I got it working. The code is not nice at all, but I think it shows how it can be done. |
Many thanks!! It worked like a charm and cleared all my doubts |
Glad to see this got so much attention. Seems like it has been successfully addressed, so closing the issue. |
Any news on this? Seems like the fork is not available anymore. So any suggestions? This drives me crazy... 🤕 |
What I ended up doing was opening an external browser window to handle the roundtrip to the idp, which means the application didn't lose the state and you would avoid the reload of the app when handling the callback. I made a sample (not pretty) for you to checkout if interested https://github.com/RasmusSlothJensen/flutter_oauth_external_window |
@RasmusSlothJensen thanks for the fast reply, I will try it today and let u know if you saved my life 😁 |
You can check the solution i created for such case. |
@RasmusSlothJensen, it's not required to persist the whole This code can be used to generate a code yourself (got it from oauth2 package) /// Allowed characters for generating the _codeVerifier
static const String _charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
// Randomly generate a 128 character string to be used as the PKCE code
// verifier.
static String createCodeVerifier() => List.generate(
128,
(i) => _charset[Random.secure().nextInt(_charset.length)],
).join(); Then in onGenerateRoute you can create the grant with the same settings and call |
Neither uni_links nor webview_flutter support web. So how can you implement the listen method?
https://stackoverflow.com/questions/63946743/oauth2-in-flutter-web
The text was updated successfully, but these errors were encountered: