cleanup session, change password self service

This commit is contained in:
peintnermax
2024-09-10 08:52:32 +02:00
parent 8c831921de
commit 5f1c86142f
8 changed files with 85 additions and 61 deletions

View File

@@ -2,6 +2,7 @@ import { getSessionCookieById } from "@/lib/cookies";
import {
getBrandingSettings,
getPasswordComplexitySettings,
getSession,
} from "@/lib/zitadel";
import Alert from "@/ui/Alert";
import ChangePasswordForm from "@/ui/ChangePasswordForm";
@@ -23,20 +24,22 @@ export default async function Page({
);
}
const session = await getSessionCookieById({
const sessionCookie = await getSessionCookieById({
sessionId,
});
const sessionFactors = await loadMostRecentSession({
loginName,
organization,
const { session } = await getSession({
sessionId: sessionCookie.id,
sessionToken: sessionCookie.token,
});
const passwordComplexitySettings = await getPasswordComplexitySettings(
session.organization,
session?.factors?.user?.organizationId,
);
const branding = await getBrandingSettings(session.organization);
const branding = await getBrandingSettings(
session?.factors?.user?.organizationId,
);
return (
<DynamicTheme branding={branding}>
@@ -44,7 +47,7 @@ export default async function Page({
<h1>Set Password</h1>
<p className="ztdl-p">Set the password for your account</p>
{(!sessionFactors || !loginName) && (
{!session && (
<div className="py-4">
<Alert>
Could not get the context of the user. Make sure to enter the
@@ -53,19 +56,19 @@ export default async function Page({
</div>
)}
{sessionFactors && (
{session && (
<UserAvatar
loginName={loginName ?? sessionFactors.factors?.user?.loginName}
displayName={sessionFactors.factors?.user?.displayName}
loginName={session.factors?.user?.loginName}
displayName={session.factors?.user?.displayName}
showDropdown
searchParams={searchParams}
></UserAvatar>
)}
{passwordComplexitySettings && (
{passwordComplexitySettings && session?.factors?.user?.id && (
<ChangePasswordForm
passwordComplexitySettings={passwordComplexitySettings}
userId={""}
userId={session.factors.user.id}
sessionId={sessionId}
></ChangePasswordForm>
)}

View File

@@ -46,7 +46,10 @@ export default async function Page({
async function loadSessionById(sessionId: string, organization?: string) {
const recent = await getSessionCookieById({ sessionId, organization });
return getSession(recent.id, recent.token).then((response) => {
return getSession({
sessionId: recent.id,
sessionToken: recent.token,
}).then((response) => {
if (response?.session && response.session.factors?.user?.id) {
return listAuthenticationMethodTypes(
response.session.factors.user.id,

View File

@@ -56,7 +56,10 @@ export default async function Page({
async function loadSessionById(sessionId: string, organization?: string) {
const recent = await getSessionCookieById({ sessionId, organization });
return getSession(recent.id, recent.token).then((response) => {
return getSession({
sessionId: recent.id,
sessionToken: recent.token,
}).then((response) => {
if (response?.session && response.session.factors?.user?.id) {
const userId = response.session.factors.user.id;
return listAuthenticationMethodTypes(userId).then((methods) => {

View File

@@ -24,7 +24,10 @@ export default async function Page({
async function loadSessionById(sessionId: string, organization?: string) {
const recent = await getSessionCookieById({ sessionId, organization });
return getSession(recent.id, recent.token).then((response) => {
return getSession({
sessionId: recent.id,
sessionToken: recent.token,
}).then((response) => {
if (response?.session) {
return response.session;
}

View File

@@ -29,11 +29,13 @@ async function loadSession(loginName: string, authRequestId?: string) {
return redirect(callbackUrl);
});
}
return getSession(recent.id, recent.token).then((response) => {
if (response?.session) {
return response.session;
}
});
return getSession({ sessionId: recent.id, sessionToken: recent.token }).then(
(response) => {
if (response?.session) {
return response.session;
}
},
);
}
export default async function Page({ searchParams }: { searchParams: any }) {

View File

@@ -21,7 +21,10 @@ export default async function Page({
async function loadSessionById(sessionId: string, organization?: string) {
const recent = await getSessionCookieById({ sessionId, organization });
return getSession(recent.id, recent.token).then((response) => {
return getSession({
sessionId: recent.id,
sessionToken: recent.token,
}).then((response) => {
if (response?.session) {
return response.session;
}

View File

@@ -188,7 +188,13 @@ export async function setSession(
);
}
export async function getSession(sessionId: string, sessionToken: string) {
export async function getSession({
sessionId,
sessionToken,
}: {
sessionId: string;
sessionToken: string;
}) {
return sessionService.getSession({ sessionId, sessionToken }, {});
}

View File

@@ -49,10 +49,10 @@ export async function createSessionAndUpdateCookie(
const createdSession = await createSessionFromChecks(checks, challenges);
if (createdSession) {
return getSession(
createdSession.sessionId,
createdSession.sessionToken,
).then((response) => {
return getSession({
sessionId: createdSession.sessionId,
sessionToken: createdSession.sessionToken,
}).then((response) => {
if (response?.session && response.session?.factors?.user?.loginName) {
const sessionCookie: CustomCookieData = {
id: createdSession.sessionId,
@@ -103,10 +103,10 @@ export async function createSessionForUserIdAndUpdateCookie(
const createdSession = await createSessionFromChecks(checks, challenges);
if (createdSession) {
return getSession(
createdSession.sessionId,
createdSession.sessionToken,
).then((response) => {
return getSession({
sessionId: createdSession.sessionId,
sessionToken: createdSession.sessionToken,
}).then((response) => {
if (response?.session && response.session?.factors?.user?.loginName) {
const sessionCookie: CustomCookieData = {
id: createdSession.sessionId,
@@ -159,10 +159,10 @@ export async function createSessionForIdpAndUpdateCookie(
);
if (createdSession) {
return getSession(
createdSession.sessionId,
createdSession.sessionToken,
).then((response) => {
return getSession({
sessionId: createdSession.sessionId,
sessionToken: createdSession.sessionToken,
}).then((response) => {
if (response?.session && response.session?.factors?.user?.loginName) {
const sessionCookie: CustomCookieData = {
id: createdSession.sessionId,
@@ -234,35 +234,36 @@ export async function setSessionAndUpdateCookie(
sessionCookie.authRequestId = authRequestId;
}
return getSession(sessionCookie.id, sessionCookie.token).then(
(response) => {
if (response?.session && response.session.factors?.user?.loginName) {
const { session } = response;
const newCookie: CustomCookieData = {
id: sessionCookie.id,
token: updatedSession.sessionToken,
creationDate: sessionCookie.creationDate,
expirationDate: sessionCookie.expirationDate,
// just overwrite the changeDate with the new one
changeDate: updatedSession.details?.changeDate
? `${timestampDate(updatedSession.details.changeDate).toDateString()}`
: "",
loginName: session.factors?.user?.loginName ?? "",
organization: session.factors?.user?.organizationId ?? "",
};
return getSession({
sessionId: sessionCookie.id,
sessionToken: sessionCookie.token,
}).then((response) => {
if (response?.session && response.session.factors?.user?.loginName) {
const { session } = response;
const newCookie: CustomCookieData = {
id: sessionCookie.id,
token: updatedSession.sessionToken,
creationDate: sessionCookie.creationDate,
expirationDate: sessionCookie.expirationDate,
// just overwrite the changeDate with the new one
changeDate: updatedSession.details?.changeDate
? `${timestampDate(updatedSession.details.changeDate).toDateString()}`
: "",
loginName: session.factors?.user?.loginName ?? "",
organization: session.factors?.user?.organizationId ?? "",
};
if (sessionCookie.authRequestId) {
newCookie.authRequestId = sessionCookie.authRequestId;
}
return updateSessionCookie(sessionCookie.id, newCookie).then(() => {
return { challenges: updatedSession.challenges, ...session };
});
} else {
throw "could not get session or session does not have loginName";
if (sessionCookie.authRequestId) {
newCookie.authRequestId = sessionCookie.authRequestId;
}
},
);
return updateSessionCookie(sessionCookie.id, newCookie).then(() => {
return { challenges: updatedSession.challenges, ...session };
});
} else {
throw "could not get session or session does not have loginName";
}
});
} else {
throw "Session not be set";
}