import qs from 'qs' ; import { constants , GitHubAppType , OAuthResponseData } from '@devhub/core' ; import { Browser } from '../browser' ; import { Platform } from '../platform' ; import { getUrlParamsIfMatches , listenForNextUrl } from './helpers' ; const redirectUri = constants . APP_DEEP_LINK_URLS . github_oauth ; export async function executeOAuth ( gitHubAppType , options = { } ) { const { appToken , scope } = options ; const scopeStr = ( scope || [ ] ) . join ( ' ' ) . trim ( ) ; const querystring = qs . stringify ( { app_token : appToken , github_app_type : gitHubAppType , is_electron : Platform . isElectron , platform : Platform . OS , redirect_uri : redirectUri , scope : scopeStr , } ) ; void Browser . openURL ( `${ constants . API_BASE_URL }/github/oauth?${ querystring }` , { native : { modalEnabled : true } , } ) ; const url = await listenForNextUrl ( ) ; const params = getUrlParamsIfMatches ( url , redirectUri ) ; if ( ! ( params && params . app_token && params . github_token ) ) { throw new Error ( 'Login failed: No access token received.' ) ; } return params ; }