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