Files
zitadel/apps/login/utils/session.ts

102 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-06-29 19:06:30 +02:00
import { createSession, getSession, server, setSession } from "#/lib/zitadel";
import { NextResponse } from "next/server";
import {
SessionCookie,
addSessionToCookie,
updateSessionCookie,
} from "./cookies";
import { ChallengeKind, Session } from "@zitadel/server";
export async function createSessionAndUpdateCookie(
loginName: string,
password: string | undefined,
domain: string,
challenges: ChallengeKind[] | undefined
): Promise<Session> {
const createdSession = await createSession(
server,
loginName,
domain,
password,
challenges
);
if (createdSession) {
return getSession(
server,
createdSession.sessionId,
createdSession.sessionToken
).then((response) => {
if (response?.session && response.session?.factors?.user?.loginName) {
const sessionCookie: SessionCookie = {
id: createdSession.sessionId,
token: createdSession.sessionToken,
changeDate: response.session.changeDate?.toString() ?? "",
loginName: response.session?.factors?.user?.loginName ?? "",
};
return addSessionToCookie(sessionCookie).then(() => {
return response.session as Session;
// {
// sessionId: createdSession.sessionId,
// factors: response?.session?.factors,
// });
});
} else {
throw "could not get session or session does not have loginName";
}
});
} else {
throw "Could not create session";
}
}
export async function setSessionAndUpdateCookie(
sessionId: string,
sessionToken: string,
loginName: string,
password: string | undefined,
domain: string | undefined,
challenges: ChallengeKind[] | undefined
): Promise<Session> {
return setSession(
server,
sessionId,
sessionToken,
domain,
password,
challenges
2023-06-30 15:32:41 +02:00
).then((updatedSession) => {
if (updatedSession) {
2023-06-29 19:06:30 +02:00
const sessionCookie: SessionCookie = {
id: sessionId,
2023-06-30 15:32:41 +02:00
token: updatedSession.sessionToken,
changeDate: updatedSession.details?.changeDate?.toString() ?? "",
2023-06-29 19:06:30 +02:00
loginName: loginName,
};
return getSession(server, sessionCookie.id, sessionCookie.token).then(
(response) => {
if (response?.session && response.session.factors?.user?.loginName) {
const { session } = response;
const newCookie: SessionCookie = {
id: sessionCookie.id,
2023-06-30 15:32:41 +02:00
token: updatedSession.sessionToken,
2023-06-29 19:06:30 +02:00
changeDate: session.changeDate?.toString() ?? "",
loginName: session.factors?.user?.loginName ?? "",
};
2023-06-30 14:13:03 +02:00
return updateSessionCookie(sessionCookie.id, newCookie).then(() => {
2023-06-30 15:32:41 +02:00
return { challenges: updatedSession.challenges, ...session };
2023-06-30 14:13:03 +02:00
});
2023-06-29 19:06:30 +02:00
} else {
throw "could not get session or session does not have loginName";
}
}
);
} else {
throw "Session not be set";
}
});
}