* feat: replace manual oauth with better-auth * fix: lint * chore: refactor accounts to unify github + vercel * feat: enhance auth config with host allowlisting Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com> * fix: add newline at end of manifest.json Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com> * fix: update betterAuth database schema mappings Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com> * fix: sanitize redirects and preserve destination in github flow Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com> * feat: enhance GitHub app callback with installation sync tracking Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com> * build: add BETTER_AUTH_SECRET to turbo env Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com> * refactor: update sign-in callback to skip onboarding Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com> --------- Co-authored-by: nicoalbanese <49612682+nicoalbanese@users.noreply.github.com> Co-authored-by: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
"use server";
|
|
|
|
import { cookies } from "next/headers";
|
|
import { deleteInstallationsByUserId } from "@/lib/db/installations";
|
|
import {
|
|
deleteGitHubAccountLink,
|
|
getUserGitHubToken,
|
|
hasGitHubAccount,
|
|
} from "@/lib/github/token";
|
|
import { getServerSession } from "@/lib/session/get-server-session";
|
|
|
|
export async function unlinkGitHub(): Promise<{
|
|
success: boolean;
|
|
error?: string;
|
|
}> {
|
|
const session = await getServerSession();
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: "Not authenticated" };
|
|
}
|
|
|
|
try {
|
|
const linked = await hasGitHubAccount(session.user.id);
|
|
if (!linked) {
|
|
await deleteInstallationsByUserId(session.user.id);
|
|
return { success: true };
|
|
}
|
|
|
|
// revoke the github token before unlinking
|
|
try {
|
|
const token = await getUserGitHubToken(session.user.id);
|
|
if (token) {
|
|
const clientId = process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID;
|
|
const clientSecret = process.env.GITHUB_CLIENT_SECRET;
|
|
if (clientId && clientSecret) {
|
|
await fetch(`https://api.github.com/applications/${clientId}/token`, {
|
|
method: "DELETE",
|
|
headers: {
|
|
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`,
|
|
Accept: "application/vnd.github.v3+json",
|
|
},
|
|
body: JSON.stringify({ access_token: token }),
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to revoke GitHub token:", error);
|
|
}
|
|
|
|
await Promise.all([
|
|
deleteGitHubAccountLink(session.user.id),
|
|
deleteInstallationsByUserId(session.user.id),
|
|
]);
|
|
|
|
const cookieStore = await cookies();
|
|
cookieStore.set("github_reconnect", "1", {
|
|
path: "/",
|
|
secure: process.env.NODE_ENV === "production",
|
|
httpOnly: true,
|
|
maxAge: 60 * 60,
|
|
sameSite: "lax",
|
|
});
|
|
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Failed to unlink GitHub:", error);
|
|
return { success: false, error: "Failed to unlink GitHub account" };
|
|
}
|
|
}
|