error handler

This commit is contained in:
Max Peintner
2025-01-27 16:23:46 +01:00
parent 63656e16fb
commit b9d4ca824f
3 changed files with 33 additions and 14 deletions

View File

@@ -7,9 +7,10 @@ import {
getSession,
setSession,
} from "@/lib/zitadel";
import { Duration, timestampMs } from "@zitadel/client";
import { ConnectError, Duration, timestampMs } from "@zitadel/client";
import {
CredentialsCheckError,
CredentialsCheckErrorSchema,
ErrorDetail,
} from "@zitadel/proto/zitadel/message_pb";
import {
@@ -30,13 +31,29 @@ type CustomCookieData = {
authRequestId?: string; // if its linked to an OIDC flow
};
const passwordAttemptsHandler = (error: ConnectError) => {
const details = error.findDetails(CredentialsCheckErrorSchema);
if (details[0] && "failedAttempts" in details[0]) {
const failedAttempts = details[0].failedAttempts;
throw {
error: `Failed to authenticate: You had ${failedAttempts} password attempts.`,
failedAttempts: failedAttempts,
};
}
throw error;
};
export async function createSessionAndUpdateCookie(
checks: Checks,
challenges: RequestChallenges | undefined,
authRequestId: string | undefined,
lifetime?: Duration,
): Promise<Session> {
const createdSession = await createSessionFromChecks(checks, challenges);
const createdSession = await createSessionFromChecks(
checks,
challenges,
).catch(passwordAttemptsHandler);
if (createdSession) {
return getSession({
@@ -215,14 +232,5 @@ export async function setSessionAndUpdateCookie(
throw "Session not be set";
}
})
.catch((error: ErrorDetail | CredentialsCheckError) => {
console.error("Could not set session", error);
if ("failedAttempts" in error && error.failedAttempts) {
throw {
error: `Failed to authenticate: You had ${error.failedAttempts} password attempts.`,
failedAttempts: error.failedAttempts,
};
}
throw error;
});
.catch(passwordAttemptsHandler);
}

View File

@@ -113,7 +113,12 @@ export async function sendPassword(command: UpdateSessionCommand) {
);
return {
error: `Failed to authenticate: You had ${error.failedAttempts} of ${lockoutSettings?.maxPasswordAttempts} password attempts.`,
error:
`Failed to authenticate. You had ${error.failedAttempts} of ${lockoutSettings?.maxPasswordAttempts} password attempts.` +
(lockoutSettings?.maxPasswordAttempts &&
error.failedAttempts >= lockoutSettings?.maxPasswordAttempts
? "Contact your administrator to unlock your account"
: ""),
};
}
return { error: "Could not create session for user" };
@@ -136,7 +141,12 @@ export async function sendPassword(command: UpdateSessionCommand) {
const lockoutSettings = await getLockoutSettings(command.organization);
return {
error: `Failed to authenticate: You had ${error.failedAttempts} of ${lockoutSettings?.maxPasswordAttempts} password attempts.`,
error:
`Failed to authenticate. You had ${error.failedAttempts} of ${lockoutSettings?.maxPasswordAttempts} password attempts.` +
(lockoutSettings?.maxPasswordAttempts &&
error.failedAttempts >= lockoutSettings?.maxPasswordAttempts
? " Contact your administrator to unlock your account"
: ""),
};
}
throw error;

View File

@@ -6,3 +6,4 @@ export { create, fromJson, toJson } from "@bufbuild/protobuf";
export type { JsonObject } from "@bufbuild/protobuf";
export { TimestampSchema, timestampDate, timestampFromDate, timestampFromMs, timestampMs } from "@bufbuild/protobuf/wkt";
export type { Duration, Timestamp } from "@bufbuild/protobuf/wkt";
export type { Code, ConnectError } from "@connectrpc/connect";