license import RPCClient , { SignInResponse } from "./RPCClient" ; export interface Repo { [ "private" ] : boolean ; created_at : string ; description : string ; fork : false ; forks_count : number ; full_name : string ; has_pages : boolean ; name : string ; network_count : number ; project_count : number ; pushed_at : string ; stargazers_count : number ; subscribers_count : number ; updated_at : string ; url : string ; } export interface Auth { accessToken : string ; email : string ; name : string ; repo : string ; repoDetails ? : Repo ; username : string ; } export const CLIENT_ID = process . env . REACT_APP_GITHUB_CLIENT_ID ; const SCOPE = "public_repo" ; export function getOauthRedirect ( csrf ) { return ( "https://github.com/login/oauth/authorize" + `?client_id=${ CLIENT_ID }&scope=${ SCOPE }&state=${ csrf }` ) ; } /**
 * It's good practice when logging out to revoke the OAuth token, I guess.
 */ export async function revokeGitHubAuth ( rpc , token ) { try { await rpc . call ( "signOut" , { token , } ) ; } catch ( err ) { alert ( "Could not revoke GitHub authorization. " + "If you would like, you can manually do this from your GitHub settings." ) ; } finally { window . location . reload ( ) ; } } /**
 * Called by <App /> to continue the OAuth flow.
 */ export async function checkLogin ( rpc , code , state , csrf ) { if ( csrf !== state ) { console . warn ( "Invalid csrf." ) ; throw new Error ( "Something went wrong. Could not log you in." ) ; } const response = await rpc . call ( "signIn" , { oauth : code , state , } ) ; if ( ! response . result . accessToken || ! response . result . email || ! response . result . name || ! response . result . repo || ! response . result . username ) { throw new Error ( "Could not log you in." ) ; } return response . result ; } /**
 * Deserializes Auth. Used when parsing localStorage.
 */ export function parseAuth ( auth ) { if ( ! auth ) { return null ; } try { const parsedAuth = JSON . parse ( auth ) ; if ( parsedAuth && parsedAuth . accessToken && parsedAuth . email && parsedAuth . name && parsedAuth . repo && parsedAuth . username ) { return parsedAuth ; } return null ; } catch ( err ) { return null ; } } export function redirectToLogin ( csrf ) { window . location . href = getOauthRedirect ( csrf ) ; }