Using BrowserWindow in the module


#1

I’m creating module for my electron application and i need to use BrowserWindow component in the module, my module look like so:
const util = require(‘util’)
const {BrowserWindow} = require(‘electron’)
const EventEmitter = require(‘events’).EventEmitter

class Auth extends EventEmitter {

  constructor(options) {
    super()
    this.options = options
    this.createWindow()
  }

  authenticate() {
    this.win.loadURL(authUrl)
    this.win.show()
  }

  handleCallback(url) {
    let raw_code = /token=([^&]*)/.exec(url) || null
    let code = (raw_code && raw_code.length > 1) ? raw_code[1] : null
    let error = /\?error=(.+)$/.exec(url)

    if (code || error) {
      // Close the browser if code found or error
      this.win.destroy()
      this.emit('token-received', code)
    }
  }

  createWindow() {
    this.win = new BrowserWindow({width: 800, height: 600, show: false, 'node-integration': false})
    this.win.webContents.on('did-get-redirect-request', (event, oldUrl, newUrl) => {
      AuthC.handleCallback(newUrl)
    })
    this.win.on('close', () => {
        this.win = null
    }, false)

  }
}

var AuthC = new Auth()

module.exports = AuthC

Error:

Error: Cannot create BrowserWindow before app is ready

It is located in the createWindow of my class, when i try to create new BrowserWindow.
My main main.js file content:

     const electron = require('electron')
     const {app, BrowserWindow, ipcMain} = electron
     const vk = require('./custom_modules/vk_auth.js')
     app.on('ready', () => {
        let win = new BrowserWindow({height: 300, width: 300})
             win.loadURL(``file://${__dirname}/views/login.html``)
        })
      }

Question:
How can i avoid that error and use BrowserWindow component in my module class? ( I understand why i got that error, but i have no idea how can i solve it in the MODULE)