Skip to content

Commit 435c9ff

Browse files
authored
Merge pull request #1 from aw2934/reposition
Reposition
2 parents c39382e + 7e86e7f commit 435c9ff

33 files changed

+904
-578
lines changed

app/electron/main.js

Lines changed: 162 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
@description: main.js is what controls the lifecycle of the electron application from initialization to termination.
3+
@actions: codes for Github Oauth has been commented out because of lack of functionality.
4+
*/
5+
require('dotenv').config();
6+
const path = require('path');
17
const {
28
app,
39
protocol,
@@ -12,27 +18,24 @@ const {
1218
const { initSplashScreen, OfficeTemplate } = require('electron-splashscreen');
1319
const { resolve } = require('app-root-path');
1420

21+
// to install react dev tool extension
1522
const {
1623
default: installExtension,
1724
REACT_DEVELOPER_TOOLS
1825
} = require('electron-devtools-installer');
1926
const debug = require('electron-debug');
2027

28+
// import custom protocol in protocol.js
2129
const Protocol = require('./protocol');
2230
// menu from another file to modularize the code
2331
const MenuBuilder = require('./menu');
2432

25-
const path = require('path');
26-
// const fs = require('fs');
27-
require('dotenv').config();
28-
33+
// mode that the app is running in
2934
const isDev =
3035
process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
3136
const port = 8080;
3237
const selfHost = `http://localhost:${port}`;
3338

34-
// main.js is what controls the lifecycle of the electron application
35-
3639
// Keep a global reference of the window object, if you don't, the window will
3740
// be closed automatically when the JavaScript object is garbage collected.
3841
let win;
@@ -41,14 +44,13 @@ let menuBuilder;
4144
// function to create a new browser window
4245
// this function will be called when Electron has initialized itself
4346
async function createWindow() {
47+
// install react dev tools if we are in development mode
4448
if (isDev) {
4549
await installExtension([REACT_DEVELOPER_TOOLS])
4650
.then(name => console.log(`Added Extension: ${name}`))
4751
.catch(err => console.log('An error occurred: ', err));
4852
} else {
49-
// Needs to happen before creating/loading the browser window;
50-
// not necessarily instead of extensions, just using this code block
51-
// so I don't have to write another 'if' statement
53+
// this will happen before creating the browser window. it returns a Boolean whether the protocol of scheme 'app://' was successfully registered and a file (index-prod.html) was sent as the response
5254
protocol.registerBufferProtocol(Protocol.scheme, Protocol.requestHandler);
5355
}
5456

@@ -196,19 +198,10 @@ app.on('ready', createWindow);
196198

197199
// Quit when all windows are closed.
198200
app.on('window-all-closed', () => {
199-
// On macOS it is common for applications and their menu bar
200-
// to stay active until the user quits explicitly with Cmd + Q
201-
// if (process.platform !== 'darwin') {
202-
// app.quit();
203-
// } else {
204-
// ContextMenu.clearMainBindings(ipcMain);
205-
// }
206201
app.quit();
207202
});
208203

209204
app.on('activate', () => {
210-
// On macOS it's common to re-create a window in the app when the
211-
// dock icon is clicked and there are no other windows open.
212205
if (win === null) {
213206
createWindow();
214207
}
@@ -360,155 +353,154 @@ if (isDev) {
360353
serverUrl = 'http://localhost:5000';
361354
}
362355

363-
// for github oauth login in production, since cookies are not accessible through document.cookie on local filesystem, we need electron to grab the cookie that is set from oauth, this listens for an set cookie event from the renderer process then sends back the cookie
364-
ipcMain.on('set_cookie', event => {
365-
session.defaultSession.cookies
366-
.get({ url: serverUrl })
367-
.then(cookie => {
368-
// this if statement is necessary or the setInterval on main app will constantly run and will emit this event.reply, causing a memory leak
369-
// checking for a cookie inside array will only emit reply when a cookie exists
370-
if (cookie[0]) {
371-
//console.log(cookie);
372-
event.reply('give_cookie', cookie);
373-
}
374-
})
375-
.catch(error => {
376-
console.log('Error giving cookies in set_cookie:', error);
377-
});
378-
});
379-
380-
// again for production, document.cookie is not accessible so we need this listener on main to delete the cookie on logout
381-
ipcMain.on('delete_cookie', event => {
382-
session.defaultSession.cookies
383-
.remove(serverUrl, 'ssid')
384-
// .then(removed => {
385-
// console.log('Cookies deleted', removed);
386-
// })
387-
.catch(err => console.log('Error deleting cookie:', err));
388-
});
389-
390-
// opens new window for github oauth when button on sign in page is clicked
391-
ipcMain.on('github', event => {
392-
// your applications credentials
393-
const githubUrl = 'https://github.com/login/oauth/authorize?';
394-
const options = {
395-
client_id: process.env.GITHUB_ID,
396-
client_secret: process.env.GITHUB_SECRET,
397-
scopes: ['user:email', 'notifications']
398-
};
399-
// create new browser window object with size, title, security options
400-
const github = new BrowserWindow({
401-
width: 800,
402-
height: 600,
403-
title: 'Github Oauth',
404-
webPreferences: {
405-
nodeIntegration: false,
406-
nodeIntegrationInWorker: false,
407-
nodeIntegrationInSubFrames: false,
408-
contextIsolation: true,
409-
enableRemoteModule: false,
410-
zoomFactor: 1.0
411-
}
412-
});
413-
const authUrl =
414-
`${githubUrl}client_id=${process.env.GITHUB_ID}`;
415-
github.loadURL(authUrl);
416-
github.show();
417-
const handleCallback = url => {
418-
const raw_code = /code=([^&]\*)/.exec(url) || null;
419-
const code = raw_code && raw_code.length > 1 ? raw_code[1] : null;
420-
const error = /\?error=(.+)\$/.exec(url);
421-
422-
if (code || error) {
423-
// Close the browser if code found or error
424-
authWindow.destroy();
425-
}
426-
427-
// If there is a code, proceed to get token from github
428-
if (code) {
429-
self.requestGithubToken(options, code);
430-
} else if (error) {
431-
alert(
432-
"Oops! Something went wrong and we couldn't" +
433-
'log you in using Github. Please try again.'
434-
);
435-
}
436-
};
437-
438-
github.webContents.on('will-navigate', (e, url) => handleCallback(url));
439-
440-
github.webContents.on('did-finish-load', (e, url, a, b) => {
441-
github.webContents.selectAll();
442-
});
443-
444-
github.webContents.on('did-get-redirect-request', (e, oldUrl, newUrl) =>
445-
handleCallback(newUrl)
446-
);
447-
448-
// Reset the authWindow on close
449-
github.on('close', () => (authWindow = null), false);
450-
451-
// if final callback is reached and we get a redirect from server back to our app, close oauth window
452-
github.webContents.on('will-redirect', (e, callbackUrl) => {
453-
const matches = callbackUrl.match(/(?<=\?=).*/);
454-
const ssid = matches ? matches[0] : '';
455-
callbackUrl = callbackUrl.replace(/\?=.*/, '');
456-
let redirectUrl = 'app://rse/';
457-
if (isDev) {
458-
redirectUrl = 'http://localhost:8080/';
459-
}
460-
if (callbackUrl === redirectUrl) {
461-
dialog.showMessageBox({
462-
type: 'info',
463-
title: 'ReacType',
464-
icon: resolve('app/src/public/icons/png/256x256.png'),
465-
message: 'Github Oauth Successful!'
466-
});
467-
github.close();
468-
win.webContents
469-
.executeJavaScript(`window.localStorage.setItem('ssid', '${ssid}')`)
470-
.then(result => win.loadURL(selfHost))
471-
.catch(err => console.log(err));
472-
}
473-
});
474-
});
475-
476-
ipcMain.on('tutorial', event => {
477-
// create new browser window object with size, title, security options
478-
const tutorial = new BrowserWindow({
479-
width: 800,
480-
height: 600,
481-
minWidth: 661,
482-
title: 'Tutorial',
483-
webPreferences: {
484-
nodeIntegration: false,
485-
nodeIntegrationInWorker: false,
486-
nodeIntegrationInSubFrames: false,
487-
contextIsolation: true,
488-
enableRemoteModule: false,
489-
zoomFactor: 1.0
490-
}
491-
});
492-
// redirects to relevant server endpoint
493-
//github.loadURL(`${serverUrl}/github`);
494-
// show window
495-
tutorial.show();
496-
// if final callback is reached and we get a redirect from server back to our app, close oauth window
497-
// github.webContents.on('will-redirect', (e, callbackUrl) => {
498-
// let redirectUrl = 'app://rse/';
499-
// if (isDev) {
500-
// redirectUrl = 'http://localhost:8080/';
501-
// }
502-
// if (callbackUrl === redirectUrl) {
503-
// dialog.showMessageBox({
504-
// type: 'info',
505-
// title: 'ReacType',
506-
// icon: resolve('app/src/public/icons/png/256x256.png'),
507-
// message: 'Github Oauth Successful!'
508-
// });
509-
// github.close();
510-
// }
511-
// });
512-
});
513-
514-
//module.exports = dialog;
356+
// // for github oauth login in production, since cookies are not accessible through document.cookie on local filesystem, we need electron to grab the cookie that is set from oauth, this listens for an set cookie event from the renderer process then sends back the cookie
357+
// ipcMain.on('set_cookie', event => {
358+
// session.defaultSession.cookies
359+
// .get({ url: serverUrl })
360+
// .then(cookie => {
361+
// // this if statement is necessary or the setInterval on main app will constantly run and will emit this event.reply, causing a memory leak
362+
// // checking for a cookie inside array will only emit reply when a cookie exists
363+
// if (cookie[0]) {
364+
// //console.log(cookie);
365+
// event.reply('give_cookie', cookie);
366+
// }
367+
// })
368+
// .catch(error => {
369+
// console.log('Error giving cookies in set_cookie:', error);
370+
// });
371+
// });
372+
373+
// // again for production, document.cookie is not accessible so we need this listener on main to delete the cookie on logout
374+
// ipcMain.on('delete_cookie', event => {
375+
// session.defaultSession.cookies
376+
// .remove(serverUrl, 'ssid')
377+
// // .then(removed => {
378+
// // console.log('Cookies deleted', removed);
379+
// // })
380+
// .catch(err => console.log('Error deleting cookie:', err));
381+
// });
382+
383+
// // opens new window for github oauth when button on sign in page is clicked
384+
// ipcMain.on('github', event => {
385+
// // your applications credentials
386+
// const githubUrl = 'https://github.com/login/oauth/authorize?';
387+
// const options = {
388+
// client_id: process.env.GITHUB_ID,
389+
// client_secret: process.env.GITHUB_SECRET,
390+
// scopes: ['user:email', 'notifications']
391+
// };
392+
// // create new browser window object with size, title, security options
393+
// const github = new BrowserWindow({
394+
// width: 800,
395+
// height: 600,
396+
// title: 'Github Oauth',
397+
// webPreferences: {
398+
// nodeIntegration: false,
399+
// nodeIntegrationInWorker: false,
400+
// nodeIntegrationInSubFrames: false,
401+
// contextIsolation: true,
402+
// enableRemoteModule: false,
403+
// zoomFactor: 1.0
404+
// }
405+
// });
406+
// const authUrl = `${githubUrl}client_id=${process.env.GITHUB_ID}`;
407+
// github.loadURL(authUrl);
408+
// github.show();
409+
// const handleCallback = url => {
410+
// const raw_code = /code=([^&]\*)/.exec(url) || null;
411+
// const code = raw_code && raw_code.length > 1 ? raw_code[1] : null;
412+
// const error = /\?error=(.+)\$/.exec(url);
413+
414+
// if (code || error) {
415+
// // Close the browser if code found or error
416+
// authWindow.destroy();
417+
// }
418+
419+
// // If there is a code, proceed to get token from github
420+
// if (code) {
421+
// self.requestGithubToken(options, code);
422+
// } else if (error) {
423+
// alert(
424+
// "Oops! Something went wrong and we couldn't" +
425+
// 'log you in using Github. Please try again.'
426+
// );
427+
// }
428+
// };
429+
430+
// github.webContents.on('will-navigate', (e, url) => handleCallback(url));
431+
432+
// github.webContents.on('did-finish-load', (e, url, a, b) => {
433+
// github.webContents.selectAll();
434+
// });
435+
436+
// github.webContents.on('did-get-redirect-request', (e, oldUrl, newUrl) =>
437+
// handleCallback(newUrl)
438+
// );
439+
440+
// // Reset the authWindow on close
441+
// github.on('close', () => (authWindow = null), false);
442+
443+
// // if final callback is reached and we get a redirect from server back to our app, close oauth window
444+
// github.webContents.on('will-redirect', (e, callbackUrl) => {
445+
// const matches = callbackUrl.match(/(?<=\?=).*/);
446+
// const ssid = matches ? matches[0] : '';
447+
// callbackUrl = callbackUrl.replace(/\?=.*/, '');
448+
// let redirectUrl = 'app://rse/';
449+
// if (isDev) {
450+
// redirectUrl = 'http://localhost:8080/';
451+
// }
452+
// if (callbackUrl === redirectUrl) {
453+
// dialog.showMessageBox({
454+
// type: 'info',
455+
// title: 'ReacType',
456+
// icon: resolve('app/src/public/icons/png/256x256.png'),
457+
// message: 'Github Oauth Successful!'
458+
// });
459+
// github.close();
460+
// win.webContents
461+
// .executeJavaScript(`window.localStorage.setItem('ssid', '${ssid}')`)
462+
// .then(result => win.loadURL(selfHost))
463+
// .catch(err => console.log(err));
464+
// }
465+
// });
466+
// });
467+
468+
// ipcMain.on('tutorial', event => {
469+
// // create new browser window object with size, title, security options
470+
// const tutorial = new BrowserWindow({
471+
// width: 800,
472+
// height: 600,
473+
// minWidth: 661,
474+
// title: 'Tutorial',
475+
// webPreferences: {
476+
// nodeIntegration: false,
477+
// nodeIntegrationInWorker: false,
478+
// nodeIntegrationInSubFrames: false,
479+
// contextIsolation: true,
480+
// enableRemoteModule: false,
481+
// zoomFactor: 1.0
482+
// }
483+
// });
484+
// // redirects to relevant server endpoint
485+
// //github.loadURL(`${serverUrl}/github`);
486+
// // show window
487+
// tutorial.show();
488+
// // if final callback is reached and we get a redirect from server back to our app, close oauth window
489+
// // github.webContents.on('will-redirect', (e, callbackUrl) => {
490+
// // let redirectUrl = 'app://rse/';
491+
// // if (isDev) {
492+
// // redirectUrl = 'http://localhost:8080/';
493+
// // }
494+
// // if (callbackUrl === redirectUrl) {
495+
// // dialog.showMessageBox({
496+
// // type: 'info',
497+
// // title: 'ReacType',
498+
// // icon: resolve('app/src/public/icons/png/256x256.png'),
499+
// // message: 'Github Oauth Successful!'
500+
// // });
501+
// // github.close();
502+
// // }
503+
// // });
504+
// });
505+
506+
module.exports = dialog;

0 commit comments

Comments
 (0)