cleanup typing

This commit is contained in:
peintnermax
2024-08-30 09:52:42 +02:00
parent 03230de33e
commit 27fb7f7b27
6 changed files with 24 additions and 23 deletions

View File

@@ -2,13 +2,14 @@
import { startIdentityProviderFlow } from "@/lib/zitadel";
export type StartIDPFlowOptions = {
export type StartIDPFlowCommand = {
idpId: string;
successUrl: string;
failureUrl: string;
};
export async function startIDPFlow(options: StartIDPFlowOptions) {
const { idpId, successUrl, failureUrl } = options;
export async function startIDPFlow(command: StartIDPFlowCommand) {
const { idpId, successUrl, failureUrl } = command;
return startIdentityProviderFlow({
idpId,

View File

@@ -12,13 +12,15 @@ import {
import { createSessionForUserIdAndUpdateCookie } from "../../utils/session";
import { redirect } from "next/navigation";
export type SendLoginnameOptions = {
export type SendLoginnameCommand = {
loginName: string;
authRequestId?: string;
organization?: string;
};
export async function sendLoginname(options: SendLoginnameOptions) {
export const UserNotFound = Error("Could not find user");
export async function sendLoginname(options: SendLoginnameCommand) {
const { loginName, authRequestId, organization } = options;
const users = await listUsers({
userName: loginName,
@@ -34,8 +36,8 @@ export async function sendLoginname(options: SendLoginnameOptions) {
authRequestId,
);
if (!session?.factors?.user?.id) {
throw "No user id found in session";
if (!session.factors?.user?.id) {
throw Error("Could not create session for user");
}
const methods = await listAuthenticationMethodTypes(
@@ -92,7 +94,7 @@ export async function sendLoginname(options: SendLoginnameOptions) {
}
});
} else {
throw "Could not find user";
throw UserNotFound;
}
} else if (
loginSettings?.allowRegister &&
@@ -114,5 +116,5 @@ export async function sendLoginname(options: SendLoginnameOptions) {
return redirect(registerUrl.toString());
}
throw "Could not find user";
throw UserNotFound;
}

View File

@@ -21,7 +21,7 @@ import { headers } from "next/headers";
import { Checks } from "@zitadel/proto/zitadel/session/v2/session_service_pb";
import { RequestChallenges } from "@zitadel/proto/zitadel/session/v2/challenge_pb";
type CreateNewSessionOptinos = {
type CreateNewSessionCommand = {
userId: string;
idpIntent: {
idpIntentId: string;
@@ -33,7 +33,7 @@ type CreateNewSessionOptinos = {
authRequestId: string;
};
export async function createNewSession(options: CreateNewSessionOptinos) {
export async function createNewSession(options: CreateNewSessionCommand) {
const {
userId,
idpIntent,
@@ -61,7 +61,7 @@ export async function createNewSession(options: CreateNewSessionOptinos) {
}
}
export type UpdateSessionOptions = {
export type UpdateSessionCommand = {
loginName?: string;
sessionId?: string;
organization?: string;
@@ -70,7 +70,7 @@ export type UpdateSessionOptions = {
challenges?: RequestChallenges;
};
export async function updateSession(options: UpdateSessionOptions) {
export async function updateSession(options: UpdateSessionCommand) {
const {
loginName,
sessionId,

View File

@@ -58,8 +58,8 @@ export default function PasswordForm({
password: { password: values.password },
} as Checks,
authRequestId,
}).catch((error) => {
setError(error ?? "Could not verify password");
}).catch((error: Error) => {
setError(error.message ?? "Could not verify password");
});
setLoading(false);

View File

@@ -51,8 +51,8 @@ export function SignInWithIDP({
`${host}/idp/${provider}/success?` + new URLSearchParams(params),
failureUrl:
`${host}/idp/${provider}/failure?` + new URLSearchParams(params),
}).catch((err) => {
setError(response.details);
}).catch((error: Error) => {
setError(error.message ?? "Could not start IDP flow");
});
setLoading(false);

View File

@@ -12,7 +12,7 @@ import {
PasskeysType,
} from "@zitadel/proto/zitadel/settings/v2/login_settings_pb";
import BackButton from "./BackButton";
import { sendLoginname, SendLoginnameOptions } from "@/lib/server/loginname";
import { sendLoginname } from "@/lib/server/loginname";
import { AuthenticationMethodType } from "@zitadel/proto/zitadel/user/v2/user_service_pb";
type Inputs = {
@@ -53,14 +53,12 @@ export default function UsernameForm({
async function submitLoginName(values: Inputs, organization?: string) {
setLoading(true);
const options: SendLoginnameOptions = {
const res = await sendLoginname({
loginName: values.loginName,
organization,
authRequestId,
};
const res = await sendLoginname(options).catch((error) => {
setError(error ?? "An internal error occurred");
}).catch((error: Error) => {
setError(error.message ?? "An internal error occurred");
return Promise.reject(error ?? "An internal error occurred");
});