Socialify

Folder ..

Viewing auth.js
53 lines (47 loc) • 1.4 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { BrowserWindow, shell } from 'electron'

export default class AuthWindow extends BrowserWindow {
  constructor(url) {
    super({
      name: 'Connect Google Drive',
      width: 400,
      height: 520,
      webPreferences: {
        enableRemoteModule: false,
        nodeIntegration: false
      }
    })
    this.url = url
    this.URLWhitelist = [
      'https://accounts.google.com/o/oauth2/v2/auth',
      'https://accounts.google.com/signin/oauth',
      'https://getswifty.pro/google_oauth2/callback'
    ]
  }

  authenticate() {
    return new Promise(resolve => {
      this.loadURL(this.url, {
        extraHeaders: 'cookie: m_pixel_ratio=2'
      })
      this.webContents.on('will-navigate', (event, url) => {
        if (!this.isWhitelisted(url)) event.preventDefault()
      })

      this.webContents.on('new-window', async (event, navigationUrl) => {
        event.preventDefault()
        await shell.openExternal(navigationUrl)
      })

      this.webContents.on('did-navigate', (event, url) => {
        if (this.isAuthSuccess(url)) {
          return this.on('page-title-updated', (event, code) => {
            resolve(code)
          })
        }
      })
    })
  }

  isWhitelisted(url) {
    return this.URLWhitelist.find(item => url.match(item))
  }

  isAuthSuccess(url) {
    return url.match(`${CONFIG.apiHost}/google_oauth2/callback`)
  }
}