Files
outline/plugins/figma/shared/FigmaUtils.ts
T
Tom Moor f50bb00b29 Refactor of OAuth account linking flows (#12246)
* Refactor of OAuth account linking flows

* PR feedback
2026-05-02 18:54:38 -04:00

60 lines
1.6 KiB
TypeScript

import queryString from "query-string";
import env from "@shared/env";
import { integrationSettingsPath } from "@shared/utils/routeHelpers";
export const FigmaOAuthNonceCookie = "figmaOAuthNonce";
export type OAuthState = {
teamId: string;
nonce: string;
};
export class FigmaUtils {
public static oauthScopes = ["current_user:read", "file_metadata:read"];
public static accountUrl = "https://api.figma.com/v1/me";
public static tokenUrl = "https://api.figma.com/v1/oauth/token";
public static refreshUrl = "https://api.figma.com/v1/oauth/refresh";
private static authBaseUrl = "https://www.figma.com/oauth";
private static settingsUrl = integrationSettingsPath("figma");
static parseState(state: string): OAuthState | undefined {
try {
return JSON.parse(state);
} catch {
return undefined;
}
}
static successUrl() {
return this.settingsUrl;
}
static errorUrl(error: string) {
return `${this.settingsUrl}?error=${error}`;
}
static callbackUrl(
{ baseUrl, params }: { baseUrl: string; params?: string } = {
baseUrl: env.URL,
params: undefined,
}
) {
return params
? `${baseUrl}/api/figma.callback?${params}`
: `${baseUrl}/api/figma.callback`;
}
static authUrl({ state }: { state: OAuthState }) {
const params = {
client_id: env.FIGMA_CLIENT_ID,
redirect_uri: this.callbackUrl(),
state: JSON.stringify(state),
scope: this.oauthScopes.join(","),
response_type: "code",
};
return `${this.authBaseUrl}?${queryString.stringify(params)}`;
}
}