Skip to content

Commit 419fd04

Browse files
authored
Merge pull request #43 from andrewjcho84/github
GitHub Oauth re-implemented and working
2 parents 688198d + bb01346 commit 419fd04

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
@@ -58,8 +58,8 @@ async function createWindow() {
5858
// icon: path.join(__dirname, '../src/public/icons/png/256x256.png'),
5959
webPreferences: {
6060
zoomFactor: 0.7,
61-
// enable devtools
62-
devTools: true,
61+
// enable devtools when in development mode
62+
devTools: isDev,
6363
// crucial security feature - blocks rendering process from having access to node moduels
6464
nodeIntegration: false,
6565
// web workers will not have access to node
@@ -214,8 +214,8 @@ app.on('web-contents-created', (event, contents) => {
214214
const parsedUrl = new URL(navigationUrl);
215215
const validOrigins = [
216216
selfHost,
217-
'http://localhost:8081',
218-
'http://reactype.heroku.com',
217+
'http://localhost:5000',
218+
'https://reactype.herokuapp.com',
219219
'https://github.com/'
220220
];
221221
// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
@@ -235,9 +235,8 @@ app.on('web-contents-created', (event, contents) => {
235235
//console.log('parsedUrl.origin is', parsedUrl.origin);
236236
const validOrigins = [
237237
selfHost,
238-
'http://localhost:8081',
239238
'http://localhost:5000',
240-
'http://reactype.heroku.com',
239+
'https://reactype.herokuapp.com',
241240
'https://github.com',
242241
'app://rse/'
243242
];
@@ -323,10 +322,16 @@ ipcMain.on('choose_app_dir', event => {
323322
.catch(err => console.log('ERROR on "choose_app_dir" event: ', err));
324323
});
325324

325+
// define serverURL for cookie and auth purposes based on environment
326+
let serverUrl = 'https://reactype.herokuapp.com';
327+
if (isDev) {
328+
serverUrl = 'http://localhost:5000';
329+
}
330+
326331
// 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
327332
ipcMain.on('set_cookie', event => {
328333
session.defaultSession.cookies
329-
.get({ url: 'https://reactype.heroku.com' })
334+
.get({ url: serverUrl })
330335
.then(cookie => {
331336
console.log(cookie);
332337
event.reply('give_cookie', cookie);
@@ -339,12 +344,47 @@ ipcMain.on('set_cookie', event => {
339344
// again for production, document.cookie is not accessible so we need this listener on main to delete the cookie on logout
340345
ipcMain.on('delete_cookie', event => {
341346
session.defaultSession.cookies
342-
.remove('https://reactype.heroku.com', 'ssid')
347+
.remove(serverUrl, 'ssid')
343348
.then(removed => {
344349
console.log('Cookies deleted', removed);
345350
})
346351
.catch(err => console.log('Error deleting cookie:', err));
347352
});
348353

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

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)