Skip to content

Twitter authentifcation form POST REDIRECT #3471

Closed

Description

@xabufr

When trying to logging in twitter, Electron 0.35.0 doesn't follow FORM redirect.
The request done is POST /sessions, and response is a 302 REDIRECT.

Code to test

var app = require("app");
var BrowserWindow = require("browser-window");

app.on("ready", function() {
    var win = new BrowserWindow({
        webPreferences: {
            nodeIntegration: false,
            partition: "in-mem"
        }
    });
    win.loadURL("https://twitter.com/");
    win.webContents.openDevTools();
});

The request is marked as canceled in dev tools.
electron-bug

Activity

  1. added
    blocked/need-info ❌
    on Nov 18, 2015
  2. zcbenz commented on Nov 18, 2015

    @zcbenz
    Contributor

    Which platform are you using? I'm unable to reproduce on OS X 10.11.

  3. xabufr commented on Nov 18, 2015

    @xabufr
    Author

    I use an up-to-date Archlinux x86_64.
    I will try with another distribution....
    [EDIT 1]
    The same on Ubuntu wily (run with docker + X11 forwarding)
    [EDIT 2]
    Always with Docker and Ubuntu 12.04, I have the same behaviour...
    [EDIT 3]
    I have exactly the same behaviour when compiling Electron for Archlinux.

    Maybe this bug affect only linux ?
    Can someone else try on linux ?

  4. added and removed
    blocked/need-info ❌
    on Dec 10, 2015
  5. zcbenz commented on Dec 10, 2015

    @zcbenz
    Contributor

    I can reproduce the same thing on OS X.

  6. deanchen commented on Dec 11, 2015

    @deanchen

    Ran in to the same problem on OSX 10.11.

  7. zcbenz commented on Jan 29, 2016

    @zcbenz
    Contributor

    Reopened because of #4264.

  8. reopened this on Jan 29, 2016
  9. felicienfrancois commented on Feb 16, 2016

    @felicienfrancois

    I get the same issue on windows 10

  10. dogancelik commented on Feb 23, 2016

    @dogancelik

    If this is about 2FA authentication then yes I have the same issue too.
    Windows 8, Electron 0.36.8

  11. felicienfrancois commented on Feb 24, 2016

    @felicienfrancois

    @dogancelik it also happen with the simple authentication

  12. sokcuri commented on Jul 30, 2016

    @sokcuri

    I finally solved this issue. Here's my code.
    this code correctly running redirect from main frame

    app.on("ready", function() {
        var win = new BrowserWindow({
            webPreferences: {
                nodeIntegration: false,
                partition: "in-mem"
            }
        });
        win.loadURL("https://twitter.com/");
        win.webContents.openDevTools();
    
        win.webContents.on('did-get-redirect-request', function(e, oldURL, newURL, isMainFrame, httpResponseCode, requestMethod, refeerrer, header) {
            if (isMainFrame)
            {
                setTimeout(() => win.loadURL(newURL), 100);
                e.preventDefault();
            }
        });
    });
  13. quanglam2807 commented on Feb 22, 2017

    @quanglam2807
    Contributor

    @sokcuri It works!! Thank you so much!

  14. bionut commented on May 9, 2017

    @bionut

    From my testing the workaround above stopped working from electron >=1.6.3, but the issue is not resolved.

  15. alexstrat commented on May 29, 2017

    @alexstrat
    Contributor

    @zcbenz it sounds like you have some insights on what is the cause of this issue. Could you please share them in this thread to help us figure out a workaround?

    I confirm indeed the workaround posted above does not work anymore. The call to loadURL has no effect.

    Update: by browsing around, I stumbled upon this adaptation of the previous workaround:

    app.on("ready", function() {
        var win = new BrowserWindow({
            webPreferences: {
                nodeIntegration: false,
                partition: "in-mem",
                preload: "./preload.js"
            }
        });
        win.loadURL("https://twitter.com/");
        win.webContents.openDevTools();
    
        win.webContents.on('did-get-redirect-request', function(e, oldURL, newURL, isMainFrame, httpResponseCode, requestMethod, refeerrer, header) {
            if (isMainFrame)
            {
                setTimeout(() => win.webContents.send('redirect-url', newURL), 100);
                e.preventDefault();
            }
        });
    });
    
    // in preload.js
    const { ipcRenderer } = require('electron');
    
    ipcRenderer.on('redirect-url', function(event, url) {
        window.location.assign(url);
    });
  16. scoobster17 commented on Aug 17, 2017

    @scoobster17

    I seem to have found a fix-with-a-catch for the above when it comes to using webviews at least.

    Instead of using send() or loadURL(), which both seem not to work after the redirect fail, (even though isCrashed() insists the page is still working), looking through the list of methods I came across executeJavaScript().

    (If I use the same methods in the dev tools it works, before and after the re-direct failure).

    myWebview.addEventListener("did-get-redirect-request", function (e) {
        setTimeout(function() {
            myWebview.executeJavaScript(`window.location = '${e.newURL}';`);
        }, 10);
        e.preventDefault();
    });
    

    There is a catch, though.

    Other re-directs seem to happen e.g. when you log out so it appears sometimes as though the page loads twice (that's why I lowered the timeout delay to 10ms, although due to page load it is still noticeable to the user). Or loads, and then reloads; I assume this is when Electron allows the re-direct, but the event listener fires for all re-directs.

    EDIT:

    In addition to the above, I've been playing this morning and it seems that for some strange reason, adding an onbeforeunload event listener, that does not seem to run, allows the re-direct when I want it to (twitter login), but when I was seeing 2 page refreshes when I logged out, I no longer see this, and only get one. It's a hacky fix, but it works.

    myWebview.addEventListener("did-get-redirect-request", function (e) {
        setTimeout(function() {
            myWebview.executeJavaScript(
                [
                    'window.onbeforeunload = function(event){',
                        'console.log(event);',
                        'return \'Are you sure you want to leave?\';',
                    '};',
                    `window.location = '${e.newURL}';`
                ].join('');
            );
        }, 10);
        e.preventDefault();
    });
    

    Ideally, I would like to have used httpResponseCode, but that doesn't seem available on webviews.
    @zcbenz would be nice if possible to have a cleaner solution by being able to access this value on webviews. The other parameters mentioned in answers above seem to be available in the event object.

  17. added a commit that references this issue on Nov 12, 2017
  18. deepak1556 commented on Apr 5, 2018

    @deepak1556
    Member

    This issue should be fixed with #12535

  19. self-assigned this
    on Apr 5, 2018
  20. Syed-Umair commented on Mar 18, 2019

    @Syed-Umair
    Contributor

    Hi Team, This issue was fixed in which version of electron release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions