1- import { app , BrowserWindow } from 'electron'
1+ import { app , BrowserWindow , net , shell , ipcMain } from "electron" ;
2+ import { Deeplink } from "electron-deeplink" ;
3+ import isDev from "electron-is-dev" ;
4+
5+ import slackApiStuff from "../../secretStuff/slackApiStuff" ;
6+
7+ const clientId = slackApiStuff . clientId ;
8+ const clientSecret = slackApiStuff . clientSecret ;
9+ import dotenv from "dotenv" ;
10+ dotenv . config ( ) ;
211
312/**
413 * Set `__statics` path to static files in production;
514 * The reason we are setting it here is that the path needs to be evaluated at runtime
615 */
716if ( process . env . PROD ) {
8- global . __statics = require ( ' path' )
9- . join ( __dirname , ' statics' )
10- . replace ( / \\ / g, ' \\\\' )
17+ global . __statics = require ( " path" )
18+ . join ( __dirname , " statics" )
19+ . replace ( / \\ / g, " \\\\" ) ;
1120}
1221
13- let mainWindow
22+ let mainWindow ;
23+ let authCode ;
24+
25+ function logEverywhere ( toBeLogged ) {
26+ if ( isDev ) {
27+ console . log ( toBeLogged ) ;
28+ } else {
29+ console . log ( toBeLogged ) ;
30+ if ( mainWindow && mainWindow . webContents ) {
31+ mainWindow . webContents . executeJavaScript (
32+ 'console.log("' + toBeLogged + '" )'
33+ ) ;
34+ }
35+ }
36+ }
37+
38+ const protocol = isDev ? "overvuedev" : "overvue" ;
39+ const deeplink = new Deeplink ( {
40+ app,
41+ mainWindow,
42+ protocol,
43+ isDev,
44+ debugLogging : true ,
45+ electronPath : "../../node_modules/electron/dist/electron.exe"
46+ } ) ;
47+ // ipcMain.handle('slackAuth', slackAuth)
48+
49+ // function customDeepLink() {
50+ // let deeplinkingUrl;
51+
52+ // if (isDev && process.platform === 'win32') {
53+ // // Set the path of electron.exe and your app.
54+ // // These two additional parameters are only available on windows.
55+ // // Setting this is required to get this working in dev mode.
56+ // app.setAsDefaultProtocolClient('overvuedev', process.execPath, [
57+ // resolve(process.argv[1])
58+ // ]);
59+ // } else {
60+ // app.setAsDefaultProtocolClient('overvue');
61+ // }
62+
63+ // app.on('open-url', function (event, url) {
64+ // event.preventDefault();
65+ // deeplinkingUrl = url;
66+ // });
67+
68+ // // Force single application instance
69+ // const gotTheLock = app.requestSingleInstanceLock();
1470
15- function createWindow ( ) {
71+ // if (!gotTheLock) {
72+ // app.quit();
73+ // return;
74+ // } else {
75+ // app.on('second-instance', (e, argv) => {
76+ // if (process.platform !== 'darwin') {
77+ // // Find the arg that is our custom protocol url and store it
78+ // deeplinkingUrl = argv.find((arg) => arg.startsWith('overvuedev://test'));
79+ // }
80+
81+ // if (myWindow) {
82+ // if (myWindow.isMinimized()) myWindow.restore();
83+ // myWindow.focus();
84+ // }
85+ // })
86+ // }
87+ // }
88+
89+ function getSlackAuth ( ) {
90+ logEverywhere ( "inside getSlackAuth" ) ;
91+
92+ const authData = {
93+ client_id : clientId ,
94+ client_secret : clientSecret ,
95+ code : authCode ,
96+ redirect_uri : isDev ? "overvuedev://test" : "overvue://slack"
97+ } ;
98+ logEverywhere ( authData . code ) ;
99+ // https://slack.com/api/openid.connect.token?client_id=2696943977700.2696948669268&client_secret=6a6206cc93da2e49243ee9683f958438&code=2696943977700.2713919388452.23b787dec24adec68eeca105f6b7d6e517425de1033a1b6bc5ba3e116b933619&redirect_uri=overvue://slack
100+ const url =
101+ "https://slack.com/api/openid.connect.token?" +
102+ "client_id=" +
103+ authData . client_id +
104+ "&client_secret=" +
105+ authData . client_secret +
106+ "&code=" +
107+ authData . code +
108+ "&grant_type=authorization_key" +
109+ "&redirect_uri=" +
110+ authData . redirect_uri ;
111+ logEverywhere ( url ) ;
112+ const request = net . request ( {
113+ method : "POST" ,
114+ url : url ,
115+ headers : {
116+ "Content-Type" : "application/x-www-form-urlencoded"
117+ // 'Content-Length': authData.length
118+ }
119+ } ) ;
120+
121+ request . on ( "response" , response => {
122+ let body ;
123+ logEverywhere ( "RESPONSE RECEIVED SON" ) ;
124+ mainWindow . webContents . send ( "tokenReceived" , response ) ;
125+ // logEverywhere('STATUS: ', response.statusCode)
126+ // logEverywhere(`HEADERS: ${JSON.stringify(response.headers)}`)
127+ response . on ( "data" , data => {
128+ // logEverywhere(`response.on datas CHUNK: ${chunk}`)
129+ logEverywhere ( "chunked" ) ;
130+ logEverywhere ( data ) ;
131+ body = data ;
132+ } ) ;
133+ response . on ( "end" , ( ) => {
134+ logEverywhere ( "Response ended " ) ;
135+ mainWindow . webContents . send ( "tokenReceived" , body ) ;
136+ } ) ;
137+ } ) ;
138+ request . end ( ) ;
139+ }
140+
141+ function getSlackToken ( ) {
142+ return deeplink . on ( "received" , link => {
143+ logEverywhere ( `auth worked here link: ${ link } ` ) ;
144+ // authCode = link.split("=")[1];
145+ authCode = link . split ( "=" ) [ 1 ] . split ( "." ) [ 2 ] ;
146+ getSlackAuth ( ) ;
147+ } ) ;
148+ }
149+
150+ function createWindow ( ) {
16151 /**
17152 * Initial window options
18153 */
154+
19155 mainWindow = new BrowserWindow ( {
20156 width : 1000 ,
21157 height : 600 ,
@@ -24,25 +160,32 @@ function createWindow () {
24160 nodeIntegration : true ,
25161 webSecurity : false
26162 }
27- } )
163+ } ) ;
28164
29- mainWindow . loadURL ( process . env . APP_URL )
165+ logEverywhere ( `current protocol ${ deeplink . getProtocol ( ) } ` ) ;
166+ mainWindow . loadURL ( process . env . APP_URL ) ;
30167
31- mainWindow . on ( ' closed' , ( ) => {
32- mainWindow = null
33- } )
168+ mainWindow . on ( " closed" , ( ) => {
169+ mainWindow = null ;
170+ } ) ;
34171}
35172
36- app . on ( 'ready' , createWindow )
173+ app . on ( "ready" , ( ) => {
174+ createWindow ( ) ;
175+ getSlackToken ( ) ;
176+ logEverywhere ( `process.env.CLIENT_ID: ${ process . env . CLIENT_ID } ` ) ;
177+ } ) ;
37178
38- app . on ( ' window-all-closed' , ( ) => {
39- if ( process . platform !== ' darwin' ) {
40- app . quit ( )
179+ app . on ( " window-all-closed" , ( ) => {
180+ if ( process . platform !== " darwin" ) {
181+ app . quit ( ) ;
41182 }
42- } )
183+ } ) ;
43184
44- app . on ( ' activate' , ( ) => {
185+ app . on ( " activate" , ( ) => {
45186 if ( mainWindow === null ) {
46- createWindow ( )
187+ createWindow ( ) ;
188+ getSlackToken ( ) ;
189+ logEverywhere ( `process.env: ${ process . env } ` ) ;
47190 }
48- } )
191+ } ) ;
0 commit comments