Skip to content

Instantly share code, notes, and snippets.

@erickoledadevrel
Created February 17, 2023 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erickoledadevrel/6a6ab6508b50d9331eb096f6c443fa1e to your computer and use it in GitHub Desktop.
Save erickoledadevrel/6a6ab6508b50d9331eb096f6c443fa1e to your computer and use it in GitHub Desktop.
Pack that authenticates with Supabase
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
// TODO: Set domain and API key.
const ProjectDomain = "<subdomain>.supabase.co";
const ApiKey = <apikey>";
const ProjectUrl = `https://${ProjectDomain}`;
pack.addNetworkDomain(ProjectDomain);
pack.setUserAuthentication({
type: coda.AuthenticationType.Custom,
params: [
{
name: "email",
description: "",
},
{
name: "password",
description: "",
}
],
getConnectionName: async function (context) {
let token = await getToken(context);
let response = await context.fetcher.fetch({
method: "GET",
url: coda.joinUrl(ProjectUrl, "/auth/v1/user"),
headers: {
apiKey: ApiKey,
Authorization: `Bearer ${token}`,
},
});
let user = response.body;
return user.email;
},
});
const TodoSchema = coda.makeObjectSchema({
properties: {
todoId: { type: coda.ValueType.Number, fromKey: "id" },
task: { type: coda.ValueType.String },
is_complete: { type: coda.ValueType.Boolean },
},
displayProperty: "task",
});
pack.addFormula({
name: "Todos",
description: "",
parameters: [],
resultType: coda.ValueType.Array,
items: TodoSchema,
execute: async function (args, context) {
let token = await getToken(context);
let response = await context.fetcher.fetch({
method: "GET",
url: coda.joinUrl(ProjectUrl, "/rest/v1/todos"),
headers: {
apiKey: ApiKey,
Authorization: `Bearer ${token}`,
},
});
let data = response.body;
return data;
},
});
async function getToken(context: coda.ExecutionContext) {
let it = context.invocationToken;
let payload = {
email: `{{email-${it}}}`,
password: `{{password-${it}}}`,
};
let response = await context.fetcher.fetch({
method: "POST",
url: coda.joinUrl(ProjectUrl, "/auth/v1/token?grant_type=password"),
headers: {
apiKey: ApiKey,
},
body: JSON.stringify(payload),
});
let data = response.body;
return data.access_token;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment