updateSession error handling

This commit is contained in:
Max Peintner
2024-12-04 15:49:49 +01:00
parent cf07c70bc9
commit 041f2bcef7
5 changed files with 49 additions and 14 deletions

View File

@@ -33,6 +33,8 @@ export default async function Page(props: {
const host = (await headers()).get("host"); const host = (await headers()).get("host");
console.log("host", host);
return ( return (
<DynamicTheme branding={branding}> <DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4"> <div className="flex flex-col items-center space-y-4">

View File

@@ -76,6 +76,13 @@ export function LoginOTP({
async function updateSessionForOTPChallenge() { async function updateSessionForOTPChallenge() {
let challenges; let challenges;
if (host) {
console.log(
`${host.includes("localhost") ? "http://" : "https://"}${host}/otp/method=${method}?code={{.Code}}&userId={{.UserID}}&sessionId={{.SessionID}}&organization={{.OrgID}}` +
(authRequestId ? `&authRequestId=${authRequestId}` : ""),
);
}
if (method === "email") { if (method === "email") {
challenges = create(RequestChallengesSchema, { challenges = create(RequestChallengesSchema, {
otpEmail: { otpEmail: {
@@ -107,14 +114,19 @@ export function LoginOTP({
challenges, challenges,
authRequestId, authRequestId,
}) })
.catch((error) => { .catch(() => {
setError(error.message ?? "Could not request OTP challenge"); setError("Could not request OTP challenge");
return; return;
}) })
.finally(() => { .finally(() => {
setLoading(false); setLoading(false);
}); });
if (response && "error" in response && response.error) {
setError(response.error);
return;
}
return response; return response;
} }
@@ -167,6 +179,11 @@ export function LoginOTP({
setLoading(false); setLoading(false);
}); });
if (response && "error" in response && response.error) {
setError(response.error);
return;
}
return response; return response;
} }

View File

@@ -110,6 +110,11 @@ export function LoginPasskey({
setLoading(false); setLoading(false);
}); });
if (session && "error" in session && session.error) {
setError(session.error);
return;
}
return session; return session;
} }
@@ -132,6 +137,11 @@ export function LoginPasskey({
setLoading(false); setLoading(false);
}); });
if (response && "error" in response && response.error) {
setError(response.error);
return;
}
return response; return response;
} }

View File

@@ -142,7 +142,7 @@ export async function removeSessionFromCookie<T>(
} }
} }
export async function getMostRecentSessionCookie<T>(): Promise<any> { export async function getMostRecentSessionCookie<T>(): Promise<Cookie> {
const cookiesList = await cookies(); const cookiesList = await cookies();
const stringifiedCookie = cookiesList.get("sessions"); const stringifiedCookie = cookiesList.get("sessions");

View File

@@ -132,21 +132,23 @@ export async function updateSession(options: UpdateSessionCommand) {
challenges, challenges,
} = options; } = options;
const recentSession = sessionId const recentSession = sessionId
? await getSessionCookieById({ sessionId }).catch((error) => { ? await getSessionCookieById({ sessionId })
return Promise.reject(error);
})
: loginName : loginName
? await getSessionCookieByLoginName({ loginName, organization }).catch( ? await getSessionCookieByLoginName({ loginName, organization })
(error) => { : await getMostRecentSessionCookie();
return Promise.reject(error);
}, if (!recentSession) {
) return {
: await getMostRecentSessionCookie().catch((error) => { error: "Could not find session",
return Promise.reject(error); };
}); }
const host = (await headers()).get("host"); const host = (await headers()).get("host");
if (!host) {
return { error: "Could not get host" };
}
if ( if (
host && host &&
challenges && challenges &&
@@ -174,6 +176,10 @@ export async function updateSession(options: UpdateSessionCommand) {
lifetime, lifetime,
); );
if (!session) {
return { error: "Could not update session" };
}
// if password, check if user has MFA methods // if password, check if user has MFA methods
let authMethods; let authMethods;
if (checks && checks.password && session.factors?.user?.id) { if (checks && checks.password && session.factors?.user?.id) {