0

I'm tasked with integrating with a service (Terra) that implements launching a web view to log into said service. Once the user logs in, it returns a url with params I need for further processing. However, my problem is that once I'm logged in successfully, all I can do is close the web browser manually, and the url that I need can't be used at all.

I'll attempt to pseudo code this:

  • Create a widget that will hit the service's endpoint to retrieve a sessionId and their url
  • Receive the url and sessionId
  • Using the url that we received, use url_launcher to open the web view
  • In this web view, log in with credentials
  • Once successfully logged in and the web view is redirected, retrieve the new URL on the flutter side to process the new params.

Only the last step is a bit unknown to me and the rest is working. I'm not sure if I could use a Stream to listen to any changes with browser's url, nor am I sure if that would work at all.

Is there any way to achieve what I need?

nyphur
  • 2,404
  • 2
  • 24
  • 48

1 Answers1

0

There's no need to use url_launcher, as you can simply redirect to the login page with, for example:

html.window.open(uri, '_self');

That opens the login page in the same window where your Flutter web app was running, and your user proceeds to log in.

At the end of that flow, the auth server should redirect you to a URL of your choosing, which you set to the URL of your Flutter app (which you can calculate at run time with):

  Uri.parse(html.window.location.href).removeFragment();

So, now you end up back running your main.dart again, but this time you have some extra parameters, which you grab immediately with:

Map<String, String>? initialQueryParameters;

void main() {
  initialQueryParameters = Uri.parse(html.window.location.href).queryParameters;
  runApp(SomeWidget());
}

Now at any point in your code you can inspect the initialQueryParameters to extract the response from the auth server which is normally simply added after the ? in the redirect URL back to you.

Richard Heap
  • 48,344
  • 9
  • 130
  • 112