Skip to content

Add support for basic auth - #3250

Merged
zcbenz merged 12 commits into
masterfrom
basic-auth
Oct 29, 2015
Merged

zcbenz merged 12 commits into
masterfrom
basic-auth

Conversation

@zcbenz

@zcbenz zcbenz commented Oct 28, 2015

Copy link
Copy Markdown
Contributor

Close #1362
Close #625

Event: 'login'

Returns:

  • event Event
  • webContents WebContents
  • request Object
    • method String
    • url URL
    • referrer URL
  • authInfo Object
    • isProxy Boolean
    • scheme String
    • host String
    • port Integer
    • realm String
  • callback Function

Emitted when webContents wants to do basic auth.

The default behavior is to cancel all authentications, to override this you
should prevent the default behavior with event.preventDefault() and call
callback(username, password) with the credentials.

app.on('login', function(event, webContents, request, authInfo, callback) {
  event.preventDefault();
  callback('username', 'secret');
})

@anaisbetts

Copy link
Copy Markdown
Contributor

Nice, will this fix proxy authentication too?

@johnhaley81

Copy link
Copy Markdown
Contributor

This is awesome! ❤️

@zcbenz

zcbenz commented Oct 29, 2015

Copy link
Copy Markdown
Contributor Author

Nice, will this fix proxy authentication too?

Yeah.

zcbenz added a commit that referenced this pull request Oct 29, 2015
Add support for basic auth
@zcbenz
zcbenz merged commit f13a02e into master Oct 29, 2015
@zcbenz
zcbenz deleted the basic-auth branch October 29, 2015 02:20
@anaisbetts

Copy link
Copy Markdown
Contributor

@zcbenz This is great work, thank you so much!

@tommoor

tommoor commented Oct 29, 2015

Copy link
Copy Markdown
Contributor

😍

@clarkey

clarkey commented Oct 29, 2015

Copy link
Copy Markdown

Thanks @zcbenz. I'm hoping this will work with Windows Authentication via IIS. If anyone could build this on Windows it would be much appreciated! I'm in a corporate environment and it's just a pain to get all the dependencies installed!

@lolclark

Copy link
Copy Markdown

Thanks for this update ! Being new on Electron I'm wondering a few things if anyone could help ?

  • does electron need updating locally to include this new "login" event ?

  • how should i implement it ? Getting "407 Proxy Authentication Required" error after using app.commandLine.appendSwitch('proxy-server','http://...')

  • the "login" event doesn't seem to be triggering using the following :

    mainWindow.webContents.on('login', fcuntion(event, webContents, request, authInfo, callback) {
    event.preventDefault();
    mainWindow.webContents.executeJavaScript("alert('Proxy OK!')");
    callback('username', 'password');
    });

Thanks for any help guys ;)

@ocombe

ocombe commented Oct 29, 2015

Copy link
Copy Markdown

Would this open a connection modal asking for credentials (like we have in browsers) or is it something that we need to implement somehow ?

@anaisbetts

Copy link
Copy Markdown
Contributor

@clarkey

clarkey commented Oct 30, 2015

Copy link
Copy Markdown

@paulcbetts Thanks buddy. Got this working on the 0.34.2 release without using app.allowNTLMCredentialsForAllDomains - laavely!

@rodrigok

rodrigok commented Apr 5, 2016

Copy link
Copy Markdown
Contributor

@zcbenz Is there any way to add this event in webviews?

@sheepray

sheepray commented Jun 2, 2016

Copy link
Copy Markdown

Hi all, I am new to electron. I am wondering when the event 'login' is triggered. And what does the 'login' even mean in here? Couple lines of example codes are appreciated. Thanks.

@patrykcieszkowski

patrykcieszkowski commented Oct 18, 2016 •

Copy link
Copy Markdown

this doesn't fix proxy issue. Event is getting fired, but after authorisation nothing happens if you run proxy from window other than main browser.

@cphoover

cphoover commented Nov 3, 2016

Copy link
Copy Markdown

How do we trigger a modal to prompt for input?

@wiliame

wiliame commented Nov 28, 2016 •

Copy link
Copy Markdown

Will be nice to clear auth credentials. For example, I'm using a proxy that accepts options via username:

proxy.com:8080
user: wiliam_country_US

So, if I want to change the peer node, I change my name:

proxy.com:8080
user: wiliam_country_GB

But, I'm not changing the proxy, so electron has valid credentials and is not going to trigger login event.

I'm searching the way to clean auth settings so electron asks them again.

(I have created a feature request: #8089)

@georgiosd

georgiosd commented Dec 1, 2016 •

Copy link
Copy Markdown

Hello,

This doesn't seem to work on the webContents.on('login') event - even though it gets called, I get a 407. Any way to fix?

Thanks

EDIT: it works if you call event.preventDefault() on the app.on('login') callback

@vadim-ch

Copy link
Copy Markdown

Hello,
How to make it work with autoUpdate???

@wiliame

wiliame commented Dec 13, 2016

Copy link
Copy Markdown

Clear auth data is fixed in electron 1.4.12:
#8089

@shrynx

shrynx commented Feb 3, 2017

Copy link
Copy Markdown

webContent returned in the function has nodeIntegration : false, How do i set it to true or can we do it by default ?

@ericuldall

Copy link
Copy Markdown

Is there any suggested workflow for incorrect credentials getting sent to the callback? Currently I have a workflow where if a user trys to click a link to one of my Basic Auth pages it pops a modal over the new window with a login prompt. Assuming I enter correct credentials it works as expected however there doesn't seem to be any way of knowing if the user passed invalid credentials at which point I end up with a blank window.

Has anyone else run into this problem and/or is there something obvious i'm missing?

@vsanse

vsanse commented Aug 25, 2019

Copy link
Copy Markdown

Getting Error while implementing basic authentication:
Code:

app.on('login', (event, webContents, request, authInfo, callback) => {
  event.preventDefault()
  console.log(callback)
  secWindow = new BrowserWindow({
    width:200,
    height:200,
    parent:mainWindow,
    modal:true,
    webPreferences: {
      nodeIntegration: true
      }
  });
  secWindow.loadFile('./renderer/secondarywindows/login.html');
  ipcMain.on("loginchannel",(e,args)=>{
    loginCred = args;
    if(loginCred.canceled){
      callback()
    }
    else{
      callback(loginCred.username, loginCred.password);
    }
    secWindow = null;
  })
})
Uncaught Exception:
Error: callback can only be called for once
    at EventEmitter.<anonymous>

Error occurs when I try to cancel first time and submit next time, so in short login event is fired twice.Please help what am I doing wrong.

@MatthieuLeMee

Copy link
Copy Markdown

callback()

You should do nothing when user cancels login. Like said by Electron, you cannot call callback() twice.
Calling it without args is useless anyway.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP Basic Authentication support Proxy support Basic Authentication?