Skip to content

Commit d0c9878

Browse files
committed
Merge branch 'master' into working
2 parents 41bb8d3 + 2b7ee7f commit d0c9878

File tree

4 files changed

+82
-25
lines changed

4 files changed

+82
-25
lines changed

app/electron/main.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ async function createWindow() {
6363
// icon: path.join(__dirname, '../src/public/icons/png/256x256.png'),
6464
webPreferences: {
6565
zoomFactor: 0.7,
66-
// enable devtools
67-
devTools: true,
66+
// enable devtools when in development mode
67+
devTools: isDev,
6868
// crucial security feature - blocks rendering process from having access to node moduels
6969
nodeIntegration: false,
7070
// web workers will not have access to node
@@ -220,8 +220,8 @@ app.on('web-contents-created', (event, contents) => {
220220
const parsedUrl = new URL(navigationUrl);
221221
const validOrigins = [
222222
selfHost,
223-
'http://localhost:8081',
224-
'http://reactype.heroku.com',
223+
'http://localhost:5000',
224+
'https://reactype.herokuapp.com',
225225
'https://github.com/'
226226
];
227227
// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
@@ -241,9 +241,8 @@ app.on('web-contents-created', (event, contents) => {
241241
//console.log('parsedUrl.origin is', parsedUrl.origin);
242242
const validOrigins = [
243243
selfHost,
244-
'http://localhost:8081',
245244
'http://localhost:5000',
246-
'http://reactype.heroku.com',
245+
'https://reactype.herokuapp.com',
247246
'https://github.com',
248247
'app://rse/'
249248
];
@@ -329,10 +328,16 @@ ipcMain.on('choose_app_dir', event => {
329328
.catch(err => console.log('ERROR on "choose_app_dir" event: ', err));
330329
});
331330

331+
// define serverURL for cookie and auth purposes based on environment
332+
let serverUrl = 'https://reactype.herokuapp.com';
333+
if (isDev) {
334+
serverUrl = 'http://localhost:5000';
335+
}
336+
332337
// 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
333338
ipcMain.on('set_cookie', event => {
334339
session.defaultSession.cookies
335-
.get({ url: 'https://reactype.heroku.com' })
340+
.get({ url: serverUrl })
336341
.then(cookie => {
337342
console.log(cookie);
338343
event.reply('give_cookie', cookie);
@@ -345,12 +350,47 @@ ipcMain.on('set_cookie', event => {
345350
// again for production, document.cookie is not accessible so we need this listener on main to delete the cookie on logout
346351
ipcMain.on('delete_cookie', event => {
347352
session.defaultSession.cookies
348-
.remove('https://reactype.heroku.com', 'ssid')
353+
.remove(serverUrl, 'ssid')
349354
.then(removed => {
350355
console.log('Cookies deleted', removed);
351356
})
352357
.catch(err => console.log('Error deleting cookie:', err));
353358
});
354359

355-
// bypass ssl certification validation error
356-
// app.commandLine.appendSwitch('ignore-certificate-errors', 'true');
360+
// opens new window for github oauth when button on signin page is clicked
361+
ipcMain.on('github', event => {
362+
// create new browserwindow object with size, title, security options
363+
const github = new BrowserWindow({
364+
width: 800,
365+
height: 600,
366+
title: 'Github Oauth',
367+
webPreferences: {
368+
nodeIntegration: false,
369+
nodeIntegrationInWorker: false,
370+
nodeIntegrationInSubFrames: false,
371+
contextIsolation: true,
372+
enableRemoteModule: false,
373+
zoomFactor: 1.0
374+
}
375+
});
376+
// redirects to relevant server endpoint
377+
github.loadURL(`${serverUrl}/github`);
378+
// show window
379+
github.show();
380+
// if final callback is reached and we get a redirect from server back to our app, close oauth window
381+
github.webContents.on('will-redirect', (e, callbackUrl) => {
382+
let redirectUrl = 'app://rse/';
383+
if (isDev) {
384+
redirectUrl = 'http://localhost:8080/';
385+
}
386+
if (callbackUrl === redirectUrl) {
387+
dialog.showMessageBox({
388+
type: 'info',
389+
title: 'ReacType',
390+
icon: resolve('app/src/public/icons/png/256x256.png'),
391+
message: 'Github Oauth Successful!'
392+
});
393+
github.close();
394+
}
395+
});
396+
});

app/electron/preload.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
setCookie,
1212
getCookie,
1313
delCookie,
14-
reload
14+
github
1515
} = require('./preloadFunctions/cookies');
1616

1717
// Expose protected methods that allow the renderer process to use select node methods
@@ -33,5 +33,6 @@ contextBridge.exposeInMainWorld('api', {
3333
writeFile,
3434
setCookie,
3535
getCookie,
36-
delCookie
36+
delCookie,
37+
github
3738
});

app/electron/preloadFunctions/cookies.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ const delCookie = () => {
1414
return ipcRenderer.send('delete_cookie');
1515
};
1616

17-
module.exports = { setCookie, getCookie, delCookie };
17+
const github = () => {
18+
return ipcRenderer.send('github');
19+
};
20+
21+
module.exports = { setCookie, getCookie, delCookie, github };

app/src/components/login/SignIn.tsx

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
8080
const [invalidUser, setInvalidUser] = useState(false);
8181
const [invalidPass, setInvalidPass] = useState(false);
8282

83+
// this useEffect will check for cookies and set an item in localstorage for github Oauth session validation
84+
useEffect(() => {
85+
const githubCookie = setInterval(() => {
86+
window.api.setCookie();
87+
window.api.getCookie(cookie => {
88+
// if a cookie exists, set localstorage item with cookie data, clear interval, go back to '/' route to load app
89+
if (cookie[0]) {
90+
window.localStorage.setItem('ssid', cookie[0].value);
91+
clearInterval(githubCookie);
92+
props.history.push('/');
93+
// if an item is already set in localstorage (guest option or normal login) clear interval needs to be run or else this will constantly run
94+
} else if (window.localStorage.getItem('ssid')) {
95+
clearInterval(githubCookie);
96+
}
97+
});
98+
}, 2000);
99+
}, []);
100+
83101
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
84102
let inputVal = e.target.value;
85103
switch (e.target.name) {
@@ -188,10 +206,11 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
188206
helperText={invalidPassMsg}
189207
error={invalidPass}
190208
/>
209+
{/* **TODO** Make 'Remember Me' functional
191210
<FormControlLabel
192211
control={<Checkbox value="remember" color="primary" />}
193212
label="Remember me"
194-
/>
213+
/> */}
195214

196215
<Button
197216
fullWidth
@@ -203,25 +222,18 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
203222
Sign In
204223
</Button>
205224
{/* Hiding github oauth button as it's still buggy and not fully working */}
206-
{/* <Button
225+
<Button
207226
fullWidth
208227
variant="contained"
209228
color="default"
210229
className={classes.submit}
211-
href="https://reactype.heroku.com/github"
212230
onClick={() => {
213-
console.log('Inside onclick of github');
214-
setTimeout(() => {
215-
window.api.setCookie();
216-
window.api.getCookie(cookie => {
217-
window.localStorage.setItem('ssid', cookie[0].value);
218-
props.history.push('/');
219-
});
220-
}, 2000);
231+
// messages the main proces to open new window for github oauth
232+
window.api.github();
221233
}}
222234
>
223235
<GitHubIcon />
224-
</Button> */}
236+
</Button>
225237

226238
<Button
227239
fullWidth

0 commit comments

Comments
 (0)