mawie81 / electron-oauth2

A library to handle OAuth2 authentication for your Electron app.
MIT License
99 stars 56 forks source link

Cannot read property 'BrowserWindow' of undefined #21

Closed MoonTahoe closed 7 years ago

MoonTahoe commented 7 years ago

The only place I can require('electron-oauth2') is from src/system/index.js. When I require the npm in this file I do not get an error. I was able to make authorization work.

However, when it came time to move electron-oauth2 into antother file src/authorization/index.js, the simple act of requiring the npm throws an error

 TypeError: Cannot read property 'BrowserWindow' of undefined

      at Object.<anonymous> (node_modules/electron-oauth2/index.js:9:64)
      at Object.<anonymous> (src/authorization/index.js:2:22)
      at Object.<anonymous> (src/authorization/index.test.js:1:145)
          at <anonymous>

This line of code causes the above error in any file that I add it to except src/system/index.js which is the entry point for my main electron app.

var electronOauth2 = require('electron-oauth2')

I've tried making up new files and adding this this reqiure statement, it always fails. I've tried adding this require statement to the test, same error. But for some reason it will work from the main electron js file only. Adding this line to that file does not throw any errors during testing or during runtime. It doesn't matter if I use it or not, the mere act of requiring it throws the error.

MoonTahoe commented 7 years ago

I've really never seen anything like this. I literally commented out all of the code in both files except for var electronOauth2 = require('electron-oauth2'). In the main file it works, no error. In any other file it throws the BrowserWindow error. Super strange.

src/system/index.js --> No Browser Window Error

// var electron = require('electron')
// var BrowserWindow = electron.BrowserWindow
// var app = electron.app
// var path = require('path')
var electronOauth2 = require('electron-oauth2')
//
// var withDevFlag = () => process.argv[2] && process.argv[2] === '--dev'
//
// if (withDevFlag()) {
//   console.log('Development Server Running on http://localhost:3000')
// }
//
// const onClose = () => {
//   app.quit()
// }
//
// const onReady = () => {

...

src/authorization/index.js --> Browser Window Error

// var fetch = require('isomorphic-fetch')
//
var electronOauth2 = require('electron-oauth2')
//
// const config = {
//     clientId: <SECURE ID>,
//     clientSecret: <SECURE SECERET>,
//     authorizationUrl: 'http://github.com/login/oauth/authorize',
//     tokenUrl: 'https://github.com/login/oauth/access_token',
//     useBasicAuthorizationHeader: false,
//     redirectUri: 'http://localhost'
// }
//
// const windowParams = {
//   alwaysOnTop: true,
//   autoHideMenuBar: true,
//   webPreferences: {
//       nodeIntegration: false
//   }
// }

...
MoonTahoe commented 7 years ago

I was able to fix this issue with: yarn add electron@latest