mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 18:02:33 +00:00
cleanup session, change password self service
This commit is contained in:
@@ -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>
|
||||
)}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 }) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 }, {});
|
||||
}
|
||||
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user