Files
feeddeck/supabase/functions/profile-v2/github.ts
T
Rico BergerandGitHub b8fdbf17a2 Rework Edge Functions (#247)
Instead of using an `import_map.json` file to define the versions for
dependencies, they are now defined directly within the import. Since the
`import_map.json` file should not be used anymore and instead a
`deno.json` file per function should be used, we decided to define them
directly with the code. The overhead compared to a `deno.json` file per
function shouldn't be that large and it makes using functions in a
self-hosted setup easier.
2025-04-23 17:00:21 +02:00

90 lines
2.4 KiB
TypeScript

import { SupabaseClient, User } from "jsr:@supabase/supabase-js@2";
import { corsHeaders } from "../_shared/utils/cors.ts";
import { log } from "../_shared/utils/log.ts";
import { encrypt } from "../_shared/utils/encrypt.ts";
/**
* `githubAddAccount` adds a new GitHub account to the users profile. A user
* must only provide a private access token to connect his GitHub account. We
* encrypt the token before we store it in the database.
*/
export const githubAddAccount = async (
supabaseClient: SupabaseClient,
user: User,
data: { token?: string } | null,
): Promise<Response> => {
if (!data || !data.token) {
return new Response(JSON.stringify({ error: "Bad Request" }), {
headers: {
...corsHeaders,
"Content-Type": "application/json; charset=utf-8",
},
status: 400,
});
}
const { error: updateError } = await supabaseClient
.from("profiles")
.update({
accountGithub: { token: await encrypt(data.token) },
})
.eq("id", user.id);
if (updateError) {
log("error", "Failed to update user profile", {
user: user,
error: updateError,
});
return new Response(JSON.stringify({ error: "Failed to update profile" }), {
headers: {
...corsHeaders,
"Content-Type": "application/json; charset=utf-8",
},
status: 500,
});
}
return new Response(undefined, {
headers: {
...corsHeaders,
"Content-Type": "application/json; charset=utf-8",
},
status: 200,
});
};
/**
* `githubDeleteAccount` deletes the users GitHub account from his profile by
* setting the value of the `accountGithub` column to `null`.
*/
export const githubDeleteAccount = async (
supabaseClient: SupabaseClient,
user: User,
): Promise<Response> => {
const { error: updateError } = await supabaseClient
.from("profiles")
.update({
accountGithub: null,
})
.eq("id", user.id);
if (updateError) {
log("error", "Failed to update user profile", {
user: user,
error: updateError,
});
return new Response(JSON.stringify({ error: "Failed to update profile" }), {
headers: {
...corsHeaders,
"Content-Type": "application/json; charset=utf-8",
},
status: 500,
});
}
return new Response(undefined, {
headers: {
...corsHeaders,
"Content-Type": "application/json; charset=utf-8",
},
status: 200,
});
};