ThexXTURBOXx / flutter_web_auth_2

Flutter plugin for authenticating a user with a web service
https://pub.dev/packages/flutter_web_auth_2
MIT License
51 stars 50 forks source link

WEB: invalid callbackUrlScheme error #32

Closed francescoberardi closed 1 year ago

francescoberardi commented 1 year ago

Describe the bug

After updating the library from version 2.0.0 to the latest version I'm not able to authenticate when running app on WEB. I always receive the error: Error: Invalid argument (callbackUrlScheme): must be a valid URL scheme:

To Reproduce

Pass any web url as callbackUrlScheme e.g. http://localhost:8080/auth.html or https://yourdomain/auth.html

Device (please complete the following information!)

Additional context

After some investigation I think that the issue is due to the _assertCallbackScheme method:

static void _assertCallbackScheme(String callbackUrlScheme) {
    **if (!_schemeRegExp.hasMatch(callbackUrlScheme) &&
        (kIsWeb || (!Platform.isWindows && !Platform.isLinux)))** {
      throw ArgumentError.value(
        callbackUrlScheme,
        'callbackUrlScheme',
        'must be a valid URL scheme',
      );
    }
  }

I think that the check should behave for web like for windows/linux:

**if (!_schemeRegExp.hasMatch(callbackUrlScheme) &&
        (!kIsWeb && !Platform.isWindows && !Platform.isLinux))** {
      throw ArgumentError.value(
        callbackUrlScheme,
        'callbackUrlScheme',
        'must be a valid URL scheme',
      );
    }

I can contribute and open a PR with the fix. Let me know what do you think

ThexXTURBOXx commented 1 year ago

Your callback URL scheme is actually invalid: https://github.com/ThexXTURBOXx/flutter_web_auth_2#troubleshooting-callbackurlscheme The callback URL scheme should only be the scheme you are using

francescoberardi commented 1 year ago

Thanks for the answer, but I didn't understand 😅 For mobile platform I pass the schema following the mentioned rules and it works, but for Web I followed the instructions: https://github.com/ThexXTURBOXx/flutter_web_auth_2#web and this is my code:

final callbackUrl = Uri(
        host: Uri.base.host,
        scheme: Uri.base.scheme,
        port: Uri.base.port,
        path: 'auth.html',
      ).toString()

 final result = await FlutterWebAuth2.authenticate(
      url: authUrl,
      callbackUrlScheme: callbackUrl,
    );

How can it work on the web if it doesn't accept a url? I'm probably missing something

ThexXTURBOXx commented 1 year ago

If you take a close look at the web implementation (https://github.com/ThexXTURBOXx/flutter_web_auth_2/blob/master/flutter_web_auth_2/lib/src/flutter_web_auth_2_web.dart), you will see that the callbackUrlScheme parameter is not used anywhere. So it doesn't matter what you put in as long as it is a valid RFC 3986 scheme as described in my link above. Your code is obviously passing a URL as callbackUrlScheme which is NOT a valid scheme

francescoberardi commented 1 year ago

Ohh, I didn't notice that it wasn't used. Thank you very much 🙂