Dealing with OAuth can be tedious. So we've built a set of utilities to make that task way easier. There are two part to our utilities:
Using a built-in provider
We provide built-in providers that you can use out of the box, such as GitHub or Linear. You don't need to configure anything for them apart from the scope your extension requires.
Copy import { Detail , LaunchProps } from "@raycast/api" ;
import { withAccessToken , getAccessToken , OAuthService } from "@raycast/utils" ;
const github = OAuthService .github ({
scope : "notifications repo read:org read:user read:project" ,
});
function AuthorizedComponent (props : LaunchProps ) {
const { token } = getAccessToken ();
return < Detail markdown = { `Access token: ${ token } ` } />;
}
export default withAccessToken (github)(AuthorizedComponent);
Copy import { OAuth , Detail , LaunchProps } from "@raycast/api" ;
import { withAccessToken , getAccessToken , OAuthService } from "@raycast/utils" ;
const client = new OAuth .PKCEClient ({
redirectMethod : OAuth . RedirectMethod .Web ,
providerName : "Your Provider Name" ,
providerIcon : "provider_icon.png" ,
providerId : "yourProviderId" ,
description : "Connect your {PROVIDER_NAME} account" ,
});
const provider = new OAuthService ({
client ,
clientId : "YOUR_CLIENT_ID" ,
scope : "YOUR_SCOPES" ,
authorizeUrl : "YOUR_AUTHORIZE_URL" ,
tokenUrl : "YOUR_TOKEN_URL" ,
});
function AuthorizedComponent (props : LaunchProps ) {
const { token } = getAccessToken ();
return < Detail markdown = { `Access token: ${ token } ` } />;
}
export default withAccessToken (provider)(AuthorizedComponent);
Using onAuthorize
to initialize an SDK or similar
This example is useful in cases where you want to initialize a third-party client and share it throughout your codebase.
Copy import { OAuthService } from "@raycast/utils" ;
import { LinearClient , LinearGraphQLClient } from "@linear/sdk" ;
let linearClient : LinearClient | null = null ;
export const linear = OAuthService .linear ({
scope : "read write" ,
onAuthorize ({ token }) {
linearClient = new LinearClient ({ accessToken : token });
} ,
});
export function withLinearClient < T >(Component : React . ComponentType < T >) {
return withAccessToken < T >(linear)(Component);
}
export function getLinearClient () : { linearClient : LinearClient ; graphQLClient : LinearGraphQLClient } {
if ( ! linearClient) {
throw new Error ( "No linear client initialized" );
}
return { linearClient , graphQLClient : linearClient .client };
}