fryette / webview_cookie_manager

MIT License
48 stars 52 forks source link

document.cookie does not match the cookieManager.getCookies output #3

Closed rodruiz closed 4 years ago

rodruiz commented 4 years ago

Hi, if you check document.cookie does not match the cookie provided.

onPageFinished: (_) async {
    final gotCookies = await cookieManager.getCookies(_url);
    for (var item in gotCookies) {
        print(item);
    }

    final String cookies = await _webViewController.evaluateJavascript('document.cookie');
    print('----- JS-COOKIES: $cookies -----');
},

Thanks.

rodruiz commented 4 years ago

ping @amag2511

fryette commented 4 years ago

@rodruiz which platform?

rodruiz commented 4 years ago

@fryette Android.

fryette commented 4 years ago

@rodruiz Seems your WebView plugin does not use the cookie manager to set cookies. This plugin using native CookieManager to add cookies and don't have anything special in the flutter. For your case seems you need to merge both results

rodruiz commented 4 years ago

@fryette I'm using exactly the same plugin and code in your example file. I assume your plugin works properly with WebView.

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: WebView(
          initialUrl: _url,
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (controller) async {
            await cookieManager.setCookies([
              Cookie(cookieName, cookieValue)
                ..domain = domain
                ..expires = DateTime.now().add(Duration(days: 10))
            ]);
          },
          onPageFinished: (_) async {
            final gotCookies = await cookieManager.getCookies(_url);
            for (var item in gotCookies) {
              print(item);
            }

           ///// CHECK WEBVIEW IS USING COOKIE MANAGER
            final String cookies = await _webViewController.evaluateJavascript('document.cookie');
            print('----- JS-COOKIES: $cookies -----');
          },
        ),
      ),
    );
fryette commented 4 years ago

@rodruiz Ok, You set cookie via cookie manager and cannot retrieve it by await _webViewController.evaluateJavascript('document.cookie');?

rodruiz commented 4 years ago

@fryette Correct. I can retrieve all cookies available in the web document using dart await _webViewController.evaluateJavascript('document.cookie');, but those cookies set by the cookie manager are not present. Seems like the webview is not using the cookie manager properly.

fryette commented 4 years ago

@rodruiz We had an issue on Android as well. We cannot set cookies to Webview and decided to wrote this library. Webview definitely using cookies from Android Cookie Manager but why we cannot retrieve it via JS I don't know

rodruiz commented 4 years ago

@fryette If you can not get those cookies via JS means that the cookies are not included in the request to the website.

rodruiz commented 4 years ago

@fryette could you reopen this ticket and investigate further? Another thing this plugin does not support using a domain like this (including a dot) when setting the cookie: .mydomain.com

rodruiz commented 4 years ago

Hi @fryette, just want to let you know that it's working now. Setting httpOnly = false did the trick. Maybe you can add that to the README.

await cookieManager.setCookies([
  Cookie(cookieName, cookieValue)
    ..domain = domain
    ..expires = DateTime.now().add(Duration(days: 10)),
    ..httpOnly = false, // NOW document.cookie will work
]);

Thanks!!!

fryette commented 4 years ago

WOW! Great! I will update readme!