( authOptions: LoginOAuthWebOptions, )
| 32 | * @returns Resolves with the authorization code and options on success. |
| 33 | */ |
| 34 | export function performGitHubWebOAuth( |
| 35 | authOptions: LoginOAuthWebOptions, |
| 36 | ): Promise<AuthResponse> { |
| 37 | return new Promise((resolve, reject) => { |
| 38 | const { url } = getWebFlowAuthorizationUrl({ |
| 39 | clientType: 'oauth-app', |
| 40 | clientId: authOptions.clientId, |
| 41 | scopes: getRecommendedScopeNames(), |
| 42 | allowSignup: false, |
| 43 | request: request.defaults({ |
| 44 | baseUrl: `https://${authOptions.hostname}`, |
| 45 | }), |
| 46 | }); |
| 47 | |
| 48 | openExternalLink(url as Link); |
| 49 | |
| 50 | const handleCallback = (callbackUrl: string) => { |
| 51 | const url = new URL(callbackUrl); |
| 52 | |
| 53 | const type = url.hostname; |
| 54 | const code = url.searchParams.get('code'); |
| 55 | const error = url.searchParams.get('error'); |
| 56 | const errorDescription = url.searchParams.get('error_description'); |
| 57 | const errorUri = url.searchParams.get('error_uri'); |
| 58 | |
| 59 | if (code && type === 'oauth') { |
| 60 | resolve({ |
| 61 | authMethod: 'OAuth App', |
| 62 | authCode: code as AuthCode, |
| 63 | authOptions: authOptions, |
| 64 | }); |
| 65 | } else if (error) { |
| 66 | reject( |
| 67 | new Error( |
| 68 | `Oops! Something went wrong and we couldn't log you in using GitHub. Please try again. Reason: ${errorDescription} Docs: ${errorUri}`, |
| 69 | ), |
| 70 | ); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | window.gitify.onAuthCallback((callbackUrl: string) => { |
| 75 | rendererLogInfo( |
| 76 | 'renderer:auth-callback', |
| 77 | `received authentication callback URL ${callbackUrl}`, |
| 78 | ); |
| 79 | handleCallback(callbackUrl); |
| 80 | }); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Start a GitHub Device Flow authorization session. |
no test coverage detected