Skip to content

Commit 30a0db1

Browse files
committed
github oauth reimplemented and working
1 parent d001f9e commit 30a0db1

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

app/electron/main.js

Lines changed: 51 additions & 8 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.herokuapp.com' })
334+
.get({ url: serverUrl })
330335
.then(cookie => {
331336
console.log(cookie);
332337
event.reply('give_cookie', cookie);
@@ -339,9 +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.herokuapp.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
});
353+
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/src/components/login/SignIn.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,19 @@ 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
8384
useEffect(() => {
84-
setInterval(() => {
85+
const githubCookie = setInterval(() => {
8586
window.api.setCookie();
8687
window.api.getCookie(cookie => {
88+
// if a cookie exists, set localstorage item with cookie data, clear interval, go back to '/' route to load app
8789
if (cookie[0]) {
8890
window.localStorage.setItem('ssid', cookie[0].value);
91+
clearInterval(githubCookie);
8992
props.history.push('/');
90-
clearInterval();
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);
9196
}
9297
});
9398
}, 2000);
@@ -201,10 +206,11 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
201206
helperText={invalidPassMsg}
202207
error={invalidPass}
203208
/>
209+
{/* **TODO** Make 'Remember Me' functional
204210
<FormControlLabel
205211
control={<Checkbox value="remember" color="primary" />}
206212
label="Remember me"
207-
/>
213+
/> */}
208214

209215
<Button
210216
fullWidth
@@ -223,6 +229,7 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
223229
className={classes.submit}
224230
onClick={() => {
225231
console.log('Inside onclick of github');
232+
// messages the main proces to open new window for github oauth
226233
window.api.github();
227234
}}
228235
>

0 commit comments

Comments
 (0)