diff --git a/account/src/lib/user-utils.ts b/account/src/lib/user-utils.ts index b43f723..e7e5386 100644 --- a/account/src/lib/user-utils.ts +++ b/account/src/lib/user-utils.ts @@ -81,7 +81,6 @@ export async function upgradeUser(userId: T.UUID, subscription?: T.StripeSubscri export async function downgradeUser(userId: T.UUID, subscriptionId?: string): Promise { await deleteCouchDbAuthUser(userId); - console.log("in function downgrading user with subscriptionId", subscriptionId); if (subscriptionId) { stripe.subscriptions.del(subscriptionId); } diff --git a/account/src/routers/auth-router.ts b/account/src/routers/auth-router.ts index 15e5edb..8ad2790 100644 --- a/account/src/routers/auth-router.ts +++ b/account/src/routers/auth-router.ts @@ -194,14 +194,16 @@ const authRouter = (passport: PassportStatic) => { router.post("/downgradeToBasic", async (req, res, next) => { try { if (!req.user) { - return res.redirect("/"); + return res.send({ ok: false, error: "user not logged in" }); } - // @ts-ignore; - console.log("will downgrade user with subscription id", req.user?.subscription?.id) - await downgradeUser(req.user.userId, "subscription" in req.user - ? req.user.subscription?.id + const subscription = "subscription" in req.user ? req.user.subscription : undefined; + await downgradeUser(req.user.userId, subscription + ? subscription.id : undefined); - res.redirect("/"); + res.send({ + ok: true, + message: `account downgraded to basic${subscription ? " and subscription cancelled" : ""}`, + }); } catch (e) { next(e); } diff --git a/account/views/user.ejs b/account/views/user.ejs index e2412c1..b8a9861 100644 --- a/account/views/user.ejs +++ b/account/views/user.ejs @@ -231,7 +231,11 @@ const answer = confirm("Are you sure you want to downgrade your account? Your wordlist will be deleted forever. (Export it to CSV from the dictionary first if you want to keep it.)"); if (answer) { fetch("/downgradeToBasic", { method: "POST" }).then((res) => res.json()).then((res) => { - window.location = "/"; + if (res.ok) { + window.location = "/"; + } else { + alert("Error downgrading account"); + } }).catch((err) => { alert("Error downgrading account - check your connection"); console.error(err);