1- import { BrowserWindow } from '@electron/remote' ;
21import { format } from 'date-fns' ;
32import semver from 'semver' ;
43
4+ import { ipcRenderer } from 'electron' ;
55import { APPLICATION } from '../../../shared/constants' ;
6+ import { namespacedEvent } from '../../../shared/events' ;
67import { logError , logWarn } from '../../../shared/logger' ;
78import type {
89 Account ,
@@ -22,80 +23,31 @@ import { Constants } from '../constants';
2223import { getPlatformFromHostname } from '../helpers' ;
2324import type { AuthMethod , AuthResponse , AuthTokenResponse } from './types' ;
2425
25- // TODO - Refactor our OAuth2 flow to use system browser and local app gitify://callback - see #485 #561 #654
26- export function authGitHub ( authOptions = Constants . DEFAULT_AUTH_OPTIONS ) {
27- const authUrl = new URL ( `https://${ authOptions . hostname } ` ) ;
28- authUrl . pathname = '/login/oauth/authorize' ;
29- authUrl . searchParams . append ( 'client_id' , authOptions . clientId ) ;
30- authUrl . searchParams . append ( 'scope' , Constants . AUTH_SCOPE . toString ( ) ) ;
31-
32- openExternalLink ( authUrl . toString ( ) as Link ) ;
33-
34- // return new Promise((resolve, reject) => {
35- // // Build the OAuth consent page URL
36- // const authWindow = new BrowserWindow({
37- // width: 548,
38- // height: 736,
39- // show: true,
40- // });
41-
42- // const authUrl = new URL(`https://${authOptions.hostname}`);
43- // authUrl.pathname = '/login/oauth/authorize';
44- // authUrl.searchParams.append('client_id', authOptions.clientId);
45- // authUrl.searchParams.append('scope', Constants.AUTH_SCOPE.toString());
46-
47- // const session = authWindow.webContents.session;
48- // session.clearStorageData();
49-
50- // authWindow.loadURL(authUrl.toString());
51-
52- // const handleCallback = (url: Link) => {
53- // const raw_code = /code=([^&]*)/.exec(url) || null;
54- // const authCode =
55- // raw_code && raw_code.length > 1 ? (raw_code[1] as AuthCode) : null;
56- // const error = /\?error=(.+)$/.exec(url);
57- // if (authCode || error) {
58- // // Close the browser if code found or error
59- // authWindow.destroy();
60- // }
61- // // If there is a code, proceed to get token from github
62- // if (authCode) {
63- // resolve({ authCode, authOptions });
64- // } else if (error) {
65- // reject(
66- // "Oops! Something went wrong and we couldn't " +
67- // 'log you in using GitHub. Please try again.',
68- // );
69- // }
70- // };
71-
72- // // If "Done" button is pressed, hide "Loading"
73- // authWindow.on('close', () => {
74- // authWindow.destroy();
75- // });
76-
77- // authWindow.webContents.on(
78- // 'did-fail-load',
79- // (_event, _errorCode, _errorDescription, validatedURL) => {
80- // if (validatedURL.includes(authOptions.hostname)) {
81- // authWindow.destroy();
82- // reject(
83- // `Invalid Hostname. Could not load https://${authOptions.hostname}/.`,
84- // );
85- // }
86- // },
87- // );
88-
89- // authWindow.webContents.on('will-redirect', (event, url) => {
90- // event.preventDefault();
91- // handleCallback(url as Link);
92- // });
93-
94- // authWindow.webContents.on('will-navigate', (event, url) => {
95- // event.preventDefault();
96- // handleCallback(url as Link);
97- // });
98- // });
26+ export function authGitHub (
27+ authOptions = Constants . DEFAULT_AUTH_OPTIONS ,
28+ ) : Promise < AuthResponse > {
29+ return new Promise ( ( resolve ) => {
30+ // // Build the OAuth consent page URL
31+ const authUrl = new URL ( `https://${ authOptions . hostname } ` ) ;
32+ authUrl . pathname = '/login/oauth/authorize' ;
33+ authUrl . searchParams . append ( 'client_id' , authOptions . clientId ) ;
34+ authUrl . searchParams . append ( 'scope' , Constants . AUTH_SCOPE . toString ( ) ) ;
35+
36+ openExternalLink ( authUrl . toString ( ) as Link ) ;
37+
38+ ipcRenderer . on (
39+ namespacedEvent ( 'auth-code' ) ,
40+ ( _ , authType : 'auth' | 'oauth' , authCode : AuthCode ) => {
41+ const type : AuthMethod =
42+ authType === 'auth' ? 'GitHub App' : 'OAuth App' ;
43+ handleCallback ( type , authCode ) ;
44+ } ,
45+ ) ;
46+
47+ const handleCallback = ( authType : AuthMethod , authCode : AuthCode ) => {
48+ resolve ( { authType, authCode, authOptions } ) ;
49+ } ;
50+ } ) ;
9951}
10052
10153export async function getUserData (
0 commit comments