You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importReactfrom"react";importApiServicefrom"@apiService";import{Oauth2Client}from"@types";import{useAppContext}from"@global/context";import{Button}from"@components/atoms/Button/Component";exportdefaultfunctionAuthorizeOauth2(): JSX.Element{letparams=Object.fromEntries(newURLSearchParams(window.location.search).entries());const{ setErrorMessage }=useAppContext();const[scopeDescriptions,setScopeDescriptions]=React.useState<{[key: string]: string;}>({});const[authorizedScopes,setAuthorizedScopes]=React.useState<string[]>([]);const[newScopes,setNewScopes]=React.useState<string[]>([]);const[client,setClient]=React.useState<Oauth2Client|null>(null);React.useEffect((): void=>{constclientId=params.client_id;constscopes=params.scope?.split(" ")??[];if(clientId){fetchData(clientId,scopes).catch((error)=>{setErrorMessage("Something went wrong");throwerror;});}},[params]);constfetchData=async(clientId: string,scopes: string[]): Promise<void>=>{const[{data: {data: {oauth2_client: client},},},{data: {data: {oauth2_authorizations: authorizations},},},{data: {data: {token_scopes: scopeDescriptions},},},]=awaitPromise.all([ApiService.oauth2Clients.show(clientId),ApiService.my.oauth2Authorizations.list({client_id: clientId}),ApiService.tokenScopes.list(),]);// If specific scopes are requested, we'll use those. Otherwise, default to all client scopes.constrequestedScopes=scopes.length>0 ? scopes : client.scopes;constauthorizedScopes=authorizations?.[0]?.scope??[];constnewScopes=requestedScopes.filter((s)=>!authorizedScopes.includes(s));if(newScopes.length===0){// If all requested scopes are already authorized, we can just re-authorize on behalf of the user.awaitauthorize(true);return;}setClient(client);setScopeDescriptions(scopeDescriptions);setAuthorizedScopes(authorizedScopes);setNewScopes(newScopes);};constauthorize=async(grantPermission: boolean): Promise<void>=>{try{const{data: {redirect_to: redirectTo},}=awaitApiService.oauth2.authorize({
...params,permission_granted: grantPermission,});window.location.replace(redirectTo);}catch(error: any){setErrorMessage(JSON.stringify(error.response.data));}};if(!client){return<div>Loading...</div>;}else{return(<div><div><div><p>
The following app wishes to use your account on your behalf:{" "}<strong>{client.name}</strong> - {client.description}</p><br/>{authorizedScopes.length>0&&(<><p>You have already granted {client.name} permission to:</p><ul>{authorizedScopes.map((scope: string,i: number)=>(<likey={i}>{scopeDescriptions[scope]??scope}</li>))}</ul></>)}<p>In addition, {client.name} wants to:</p><ul>{newScopes.map((scope: string,i: number)=>(<likey={i}>{scopeDescriptions[scope]??scope}</li>))}</ul></div></div><Buttonas="button"onClick={async()=>awaitauthorize(true)}>
Confirm
</Button><Buttonas="button"onClick={async()=>awaitauthorize(false)}>
Deny
</Button></div>);}}