cleanup getmostrecentsession

This commit is contained in:
peintnermax
2024-08-13 14:14:10 +02:00
parent c7f3ae4373
commit c85975fef0
7 changed files with 41 additions and 109 deletions

View File

@@ -2,16 +2,14 @@ import {
getBrandingSettings,
getSession,
listAuthenticationMethodTypes,
sessionService,
} from "@/lib/zitadel";
import Alert from "@/ui/Alert";
import BackButton from "@/ui/BackButton";
import ChooseSecondFactor from "@/ui/ChooseSecondFactor";
import DynamicTheme from "@/ui/DynamicTheme";
import UserAvatar from "@/ui/UserAvatar";
import {
getMostRecentCookieWithLoginname,
getSessionCookieById,
} from "@zitadel/next";
import { getSessionCookieById, loadMostRecentSession } from "@zitadel/next";
export default async function Page({
searchParams,
@@ -29,20 +27,19 @@ export default async function Page({
loginName?: string,
organization?: string,
) {
const recent = await getMostRecentCookieWithLoginname({
return loadMostRecentSession(sessionService, {
loginName,
organization,
});
return getSession(recent.id, recent.token).then((response) => {
if (response?.session && response.session.factors?.user?.id) {
return listAuthenticationMethodTypes(
response.session.factors.user.id,
).then((methods) => {
return {
factors: response.session?.factors,
authMethods: methods.authMethodTypes ?? [],
};
});
}).then((session) => {
if (session && session.factors?.user?.id) {
return listAuthenticationMethodTypes(session.factors.user.id).then(
(methods) => {
return {
factors: session?.factors,
authMethods: methods.authMethodTypes ?? [],
};
},
);
}
});
}

View File

@@ -4,16 +4,14 @@ import {
getSession,
getUserByID,
listAuthenticationMethodTypes,
sessionService,
} from "@/lib/zitadel";
import Alert from "@/ui/Alert";
import BackButton from "@/ui/BackButton";
import ChooseSecondFactorToSetup from "@/ui/ChooseSecondFactorToSetup";
import DynamicTheme from "@/ui/DynamicTheme";
import UserAvatar from "@/ui/UserAvatar";
import {
getMostRecentCookieWithLoginname,
getSessionCookieById,
} from "@zitadel/next";
import { getSessionCookieById, loadMostRecentSession } from "@zitadel/next";
export default async function Page({
searchParams,
@@ -31,13 +29,12 @@ export default async function Page({
loginName?: string,
organization?: string,
) {
const recent = await getMostRecentCookieWithLoginname({
return loadMostRecentSession(sessionService, {
loginName,
organization,
});
return getSession(recent.id, recent.token).then((response) => {
if (response?.session && response.session.factors?.user?.id) {
const userId = response.session.factors.user.id;
}).then((session) => {
if (session && session.factors?.user?.id) {
const userId = session.factors.user.id;
return listAuthenticationMethodTypes(userId).then((methods) => {
return getUserByID(userId).then((user) => {
const humanUser =
@@ -46,7 +43,7 @@ export default async function Page({
: undefined;
return {
factors: response.session?.factors,
factors: session?.factors,
authMethods: methods.authMethodTypes ?? [],
phoneVerified: humanUser?.phone?.isVerified ?? false,
emailVerified: humanUser?.email?.isVerified ?? false,

View File

@@ -1,12 +1,9 @@
import { getBrandingSettings, getSession } from "@/lib/zitadel";
import { getBrandingSettings, getSession, sessionService } from "@/lib/zitadel";
import Alert from "@/ui/Alert";
import DynamicTheme from "@/ui/DynamicTheme";
import LoginPasskey from "@/ui/LoginPasskey";
import UserAvatar from "@/ui/UserAvatar";
import {
getMostRecentCookieWithLoginname,
getSessionCookieById,
} from "@zitadel/next";
import { getSessionCookieById, loadMostRecentSession } from "@zitadel/next";
const title = "Authenticate with a passkey";
const description =
@@ -22,22 +19,7 @@ export default async function Page({
const sessionFactors = sessionId
? await loadSessionById(sessionId, organization)
: await loadSessionByLoginname(loginName, organization);
async function loadSessionByLoginname(
loginName?: string,
organization?: string,
) {
const recent = await getMostRecentCookieWithLoginname({
loginName,
organization,
});
return getSession(recent.id, recent.token).then((response) => {
if (response?.session) {
return response.session;
}
});
}
: await loadMostRecentSession(sessionService, { loginName, organization });
async function loadSessionById(sessionId: string, organization?: string) {
const recent = await getSessionCookieById({ sessionId, organization });

View File

@@ -1,16 +1,9 @@
import {
getBrandingSettings,
getLoginSettings,
getSession,
} from "@/lib/zitadel";
import { getBrandingSettings, getSession, sessionService } from "@/lib/zitadel";
import Alert from "@/ui/Alert";
import DynamicTheme from "@/ui/DynamicTheme";
import LoginPasskey from "@/ui/LoginPasskey";
import UserAvatar from "@/ui/UserAvatar";
import {
getMostRecentCookieWithLoginname,
getSessionCookieById,
} from "@zitadel/next";
import { getSessionCookieById, loadMostRecentSession } from "@zitadel/next";
export default async function Page({
searchParams,
@@ -25,22 +18,7 @@ export default async function Page({
const sessionFactors = sessionId
? await loadSessionById(sessionId, organization)
: await loadSessionByLoginname(loginName, organization);
async function loadSessionByLoginname(
loginName?: string,
organization?: string,
) {
const recent = await getMostRecentCookieWithLoginname({
loginName,
organization,
});
return getSession(recent.id, recent.token).then((response) => {
if (response?.session) {
return response.session;
}
});
}
: await loadMostRecentSession(sessionService, { loginName, organization });
async function loadSessionById(sessionId: string, organization?: string) {
const recent = await getSessionCookieById({ sessionId, organization });

View File

@@ -1,10 +1,8 @@
import { getBrandingSettings, getSession, sessionService } from "@/lib/zitadel";
import Alert, { AlertType } from "@/ui/Alert";
import { getBrandingSettings, sessionService } from "@/lib/zitadel";
import Alert from "@/ui/Alert";
import DynamicTheme from "@/ui/DynamicTheme";
import RegisterPasskey from "@/ui/RegisterPasskey";
import RegisterU2F from "@/ui/RegisterU2F";
import UserAvatar from "@/ui/UserAvatar";
import { getMostRecentCookieWithLoginname } from "@zitadel/next";
import { loadMostRecentSession } from "@zitadel/next";
export default async function Page({

View File

@@ -1,24 +1,21 @@
"use server";
import { getMostRecentCookieWithLoginname } from "@zitadel/next";
import { getSession, verifyTOTPRegistration } from "./zitadel";
import { loadMostRecentSession } from "@zitadel/next";
import { sessionService, verifyTOTPRegistration } from "./zitadel";
export async function verifyTOTP(
code: string,
loginName?: string,
organization?: string,
) {
return getMostRecentCookieWithLoginname({ loginName, organization })
.then((recent) => {
return getSession(recent.id, recent.token).then((response) => {
return { session: response?.session, token: recent.token };
});
})
.then(({ session, token }) => {
if (session?.factors?.user?.id) {
return verifyTOTPRegistration(code, session.factors.user.id, token);
} else {
throw Error("No user id found in session.");
}
});
return loadMostRecentSession(sessionService, {
loginName,
organization,
}).then((session) => {
if (session?.factors?.user?.id) {
return verifyTOTPRegistration(code, session.factors.user.id);
} else {
throw Error("No user id found in session.");
}
});
}

View File

@@ -225,24 +225,7 @@ export async function addHumanUser({
});
}
export async function verifyTOTPRegistration(
code: string,
userId: string,
token?: string,
) {
// let userService;
// if (token) {
// const authConfig: ZitadelServerOptions = {
// name: "zitadel login",
// apiUrl: process.env.ZITADEL_API_URL ?? "",
// token: token,
// };
//
// const sessionUser = initializeServer(authConfig);
// userService = user.getUser(sessionUser);
// } else {
// userService = user.getUser(server);
// }
export async function verifyTOTPRegistration(code: string, userId: string) {
return userService.verifyTOTPRegistration({ code, userId }, {});
}