mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-16 14:38:15 +00:00
Compare commits
3 Commits
dev
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a1e633607 | ||
|
|
9eacefb155 | ||
|
|
843b13ed57 |
39
.github/workflows/cicd.yml
vendored
39
.github/workflows/cicd.yml
vendored
@@ -525,41 +525,10 @@ jobs:
|
|||||||
VERIFIED_INDEX_KEYLESS=false
|
VERIFIED_INDEX_KEYLESS=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If index verification fails, attempt to verify child platform manifests
|
# Check if verification succeeded
|
||||||
if [ "${VERIFIED_INDEX}" != "true" ] || [ "${VERIFIED_INDEX_KEYLESS}" != "true" ]; then
|
if [ "${VERIFIED_INDEX}" != "true" ] && [ "${VERIFIED_INDEX_KEYLESS}" != "true" ]; then
|
||||||
echo "Index verification not available; attempting child manifest verification for ${BASE_IMAGE}:${IMAGE_TAG}"
|
echo "⚠️ WARNING: Verification not available for ${BASE_IMAGE}:${IMAGE_TAG}"
|
||||||
CHILD_VERIFIED=false
|
echo "This may be due to registry propagation delays. Continuing anyway."
|
||||||
|
|
||||||
for ARCH in arm64 amd64; do
|
|
||||||
CHILD_TAG="${IMAGE_TAG}-${ARCH}"
|
|
||||||
echo "Resolving child digest for ${BASE_IMAGE}:${CHILD_TAG}"
|
|
||||||
CHILD_DIGEST="$(skopeo inspect --retry-times 3 docker://${BASE_IMAGE}:${CHILD_TAG} | jq -r '.Digest' || true)"
|
|
||||||
if [ -n "${CHILD_DIGEST}" ] && [ "${CHILD_DIGEST}" != "null" ]; then
|
|
||||||
CHILD_REF="${BASE_IMAGE}@${CHILD_DIGEST}"
|
|
||||||
echo "==> cosign verify (public key) child ${CHILD_REF}"
|
|
||||||
if retry_verify "cosign verify --key env://COSIGN_PUBLIC_KEY '${CHILD_REF}' -o text"; then
|
|
||||||
CHILD_VERIFIED=true
|
|
||||||
echo "Public key verification succeeded for child ${CHILD_REF}"
|
|
||||||
else
|
|
||||||
echo "Public key verification failed for child ${CHILD_REF}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "==> cosign verify (keyless policy) child ${CHILD_REF}"
|
|
||||||
if retry_verify "cosign verify --certificate-oidc-issuer '${issuer}' --certificate-identity-regexp '${id_regex}' '${CHILD_REF}' -o text"; then
|
|
||||||
CHILD_VERIFIED=true
|
|
||||||
echo "Keyless verification succeeded for child ${CHILD_REF}"
|
|
||||||
else
|
|
||||||
echo "Keyless verification failed for child ${CHILD_REF}"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "No child digest found for ${BASE_IMAGE}:${CHILD_TAG}; skipping"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ "${CHILD_VERIFIED}" != "true" ]; then
|
|
||||||
echo "Failed to verify index and no child manifests verified for ${BASE_IMAGE}:${IMAGE_TAG}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
) || TAG_FAILED=true
|
) || TAG_FAILED=true
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ module installer
|
|||||||
go 1.24.0
|
go 1.24.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
golang.org/x/term v0.39.0
|
golang.org/x/term v0.40.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require golang.org/x/sys v0.40.0 // indirect
|
require golang.org/x/sys v0.41.0 // indirect
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
|
golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg=
|
||||||
golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww=
|
golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
@@ -201,6 +201,7 @@
|
|||||||
"protocolSelect": "Select a protocol",
|
"protocolSelect": "Select a protocol",
|
||||||
"resourcePortNumber": "Port Number",
|
"resourcePortNumber": "Port Number",
|
||||||
"resourcePortNumberDescription": "The external port number to proxy requests.",
|
"resourcePortNumberDescription": "The external port number to proxy requests.",
|
||||||
|
"back": "Back",
|
||||||
"cancel": "Cancel",
|
"cancel": "Cancel",
|
||||||
"resourceConfig": "Configuration Snippets",
|
"resourceConfig": "Configuration Snippets",
|
||||||
"resourceConfigDescription": "Copy and paste these configuration snippets to set up the TCP/UDP resource",
|
"resourceConfigDescription": "Copy and paste these configuration snippets to set up the TCP/UDP resource",
|
||||||
@@ -246,6 +247,17 @@
|
|||||||
"orgErrorDeleteMessage": "An error occurred while deleting the organization.",
|
"orgErrorDeleteMessage": "An error occurred while deleting the organization.",
|
||||||
"orgDeleted": "Organization deleted",
|
"orgDeleted": "Organization deleted",
|
||||||
"orgDeletedMessage": "The organization and its data has been deleted.",
|
"orgDeletedMessage": "The organization and its data has been deleted.",
|
||||||
|
"deleteAccount": "Delete Account",
|
||||||
|
"deleteAccountDescription": "Permanently delete your account, all organizations you own, and all data within those organizations. This cannot be undone.",
|
||||||
|
"deleteAccountButton": "Delete Account",
|
||||||
|
"deleteAccountConfirmTitle": "Delete Account",
|
||||||
|
"deleteAccountConfirmMessage": "This will permanently wipe your account, all organizations you own, and all data within those organizations. This cannot be undone.",
|
||||||
|
"deleteAccountConfirmString": "delete account",
|
||||||
|
"deleteAccountSuccess": "Account Deleted",
|
||||||
|
"deleteAccountSuccessMessage": "Your account has been deleted.",
|
||||||
|
"deleteAccountError": "Failed to delete account",
|
||||||
|
"deleteAccountPreviewAccount": "Your Account",
|
||||||
|
"deleteAccountPreviewOrgs": "Organizations you own (and all their data)",
|
||||||
"orgMissing": "Organization ID Missing",
|
"orgMissing": "Organization ID Missing",
|
||||||
"orgMissingMessage": "Unable to regenerate invitation without an organization ID.",
|
"orgMissingMessage": "Unable to regenerate invitation without an organization ID.",
|
||||||
"accessUsersManage": "Manage Users",
|
"accessUsersManage": "Manage Users",
|
||||||
|
|||||||
169
server/lib/deleteOrg.ts
Normal file
169
server/lib/deleteOrg.ts
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
import {
|
||||||
|
clients,
|
||||||
|
clientSiteResourcesAssociationsCache,
|
||||||
|
clientSitesAssociationsCache,
|
||||||
|
db,
|
||||||
|
domains,
|
||||||
|
olms,
|
||||||
|
orgDomains,
|
||||||
|
orgs,
|
||||||
|
resources,
|
||||||
|
sites
|
||||||
|
} from "@server/db";
|
||||||
|
import { newts, newtSessions } from "@server/db";
|
||||||
|
import { eq, and, inArray, sql } from "drizzle-orm";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { sendToClient } from "#dynamic/routers/ws";
|
||||||
|
import { deletePeer } from "@server/routers/gerbil/peers";
|
||||||
|
import { OlmErrorCodes } from "@server/routers/olm/error";
|
||||||
|
import { sendTerminateClient } from "@server/routers/client/terminate";
|
||||||
|
|
||||||
|
export type DeleteOrgByIdResult = {
|
||||||
|
deletedNewtIds: string[];
|
||||||
|
olmsToTerminate: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes one organization and its related data. Returns ids for termination
|
||||||
|
* messages; caller should call sendTerminationMessages with the result.
|
||||||
|
* Throws if org not found.
|
||||||
|
*/
|
||||||
|
export async function deleteOrgById(
|
||||||
|
orgId: string
|
||||||
|
): Promise<DeleteOrgByIdResult> {
|
||||||
|
const [org] = await db
|
||||||
|
.select()
|
||||||
|
.from(orgs)
|
||||||
|
.where(eq(orgs.orgId, orgId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!org) {
|
||||||
|
throw createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
`Organization with ID ${orgId} not found`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const orgSites = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.orgId, orgId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
const orgClients = await db
|
||||||
|
.select()
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.orgId, orgId));
|
||||||
|
|
||||||
|
const deletedNewtIds: string[] = [];
|
||||||
|
const olmsToTerminate: string[] = [];
|
||||||
|
|
||||||
|
await db.transaction(async (trx) => {
|
||||||
|
for (const site of orgSites) {
|
||||||
|
if (site.pubKey) {
|
||||||
|
if (site.type == "wireguard") {
|
||||||
|
await deletePeer(site.exitNodeId!, site.pubKey);
|
||||||
|
} else if (site.type == "newt") {
|
||||||
|
const [deletedNewt] = await trx
|
||||||
|
.delete(newts)
|
||||||
|
.where(eq(newts.siteId, site.siteId))
|
||||||
|
.returning();
|
||||||
|
if (deletedNewt) {
|
||||||
|
deletedNewtIds.push(deletedNewt.newtId);
|
||||||
|
await trx
|
||||||
|
.delete(newtSessions)
|
||||||
|
.where(
|
||||||
|
eq(newtSessions.newtId, deletedNewt.newtId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info(`Deleting site ${site.siteId}`);
|
||||||
|
await trx.delete(sites).where(eq(sites.siteId, site.siteId));
|
||||||
|
}
|
||||||
|
for (const client of orgClients) {
|
||||||
|
const [olm] = await trx
|
||||||
|
.select()
|
||||||
|
.from(olms)
|
||||||
|
.where(eq(olms.clientId, client.clientId))
|
||||||
|
.limit(1);
|
||||||
|
if (olm) {
|
||||||
|
olmsToTerminate.push(olm.olmId);
|
||||||
|
}
|
||||||
|
logger.info(`Deleting client ${client.clientId}`);
|
||||||
|
await trx
|
||||||
|
.delete(clients)
|
||||||
|
.where(eq(clients.clientId, client.clientId));
|
||||||
|
await trx
|
||||||
|
.delete(clientSiteResourcesAssociationsCache)
|
||||||
|
.where(
|
||||||
|
eq(
|
||||||
|
clientSiteResourcesAssociationsCache.clientId,
|
||||||
|
client.clientId
|
||||||
|
)
|
||||||
|
);
|
||||||
|
await trx
|
||||||
|
.delete(clientSitesAssociationsCache)
|
||||||
|
.where(
|
||||||
|
eq(clientSitesAssociationsCache.clientId, client.clientId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const allOrgDomains = await trx
|
||||||
|
.select()
|
||||||
|
.from(orgDomains)
|
||||||
|
.innerJoin(domains, eq(domains.domainId, orgDomains.domainId))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(orgDomains.orgId, orgId),
|
||||||
|
eq(domains.configManaged, false)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const domainIdsToDelete: string[] = [];
|
||||||
|
for (const orgDomain of allOrgDomains) {
|
||||||
|
const domainId = orgDomain.domains.domainId;
|
||||||
|
const orgCount = await trx
|
||||||
|
.select({ count: sql<number>`count(*)` })
|
||||||
|
.from(orgDomains)
|
||||||
|
.where(eq(orgDomains.domainId, domainId));
|
||||||
|
if (orgCount[0].count === 1) {
|
||||||
|
domainIdsToDelete.push(domainId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (domainIdsToDelete.length > 0) {
|
||||||
|
await trx
|
||||||
|
.delete(domains)
|
||||||
|
.where(inArray(domains.domainId, domainIdsToDelete));
|
||||||
|
}
|
||||||
|
await trx.delete(resources).where(eq(resources.orgId, orgId));
|
||||||
|
await trx.delete(orgs).where(eq(orgs.orgId, orgId));
|
||||||
|
});
|
||||||
|
|
||||||
|
return { deletedNewtIds, olmsToTerminate };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sendTerminationMessages(result: DeleteOrgByIdResult): void {
|
||||||
|
for (const newtId of result.deletedNewtIds) {
|
||||||
|
sendToClient(newtId, { type: `newt/wg/terminate`, data: {} }).catch(
|
||||||
|
(error) => {
|
||||||
|
logger.error(
|
||||||
|
"Failed to send termination message to newt:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (const olmId of result.olmsToTerminate) {
|
||||||
|
sendTerminateClient(
|
||||||
|
0,
|
||||||
|
OlmErrorCodes.TERMINATED_REKEYED,
|
||||||
|
olmId
|
||||||
|
).catch((error) => {
|
||||||
|
logger.error(
|
||||||
|
"Failed to send termination message to olm:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
228
server/routers/auth/deleteMyAccount.ts
Normal file
228
server/routers/auth/deleteMyAccount.ts
Normal file
@@ -0,0 +1,228 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db, orgs, userOrgs, users } from "@server/db";
|
||||||
|
import { eq, and, inArray } from "drizzle-orm";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
import { verifySession } from "@server/auth/sessions/verifySession";
|
||||||
|
import {
|
||||||
|
invalidateSession,
|
||||||
|
createBlankSessionTokenCookie
|
||||||
|
} from "@server/auth/sessions/app";
|
||||||
|
import { verifyPassword } from "@server/auth/password";
|
||||||
|
import { verifyTotpCode } from "@server/auth/totp";
|
||||||
|
import { calculateUserClientsForOrgs } from "@server/lib/calculateUserClientsForOrgs";
|
||||||
|
import {
|
||||||
|
deleteOrgById,
|
||||||
|
sendTerminationMessages
|
||||||
|
} from "@server/lib/deleteOrg";
|
||||||
|
import { UserType } from "@server/types/UserTypes";
|
||||||
|
|
||||||
|
const deleteMyAccountBody = z.strictObject({
|
||||||
|
password: z.string().optional(),
|
||||||
|
code: z.string().optional()
|
||||||
|
});
|
||||||
|
|
||||||
|
export type DeleteMyAccountPreviewResponse = {
|
||||||
|
preview: true;
|
||||||
|
orgs: { orgId: string; name: string }[];
|
||||||
|
twoFactorEnabled: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DeleteMyAccountCodeRequestedResponse = {
|
||||||
|
codeRequested: true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DeleteMyAccountSuccessResponse = {
|
||||||
|
success: true;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Self-service account deletion (saas only). Returns preview when no password;
|
||||||
|
* requires password and optional 2FA code to perform deletion. Uses shared
|
||||||
|
* deleteOrgById for each owned org (delete-my-account may delete multiple orgs).
|
||||||
|
*/
|
||||||
|
export async function deleteMyAccount(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const { user, session } = await verifySession(req);
|
||||||
|
if (!user || !session) {
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.UNAUTHORIZED, "Not authenticated")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.serverAdmin) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
"Server admins cannot delete their account this way"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.type !== UserType.Internal) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
"Account deletion with password is only supported for internal users"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = deleteMyAccountBody.safeParse(req.body ?? {});
|
||||||
|
if (!parsed.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsed.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const { password, code } = parsed.data;
|
||||||
|
|
||||||
|
const userId = user.userId;
|
||||||
|
|
||||||
|
const ownedOrgsRows = await db
|
||||||
|
.select({
|
||||||
|
orgId: userOrgs.orgId
|
||||||
|
})
|
||||||
|
.from(userOrgs)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(userOrgs.userId, userId),
|
||||||
|
eq(userOrgs.isOwner, true)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
const orgIds = ownedOrgsRows.map((r) => r.orgId);
|
||||||
|
|
||||||
|
if (!password) {
|
||||||
|
const orgsWithNames =
|
||||||
|
orgIds.length > 0
|
||||||
|
? await db
|
||||||
|
.select({
|
||||||
|
orgId: orgs.orgId,
|
||||||
|
name: orgs.name
|
||||||
|
})
|
||||||
|
.from(orgs)
|
||||||
|
.where(inArray(orgs.orgId, orgIds))
|
||||||
|
: [];
|
||||||
|
return response<DeleteMyAccountPreviewResponse>(res, {
|
||||||
|
data: {
|
||||||
|
preview: true,
|
||||||
|
orgs: orgsWithNames.map((o) => ({
|
||||||
|
orgId: o.orgId,
|
||||||
|
name: o.name ?? ""
|
||||||
|
})),
|
||||||
|
twoFactorEnabled: user.twoFactorEnabled ?? false
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Preview",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const validPassword = await verifyPassword(
|
||||||
|
password,
|
||||||
|
user.passwordHash!
|
||||||
|
);
|
||||||
|
if (!validPassword) {
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.UNAUTHORIZED, "Invalid password")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.twoFactorEnabled) {
|
||||||
|
if (!code) {
|
||||||
|
return response<DeleteMyAccountCodeRequestedResponse>(res, {
|
||||||
|
data: { codeRequested: true },
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Two-factor code required",
|
||||||
|
status: HttpCode.ACCEPTED
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const validOTP = await verifyTotpCode(
|
||||||
|
code,
|
||||||
|
user.twoFactorSecret!,
|
||||||
|
user.userId
|
||||||
|
);
|
||||||
|
if (!validOTP) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
"The two-factor code you entered is incorrect"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const allDeletedNewtIds: string[] = [];
|
||||||
|
const allOlmsToTerminate: string[] = [];
|
||||||
|
|
||||||
|
for (const row of ownedOrgsRows) {
|
||||||
|
try {
|
||||||
|
const result = await deleteOrgById(row.orgId);
|
||||||
|
allDeletedNewtIds.push(...result.deletedNewtIds);
|
||||||
|
allOlmsToTerminate.push(...result.olmsToTerminate);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(
|
||||||
|
`Failed to delete org ${row.orgId} during account deletion`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"Failed to delete organization"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendTerminationMessages({
|
||||||
|
deletedNewtIds: allDeletedNewtIds,
|
||||||
|
olmsToTerminate: allOlmsToTerminate
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.transaction(async (trx) => {
|
||||||
|
await trx.delete(users).where(eq(users.userId, userId));
|
||||||
|
await calculateUserClientsForOrgs(userId, trx);
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await invalidateSession(session.sessionId);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(
|
||||||
|
"Failed to invalidate session after account deletion",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSecure = req.protocol === "https";
|
||||||
|
res.setHeader("Set-Cookie", createBlankSessionTokenCookie(isSecure));
|
||||||
|
|
||||||
|
return response<DeleteMyAccountSuccessResponse>(res, {
|
||||||
|
data: { success: true },
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Account deleted successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"An error occurred"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,4 +17,5 @@ export * from "./securityKey";
|
|||||||
export * from "./startDeviceWebAuth";
|
export * from "./startDeviceWebAuth";
|
||||||
export * from "./verifyDeviceWebAuth";
|
export * from "./verifyDeviceWebAuth";
|
||||||
export * from "./pollDeviceWebAuth";
|
export * from "./pollDeviceWebAuth";
|
||||||
export * from "./lookupUser";
|
export * from "./lookupUser";
|
||||||
|
export * from "./deleteMyAccount";
|
||||||
@@ -1164,6 +1164,7 @@ authRouter.post(
|
|||||||
auth.login
|
auth.login
|
||||||
);
|
);
|
||||||
authRouter.post("/logout", auth.logout);
|
authRouter.post("/logout", auth.logout);
|
||||||
|
authRouter.post("/delete-my-account", auth.deleteMyAccount);
|
||||||
authRouter.post(
|
authRouter.post(
|
||||||
"/lookup-user",
|
"/lookup-user",
|
||||||
rateLimit({
|
rateLimit({
|
||||||
|
|||||||
@@ -1,28 +1,12 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import {
|
|
||||||
clients,
|
|
||||||
clientSiteResourcesAssociationsCache,
|
|
||||||
clientSitesAssociationsCache,
|
|
||||||
db,
|
|
||||||
domains,
|
|
||||||
olms,
|
|
||||||
orgDomains,
|
|
||||||
resources
|
|
||||||
} from "@server/db";
|
|
||||||
import { newts, newtSessions, orgs, sites, userActions } from "@server/db";
|
|
||||||
import { eq, and, inArray, sql } from "drizzle-orm";
|
|
||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { sendToClient } from "#dynamic/routers/ws";
|
|
||||||
import { deletePeer } from "../gerbil/peers";
|
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
import { OlmErrorCodes } from "../olm/error";
|
import { deleteOrgById, sendTerminationMessages } from "@server/lib/deleteOrg";
|
||||||
import { sendTerminateClient } from "../client/terminate";
|
|
||||||
|
|
||||||
const deleteOrgSchema = z.strictObject({
|
const deleteOrgSchema = z.strictObject({
|
||||||
orgId: z.string()
|
orgId: z.string()
|
||||||
@@ -56,170 +40,9 @@ export async function deleteOrg(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { orgId } = parsedParams.data;
|
const { orgId } = parsedParams.data;
|
||||||
|
const result = await deleteOrgById(orgId);
|
||||||
const [org] = await db
|
sendTerminationMessages(result);
|
||||||
.select()
|
|
||||||
.from(orgs)
|
|
||||||
.where(eq(orgs.orgId, orgId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!org) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.NOT_FOUND,
|
|
||||||
`Organization with ID ${orgId} not found`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// we need to handle deleting each site
|
|
||||||
const orgSites = await db
|
|
||||||
.select()
|
|
||||||
.from(sites)
|
|
||||||
.where(eq(sites.orgId, orgId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
const orgClients = await db
|
|
||||||
.select()
|
|
||||||
.from(clients)
|
|
||||||
.where(eq(clients.orgId, orgId));
|
|
||||||
|
|
||||||
const deletedNewtIds: string[] = [];
|
|
||||||
const olmsToTerminate: string[] = [];
|
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
|
||||||
for (const site of orgSites) {
|
|
||||||
if (site.pubKey) {
|
|
||||||
if (site.type == "wireguard") {
|
|
||||||
await deletePeer(site.exitNodeId!, site.pubKey);
|
|
||||||
} else if (site.type == "newt") {
|
|
||||||
// get the newt on the site by querying the newt table for siteId
|
|
||||||
const [deletedNewt] = await trx
|
|
||||||
.delete(newts)
|
|
||||||
.where(eq(newts.siteId, site.siteId))
|
|
||||||
.returning();
|
|
||||||
if (deletedNewt) {
|
|
||||||
deletedNewtIds.push(deletedNewt.newtId);
|
|
||||||
|
|
||||||
// delete all of the sessions for the newt
|
|
||||||
await trx
|
|
||||||
.delete(newtSessions)
|
|
||||||
.where(
|
|
||||||
eq(newtSessions.newtId, deletedNewt.newtId)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(`Deleting site ${site.siteId}`);
|
|
||||||
await trx.delete(sites).where(eq(sites.siteId, site.siteId));
|
|
||||||
}
|
|
||||||
for (const client of orgClients) {
|
|
||||||
const [olm] = await trx
|
|
||||||
.select()
|
|
||||||
.from(olms)
|
|
||||||
.where(eq(olms.clientId, client.clientId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (olm) {
|
|
||||||
olmsToTerminate.push(olm.olmId);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(`Deleting client ${client.clientId}`);
|
|
||||||
await trx
|
|
||||||
.delete(clients)
|
|
||||||
.where(eq(clients.clientId, client.clientId));
|
|
||||||
|
|
||||||
// also delete the associations
|
|
||||||
await trx
|
|
||||||
.delete(clientSiteResourcesAssociationsCache)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
clientSiteResourcesAssociationsCache.clientId,
|
|
||||||
client.clientId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
await trx
|
|
||||||
.delete(clientSitesAssociationsCache)
|
|
||||||
.where(
|
|
||||||
eq(
|
|
||||||
clientSitesAssociationsCache.clientId,
|
|
||||||
client.clientId
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const allOrgDomains = await trx
|
|
||||||
.select()
|
|
||||||
.from(orgDomains)
|
|
||||||
.innerJoin(domains, eq(domains.domainId, orgDomains.domainId))
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(orgDomains.orgId, orgId),
|
|
||||||
eq(domains.configManaged, false)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// For each domain, check if it belongs to multiple organizations
|
|
||||||
const domainIdsToDelete: string[] = [];
|
|
||||||
for (const orgDomain of allOrgDomains) {
|
|
||||||
const domainId = orgDomain.domains.domainId;
|
|
||||||
|
|
||||||
// Count how many organizations this domain belongs to
|
|
||||||
const orgCount = await trx
|
|
||||||
.select({ count: sql<number>`count(*)` })
|
|
||||||
.from(orgDomains)
|
|
||||||
.where(eq(orgDomains.domainId, domainId));
|
|
||||||
|
|
||||||
// Only delete the domain if it belongs to exactly 1 organization (the one being deleted)
|
|
||||||
if (orgCount[0].count === 1) {
|
|
||||||
domainIdsToDelete.push(domainId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete domains that belong exclusively to this organization
|
|
||||||
if (domainIdsToDelete.length > 0) {
|
|
||||||
await trx
|
|
||||||
.delete(domains)
|
|
||||||
.where(inArray(domains.domainId, domainIdsToDelete));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete resources
|
|
||||||
await trx.delete(resources).where(eq(resources.orgId, orgId));
|
|
||||||
|
|
||||||
await trx.delete(orgs).where(eq(orgs.orgId, orgId));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send termination messages outside of transaction to prevent blocking
|
|
||||||
for (const newtId of deletedNewtIds) {
|
|
||||||
const payload = {
|
|
||||||
type: `newt/wg/terminate`,
|
|
||||||
data: {}
|
|
||||||
};
|
|
||||||
// Don't await this to prevent blocking the response
|
|
||||||
sendToClient(newtId, payload).catch((error) => {
|
|
||||||
logger.error(
|
|
||||||
"Failed to send termination message to newt:",
|
|
||||||
error
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const olmId of olmsToTerminate) {
|
|
||||||
sendTerminateClient(
|
|
||||||
0, // clientId not needed since we're passing olmId
|
|
||||||
OlmErrorCodes.TERMINATED_REKEYED,
|
|
||||||
olmId
|
|
||||||
).catch((error) => {
|
|
||||||
logger.error(
|
|
||||||
"Failed to send termination message to olm:",
|
|
||||||
error
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return response(res, {
|
return response(res, {
|
||||||
data: null,
|
data: null,
|
||||||
success: true,
|
success: true,
|
||||||
@@ -228,6 +51,9 @@ export async function deleteOrg(
|
|||||||
status: HttpCode.OK
|
status: HttpCode.OK
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (createHttpError.isHttpError(error)) {
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(
|
||||||
|
|||||||
74
src/app/auth/delete-account/DeleteAccountClient.tsx
Normal file
74
src/app/auth/delete-account/DeleteAccountClient.tsx
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { useTranslations } from "next-intl";
|
||||||
|
import { Button } from "@app/components/ui/button";
|
||||||
|
import DeleteAccountConfirmDialog from "@app/components/DeleteAccountConfirmDialog";
|
||||||
|
import UserProfileCard from "@app/components/UserProfileCard";
|
||||||
|
import { ArrowLeft } from "lucide-react";
|
||||||
|
import { createApiClient } from "@app/lib/api";
|
||||||
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||||
|
import { toast } from "@app/hooks/useToast";
|
||||||
|
import { formatAxiosError } from "@app/lib/api";
|
||||||
|
|
||||||
|
type DeleteAccountClientProps = {
|
||||||
|
displayName: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function DeleteAccountClient({
|
||||||
|
displayName
|
||||||
|
}: DeleteAccountClientProps) {
|
||||||
|
const router = useRouter();
|
||||||
|
const t = useTranslations();
|
||||||
|
const { env } = useEnvContext();
|
||||||
|
const api = createApiClient({ env });
|
||||||
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
||||||
|
|
||||||
|
function handleUseDifferentAccount() {
|
||||||
|
api.post("/auth/logout")
|
||||||
|
.catch((e) => {
|
||||||
|
console.error(t("logoutError"), e);
|
||||||
|
toast({
|
||||||
|
title: t("logoutError"),
|
||||||
|
description: formatAxiosError(e, t("logoutError"))
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
router.push(
|
||||||
|
"/auth/login?internal_redirect=/auth/delete-account"
|
||||||
|
);
|
||||||
|
router.refresh();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
<UserProfileCard
|
||||||
|
identifier={displayName}
|
||||||
|
description={t("signingAs")}
|
||||||
|
onUseDifferentAccount={handleUseDifferentAccount}
|
||||||
|
useDifferentAccountText={t("deviceLoginUseDifferentAccount")}
|
||||||
|
/>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{t("deleteAccountDescription")}
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Button variant="outline" onClick={() => router.back()}>
|
||||||
|
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||||
|
{t("back")}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
onClick={() => setIsDialogOpen(true)}
|
||||||
|
>
|
||||||
|
{t("deleteAccountButton")}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<DeleteAccountConfirmDialog
|
||||||
|
open={isDialogOpen}
|
||||||
|
setOpen={setIsDialogOpen}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
28
src/app/auth/delete-account/page.tsx
Normal file
28
src/app/auth/delete-account/page.tsx
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { verifySession } from "@app/lib/auth/verifySession";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { build } from "@server/build";
|
||||||
|
import { cache } from "react";
|
||||||
|
import DeleteAccountClient from "./DeleteAccountClient";
|
||||||
|
import { getTranslations } from "next-intl/server";
|
||||||
|
import { getUserDisplayName } from "@app/lib/getUserDisplayName";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
export default async function DeleteAccountPage() {
|
||||||
|
const getUser = cache(verifySession);
|
||||||
|
const user = await getUser({ skipCheckVerifyEmail: true });
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
redirect("/auth/login");
|
||||||
|
}
|
||||||
|
|
||||||
|
const t = await getTranslations();
|
||||||
|
const displayName = getUserDisplayName({ user });
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<h1 className="text-xl font-semibold">{t("deleteAccount")}</h1>
|
||||||
|
<DeleteAccountClient displayName={displayName} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { consumeInternalRedirectPath } from "@app/lib/internalRedirect";
|
import { getInternalRedirectTarget } from "@app/lib/internalRedirect";
|
||||||
|
|
||||||
type ApplyInternalRedirectProps = {
|
type ApplyInternalRedirectProps = {
|
||||||
orgId: string;
|
orgId: string;
|
||||||
@@ -14,9 +14,9 @@ export default function ApplyInternalRedirect({
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const path = consumeInternalRedirectPath();
|
const target = getInternalRedirectTarget(orgId);
|
||||||
if (path) {
|
if (target) {
|
||||||
router.replace(`/${orgId}${path}`);
|
router.replace(target);
|
||||||
}
|
}
|
||||||
}, [orgId, router]);
|
}, [orgId, router]);
|
||||||
|
|
||||||
|
|||||||
414
src/components/DeleteAccountConfirmDialog.tsx
Normal file
414
src/components/DeleteAccountConfirmDialog.tsx
Normal file
@@ -0,0 +1,414 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, useEffect, useMemo } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { createApiClient } from "@app/lib/api";
|
||||||
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||||
|
import { formatAxiosError } from "@app/lib/api";
|
||||||
|
import { toast } from "@app/hooks/useToast";
|
||||||
|
import { useTranslations } from "next-intl";
|
||||||
|
import { Button } from "@app/components/ui/button";
|
||||||
|
import {
|
||||||
|
Credenza,
|
||||||
|
CredenzaBody,
|
||||||
|
CredenzaClose,
|
||||||
|
CredenzaContent,
|
||||||
|
CredenzaFooter,
|
||||||
|
CredenzaHeader,
|
||||||
|
CredenzaTitle
|
||||||
|
} from "@app/components/Credenza";
|
||||||
|
import {
|
||||||
|
Form,
|
||||||
|
FormControl,
|
||||||
|
FormField,
|
||||||
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
|
FormMessage
|
||||||
|
} from "@app/components/ui/form";
|
||||||
|
import { Input } from "@app/components/ui/input";
|
||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
|
import { z } from "zod";
|
||||||
|
import {
|
||||||
|
InputOTP,
|
||||||
|
InputOTPGroup,
|
||||||
|
InputOTPSlot
|
||||||
|
} from "@app/components/ui/input-otp";
|
||||||
|
import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp";
|
||||||
|
import type {
|
||||||
|
DeleteMyAccountPreviewResponse,
|
||||||
|
DeleteMyAccountCodeRequestedResponse,
|
||||||
|
DeleteMyAccountSuccessResponse
|
||||||
|
} from "@server/routers/auth/deleteMyAccount";
|
||||||
|
import { AxiosResponse } from "axios";
|
||||||
|
|
||||||
|
type DeleteAccountConfirmDialogProps = {
|
||||||
|
open: boolean;
|
||||||
|
setOpen: (open: boolean) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function DeleteAccountConfirmDialog({
|
||||||
|
open,
|
||||||
|
setOpen
|
||||||
|
}: DeleteAccountConfirmDialogProps) {
|
||||||
|
const { env } = useEnvContext();
|
||||||
|
const api = createApiClient({ env });
|
||||||
|
const router = useRouter();
|
||||||
|
const t = useTranslations();
|
||||||
|
|
||||||
|
const passwordSchema = useMemo(
|
||||||
|
() =>
|
||||||
|
z.object({
|
||||||
|
password: z.string().min(1, { message: t("passwordRequired") })
|
||||||
|
}),
|
||||||
|
[t]
|
||||||
|
);
|
||||||
|
|
||||||
|
const codeSchema = useMemo(
|
||||||
|
() =>
|
||||||
|
z.object({
|
||||||
|
code: z.string().length(6, { message: t("pincodeInvalid") })
|
||||||
|
}),
|
||||||
|
[t]
|
||||||
|
);
|
||||||
|
|
||||||
|
const [step, setStep] = useState<0 | 1 | 2>(0);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [loadingPreview, setLoadingPreview] = useState(false);
|
||||||
|
const [preview, setPreview] =
|
||||||
|
useState<DeleteMyAccountPreviewResponse | null>(null);
|
||||||
|
const [passwordValue, setPasswordValue] = useState("");
|
||||||
|
|
||||||
|
const passwordForm = useForm<z.infer<typeof passwordSchema>>({
|
||||||
|
resolver: zodResolver(passwordSchema),
|
||||||
|
defaultValues: { password: "" }
|
||||||
|
});
|
||||||
|
|
||||||
|
const codeForm = useForm<z.infer<typeof codeSchema>>({
|
||||||
|
resolver: zodResolver(codeSchema),
|
||||||
|
defaultValues: { code: "" }
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (open && step === 0 && !preview) {
|
||||||
|
setLoadingPreview(true);
|
||||||
|
api.post<AxiosResponse<DeleteMyAccountPreviewResponse>>(
|
||||||
|
"/auth/delete-my-account",
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
.then((res) => {
|
||||||
|
if (res.data?.data?.preview) {
|
||||||
|
setPreview(res.data.data);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: t("deleteAccountError"),
|
||||||
|
description: formatAxiosError(
|
||||||
|
err,
|
||||||
|
t("deleteAccountError")
|
||||||
|
)
|
||||||
|
});
|
||||||
|
setOpen(false);
|
||||||
|
})
|
||||||
|
.finally(() => setLoadingPreview(false));
|
||||||
|
}
|
||||||
|
}, [open, step, preview, api, setOpen, t]);
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
setStep(0);
|
||||||
|
setPreview(null);
|
||||||
|
setPasswordValue("");
|
||||||
|
passwordForm.reset();
|
||||||
|
codeForm.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleContinueToPassword() {
|
||||||
|
setStep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handlePasswordSubmit(
|
||||||
|
values: z.infer<typeof passwordSchema>
|
||||||
|
) {
|
||||||
|
setLoading(true);
|
||||||
|
setPasswordValue(values.password);
|
||||||
|
try {
|
||||||
|
const res = await api.post<
|
||||||
|
| AxiosResponse<DeleteMyAccountCodeRequestedResponse>
|
||||||
|
| AxiosResponse<DeleteMyAccountSuccessResponse>
|
||||||
|
>("/auth/delete-my-account", { password: values.password });
|
||||||
|
|
||||||
|
const data = res.data?.data;
|
||||||
|
|
||||||
|
if (data && "codeRequested" in data && data.codeRequested) {
|
||||||
|
setStep(2);
|
||||||
|
} else if (data && "success" in data && data.success) {
|
||||||
|
toast({
|
||||||
|
title: t("deleteAccountSuccess"),
|
||||||
|
description: t("deleteAccountSuccessMessage")
|
||||||
|
});
|
||||||
|
setOpen(false);
|
||||||
|
reset();
|
||||||
|
router.push("/auth/login");
|
||||||
|
router.refresh();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: t("deleteAccountError"),
|
||||||
|
description: formatAxiosError(err, t("deleteAccountError"))
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleCodeSubmit(values: z.infer<typeof codeSchema>) {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
|
const res = await api.post<
|
||||||
|
AxiosResponse<DeleteMyAccountSuccessResponse>
|
||||||
|
>("/auth/delete-my-account", {
|
||||||
|
password: passwordValue,
|
||||||
|
code: values.code
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.data?.data?.success) {
|
||||||
|
toast({
|
||||||
|
title: t("deleteAccountSuccess"),
|
||||||
|
description: t("deleteAccountSuccessMessage")
|
||||||
|
});
|
||||||
|
setOpen(false);
|
||||||
|
reset();
|
||||||
|
router.push("/auth/login");
|
||||||
|
router.refresh();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
toast({
|
||||||
|
variant: "destructive",
|
||||||
|
title: t("deleteAccountError"),
|
||||||
|
description: formatAxiosError(err, t("deleteAccountError"))
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Credenza
|
||||||
|
open={open}
|
||||||
|
onOpenChange={(val) => {
|
||||||
|
setOpen(val);
|
||||||
|
if (!val) reset();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<CredenzaContent>
|
||||||
|
<CredenzaHeader>
|
||||||
|
<CredenzaTitle>
|
||||||
|
{t("deleteAccountConfirmTitle")}
|
||||||
|
</CredenzaTitle>
|
||||||
|
</CredenzaHeader>
|
||||||
|
<CredenzaBody>
|
||||||
|
<div className="space-y-4">
|
||||||
|
{step === 0 && (
|
||||||
|
<>
|
||||||
|
{loadingPreview ? (
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{t("loading")}...
|
||||||
|
</p>
|
||||||
|
) : preview ? (
|
||||||
|
<>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{t("deleteAccountConfirmMessage")}
|
||||||
|
</p>
|
||||||
|
<div className="rounded-md bg-muted p-3 space-y-2">
|
||||||
|
<p className="text-sm font-medium">
|
||||||
|
{t(
|
||||||
|
"deleteAccountPreviewAccount"
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
{preview.orgs.length > 0 && (
|
||||||
|
<>
|
||||||
|
<p className="text-sm font-medium mt-2">
|
||||||
|
{t(
|
||||||
|
"deleteAccountPreviewOrgs"
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
<ul className="list-disc list-inside text-sm text-muted-foreground space-y-1">
|
||||||
|
{preview.orgs.map(
|
||||||
|
(org) => (
|
||||||
|
<li
|
||||||
|
key={
|
||||||
|
org.orgId
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{org.name ||
|
||||||
|
org.orgId}
|
||||||
|
</li>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</ul>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<p className="text-sm font-bold text-destructive">
|
||||||
|
{t("cannotbeUndone")}
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{step === 1 && (
|
||||||
|
<Form {...passwordForm}>
|
||||||
|
<form
|
||||||
|
id="delete-account-password-form"
|
||||||
|
onSubmit={passwordForm.handleSubmit(
|
||||||
|
handlePasswordSubmit
|
||||||
|
)}
|
||||||
|
className="space-y-4"
|
||||||
|
>
|
||||||
|
<FormField
|
||||||
|
control={passwordForm.control}
|
||||||
|
name="password"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>
|
||||||
|
{t("password")}
|
||||||
|
</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
autoComplete="current-password"
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{step === 2 && (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="text-center">
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{t("otpAuthDescription")}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<Form {...codeForm}>
|
||||||
|
<form
|
||||||
|
id="delete-account-code-form"
|
||||||
|
onSubmit={codeForm.handleSubmit(
|
||||||
|
handleCodeSubmit
|
||||||
|
)}
|
||||||
|
className="space-y-4"
|
||||||
|
>
|
||||||
|
<FormField
|
||||||
|
control={codeForm.control}
|
||||||
|
name="code"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormControl>
|
||||||
|
<div className="flex justify-center">
|
||||||
|
<InputOTP
|
||||||
|
maxLength={6}
|
||||||
|
{...field}
|
||||||
|
pattern={
|
||||||
|
REGEXP_ONLY_DIGITS_AND_CHARS
|
||||||
|
}
|
||||||
|
onChange={(
|
||||||
|
value: string
|
||||||
|
) => {
|
||||||
|
field.onChange(
|
||||||
|
value
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<InputOTPGroup>
|
||||||
|
<InputOTPSlot
|
||||||
|
index={
|
||||||
|
0
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputOTPSlot
|
||||||
|
index={
|
||||||
|
1
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputOTPSlot
|
||||||
|
index={
|
||||||
|
2
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputOTPSlot
|
||||||
|
index={
|
||||||
|
3
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputOTPSlot
|
||||||
|
index={
|
||||||
|
4
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<InputOTPSlot
|
||||||
|
index={
|
||||||
|
5
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</InputOTPGroup>
|
||||||
|
</InputOTP>
|
||||||
|
</div>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</CredenzaBody>
|
||||||
|
<CredenzaFooter>
|
||||||
|
<CredenzaClose asChild>
|
||||||
|
<Button variant="outline">{t("close")}</Button>
|
||||||
|
</CredenzaClose>
|
||||||
|
{step === 0 && preview && !loadingPreview && (
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
onClick={handleContinueToPassword}
|
||||||
|
>
|
||||||
|
{t("continue")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{step === 1 && (
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
type="submit"
|
||||||
|
form="delete-account-password-form"
|
||||||
|
loading={loading}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
{t("deleteAccountButton")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{step === 2 && (
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
type="submit"
|
||||||
|
form="delete-account-code-form"
|
||||||
|
loading={loading}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
{t("deleteAccountButton")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</CredenzaFooter>
|
||||||
|
</CredenzaContent>
|
||||||
|
</Credenza>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -15,9 +15,11 @@ import { useEnvContext } from "@app/hooks/useEnvContext";
|
|||||||
import { toast } from "@app/hooks/useToast";
|
import { toast } from "@app/hooks/useToast";
|
||||||
import { formatAxiosError } from "@app/lib/api";
|
import { formatAxiosError } from "@app/lib/api";
|
||||||
import { getUserDisplayName } from "@app/lib/getUserDisplayName";
|
import { getUserDisplayName } from "@app/lib/getUserDisplayName";
|
||||||
import { Laptop, LogOut, Moon, Sun, Smartphone } from "lucide-react";
|
import { Laptop, LogOut, Moon, Sun, Smartphone, Trash2 } from "lucide-react";
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "next-themes";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { build } from "@server/build";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useUserContext } from "@app/hooks/useUserContext";
|
import { useUserContext } from "@app/hooks/useUserContext";
|
||||||
import Disable2FaForm from "./Disable2FaForm";
|
import Disable2FaForm from "./Disable2FaForm";
|
||||||
@@ -187,6 +189,20 @@ export default function ProfileIcon() {
|
|||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<LocaleSwitcher />
|
<LocaleSwitcher />
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
|
{user?.type === UserType.Internal && !user?.serverAdmin && (
|
||||||
|
<>
|
||||||
|
<DropdownMenuItem asChild>
|
||||||
|
<Link
|
||||||
|
href="/auth/delete-account"
|
||||||
|
className="flex cursor-pointer items-center"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 h-4 w-4" />
|
||||||
|
<span>{t("deleteAccount")}</span>
|
||||||
|
</Link>
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuSeparator />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<DropdownMenuItem onClick={() => logout()}>
|
<DropdownMenuItem onClick={() => logout()}>
|
||||||
{/* <LogOut className="mr-2 h-4 w-4" /> */}
|
{/* <LogOut className="mr-2 h-4 w-4" /> */}
|
||||||
<span>{t("logout")}</span>
|
<span>{t("logout")}</span>
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ export default function RedirectToOrg({ targetOrgId }: RedirectToOrgProps) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
try {
|
try {
|
||||||
const target = getInternalRedirectTarget(targetOrgId);
|
const target =
|
||||||
|
getInternalRedirectTarget(targetOrgId) ?? `/${targetOrgId}`;
|
||||||
router.replace(target);
|
router.replace(target);
|
||||||
} catch {
|
} catch {
|
||||||
router.replace(`/${targetOrgId}`);
|
router.replace(`/${targetOrgId}`);
|
||||||
|
|||||||
@@ -41,11 +41,12 @@ export function consumeInternalRedirectPath(): string | null {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the full redirect target for an org: either `/${orgId}` or
|
* Returns the full redirect target if a valid internal_redirect was stored
|
||||||
* `/${orgId}${path}` if a valid internal_redirect was stored. Consumes the
|
* (consumes the stored value). Returns null if none was stored or expired.
|
||||||
* stored value.
|
* Paths starting with /auth/ are returned as-is; others are prefixed with orgId.
|
||||||
*/
|
*/
|
||||||
export function getInternalRedirectTarget(orgId: string): string {
|
export function getInternalRedirectTarget(orgId: string): string | null {
|
||||||
const path = consumeInternalRedirectPath();
|
const path = consumeInternalRedirectPath();
|
||||||
return path ? `/${orgId}${path}` : `/${orgId}`;
|
if (!path) return null;
|
||||||
|
return path.startsWith("/auth/") ? path : `/${orgId}${path}`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user