Skip to content

Firefox can't close redirect window #83

@TyGrze

Description

@TyGrze

Describe the bug

By default firefox wont close the redirect auth.html page and will give this warning
"Scripts may not close windows that were not opened by script."

Also having it open a new page and redirect to a random html page outside of the flutter app feels bad
GoogleSignIn can open a new window for signing in and than pass back the info directly to flutter without needing a external html page

Im wondering if there is a way for this auth package to do the same as I would like to perform the auth on my backend server and google and just pass back a token to confirm the season to the client.

Example from my tests
Google Sign in

  static Future<String?> googleLogin() async {
    final googleUser = await GoogleSignIn(
      clientId: dotenv.env['GOOGLE_ClIENT_ID'],
      scopes: [
        'email'
      ]
    ).signIn();

    if (googleUser == null) return null;

    final googleAuth = await googleUser.authentication;
    return "Access Token: ${googleAuth.accessToken} User ${googleAuth.idToken} ID ${googleUser.id} Display Name: ${    googleUser.displayName}  AuthCode: ${googleUser.serverAuthCode} Image: ${googleUser.photoUrl}";
  }

Flutter_Web_Auth_2 code

static Future<String?> handleGoogleSignIn() async {
   String uri =
       kReleaseMode ? dotenv.env['PROD_API_URL']! : dotenv.env['DEV_API_URL']!;

   uri = '$uri/auth/google/authorize';

   final result = await FlutterWebAuth2.authenticate(
     url: uri,
     callbackUrlScheme: "my-custom-app",
   );

   final results = Uri.parse(result).queryParameters['token'];
   return results;
 }
 }

auth.html

<!DOCTYPE html>
<title>Authentication complete</title>
<p>Authentication will complete in around two seconds. If this does not happen automatically, please
close the window.
<script>
setTimeout(function(){
  if (window.opener) {
    window.opener.postMessage({
      'flutter-web-auth-2': window.location.href
    }, window.location.origin);
  } else {
    localStorage.setItem('flutter-web-auth-2', window.location.href);
  }
  window.close();
}, 2000);
</script>

To Reproduce

Steps to reproduce the behavior:

  1. Follow example in README
  2. Test in Firefox

Expected behavior

auth.html closes the window

Screenshots

If applicable, add screenshots to help explain your problem.

Device (please complete the following information!)

  • Device: computer
  • OS: windows
  • Browser: Firefox
  • flutter_web_auth_2 version: 3.0.3

Additional context

Add any other context about the problem here.

Checklist

  • I have read and followed the entire troubleshooting guide and it has not provided the solution I need.
  • I have provided all the information I can (incl. auth URL etc.)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions