|
import { redirect } from "@remix-run/node"; |
|
import type { LoaderFunctionArgs } from "@remix-run/node"; |
|
import { getSession, commitSession, destroySession } from "~/lib/session.server"; |
|
import { accountLinkingService } from "~/lib/account-linking.server"; |
|
|
|
export async function loader({ request }: LoaderFunctionArgs) { |
|
const url = new URL(request.url); |
|
const service = url.searchParams.get("service"); |
|
const unlink = url.searchParams.get("unlink") === "true"; |
|
|
|
const session = await getSession(request.headers.get("Cookie")); |
|
const userSession = session.get("user"); |
|
|
|
|
|
if (!userSession) { |
|
return redirect("/"); |
|
} |
|
|
|
|
|
if (unlink && userSession.isLinked && userSession.github) { |
|
console.log(`π Unlinking accounts for GitHub user: ${userSession.github.login}`); |
|
try { |
|
|
|
accountLinkingService.removeLink(userSession.github.userId); |
|
|
|
|
|
userSession.isLinked = false; |
|
delete userSession.linkedAt; |
|
|
|
|
|
session.set("user", userSession); |
|
|
|
return redirect("/?message=accounts_unlinked", { |
|
headers: { |
|
"Set-Cookie": await commitSession(session), |
|
}, |
|
}); |
|
} catch (error) { |
|
console.error("Error unlinking accounts:", error); |
|
return redirect("/?error=unlink_failed"); |
|
} |
|
} |
|
|
|
|
|
if (service) { |
|
console.log(`π Logging out of ${service} service`); |
|
|
|
if (service === "github" && userSession.github) { |
|
|
|
delete userSession.github; |
|
|
|
|
|
if (userSession.isLinked) { |
|
userSession.isLinked = false; |
|
delete userSession.linkedAt; |
|
} |
|
|
|
|
|
session.set("user", userSession); |
|
|
|
return redirect("/", { |
|
headers: { |
|
"Set-Cookie": await commitSession(session), |
|
}, |
|
}); |
|
} |
|
|
|
if (service === "huggingface" && userSession.huggingface) { |
|
|
|
delete userSession.huggingface; |
|
|
|
|
|
if (userSession.isLinked) { |
|
userSession.isLinked = false; |
|
delete userSession.linkedAt; |
|
} |
|
|
|
|
|
session.set("user", userSession); |
|
|
|
return redirect("/", { |
|
headers: { |
|
"Set-Cookie": await commitSession(session), |
|
}, |
|
}); |
|
} |
|
} |
|
|
|
|
|
return redirect("/", { |
|
headers: { |
|
"Set-Cookie": await destroySession(session), |
|
}, |
|
}); |
|
} |