Skip to content

Instantly share code, notes, and snippets.

@alex-streza
Last active December 28, 2024 10:47
Show Gist options
  • Save alex-streza/16485bbdb4dd33f7ea4b534b296f5f03 to your computer and use it in GitHub Desktop.
Save alex-streza/16485bbdb4dd33f7ea4b534b296f5f03 to your computer and use it in GitHub Desktop.
Figma OAuth Provider implementation for Auth.js (previously next-auth)
import { Provider } from "next-auth/providers";
export const FigmaProvider: Provider = {
id: "figma",
name: "Figma",
type: "oauth",
authorization: {
url: "https://www.figma.com/oauth",
params: {
scope: "file_read",
response_type: "code",
},
},
token: {
url: "https://api.figma.com/v1/oauth/token",
async request(context) {
const provider = context.provider;
// Create Base64 encoded credentials
const credentials = Buffer.from(
`${provider.clientId}:${provider.clientSecret}`
).toString("base64");
// Prepare form data
const formData = new URLSearchParams({
redirect_uri: provider.callbackUrl,
code: context.params.code,
grant_type: "authorization_code",
});
const res = await fetch("https://api.figma.com/v1/oauth/token", {
method: "POST",
headers: {
"Authorization": `Basic ${credentials}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: formData.toString(),
});
const json = await res.json();
if (!res.ok) {
throw new Error(`Failed to get token: ${JSON.stringify(json)}`);
}
return { tokens: json };
},
},
userinfo: "https://api.figma.com/v1/me",
profile(profile) {
return {
id: profile.id,
name: `${profile.handle}`,
email: profile.email,
image: profile.img_url,
};
},
clientId: process.env.FIGMA_ID,
clientSecret: process.env.FIGMA_SECRET,
};
@Akshithpottigari
Copy link

This isn't working anymore. Can you update this?

@nikitashekhov
Copy link

nikitashekhov commented Dec 14, 2024

Update this accordingly: https://www.figma.com/developers/api#oauth_migration_guide

If you have troubles, just dm me on telegram: https://t.me/NikitaShekhov

@alex-streza
Copy link
Author

@Akshithpottigari I've updated it although haven't had the time to test it thoroughly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment