tests, session using timestamp

This commit is contained in:
peintnermax
2024-10-23 14:28:33 +02:00
parent cde5f6cbd0
commit 52ce9219bb
21 changed files with 232 additions and 116 deletions

View File

@@ -7,7 +7,7 @@ import {
getSession,
setSession,
} from "@/lib/zitadel";
import { timestampDate } from "@zitadel/client";
import { timestampMs } from "@zitadel/client";
import {
Challenges,
RequestChallenges,
@@ -43,13 +43,13 @@ export async function createSessionAndUpdateCookie(
id: createdSession.sessionId,
token: createdSession.sessionToken,
creationDate: response.session.creationDate
? `${timestampDate(response.session.creationDate).toDateString()}`
? `${timestampMs(response.session.creationDate)}`
: "",
expirationDate: response.session.expirationDate
? `${timestampDate(response.session.expirationDate).toDateString()}`
? `${timestampMs(response.session.expirationDate)}`
: "",
changeDate: response.session.changeDate
? `${timestampDate(response.session.changeDate).toDateString()}`
? `${timestampMs(response.session.changeDate)}`
: "",
loginName: response.session.factors.user.loginName ?? "",
};
@@ -98,13 +98,13 @@ export async function createSessionForIdpAndUpdateCookie(
id: createdSession.sessionId,
token: createdSession.sessionToken,
creationDate: response.session.creationDate
? `${timestampDate(response.session.creationDate).toDateString()}`
? `${timestampMs(response.session.creationDate)}`
: "",
expirationDate: response.session.expirationDate
? `${timestampDate(response.session.expirationDate).toDateString()}`
? `${timestampMs(response.session.expirationDate)}`
: "",
changeDate: response.session.changeDate
? `${timestampDate(response.session.changeDate).toDateString()}`
? `${timestampMs(response.session.changeDate)}`
: "",
loginName: response.session.factors.user.loginName ?? "",
organization: response.session.factors.user.organizationId ?? "",
@@ -155,7 +155,7 @@ export async function setSessionAndUpdateCookie(
expirationDate: recentCookie.expirationDate,
// just overwrite the changeDate with the new one
changeDate: updatedSession.details?.changeDate
? `${timestampDate(updatedSession.details.changeDate).toDateString()}`
? `${timestampMs(updatedSession.details.changeDate)}`
: "",
loginName: recentCookie.loginName,
organization: recentCookie.organization,
@@ -178,7 +178,7 @@ export async function setSessionAndUpdateCookie(
expirationDate: sessionCookie.expirationDate,
// just overwrite the changeDate with the new one
changeDate: updatedSession.details?.changeDate
? `${timestampDate(updatedSession.details.changeDate).toDateString()}`
? `${timestampMs(updatedSession.details.changeDate)}`
: "",
loginName: session.factors?.user?.loginName ?? "",
organization: session.factors?.user?.organizationId ?? "",

View File

@@ -139,6 +139,7 @@ export async function sendLoginname(command: SendLoginnameCommand) {
if (users.result[0].state === UserState.INITIAL) {
const params = new URLSearchParams({
loginName: session.factors?.user?.loginName,
initial: "true", // this does not require a code to be set
});
if (command.organization || session.factors?.user?.organizationId) {

View File

@@ -284,5 +284,5 @@ export async function changePassword(command: {
}
const userId = user.userId;
return setPassword(userId, command.password, command.code);
return setPassword(userId, command.password, user, command.code);
}

View File

@@ -37,7 +37,11 @@ import {
SearchQuery,
SearchQuerySchema,
} from "@zitadel/proto/zitadel/user/v2/query_pb";
import { SendInviteCodeSchema } from "@zitadel/proto/zitadel/user/v2/user_pb";
import {
SendInviteCodeSchema,
User,
UserState,
} from "@zitadel/proto/zitadel/user/v2/user_pb";
import { unstable_cache } from "next/cache";
import { PROVIDER_MAPPING } from "./idp";
@@ -327,7 +331,7 @@ export async function createInviteCode(userId: string, host: string | null) {
if (host) {
medium = {
...medium,
urlTemplate: `https://${host}/verify?code={{.Code}}&userId={{.UserID}}&organization={{.OrgID}}&invite=true`,
urlTemplate: `${host.includes("localhost") ? "http://" : "https://"}${host}/verify?code={{.Code}}&userId={{.UserID}}&organization={{.OrgID}}&invite=true`,
};
}
@@ -564,7 +568,7 @@ export async function passwordReset(userId: string, host: string | null) {
if (host) {
medium = {
...medium,
urlTemplate: `https://${host}/password/set?code={{.Code}}&userId={{.UserID}}&organization={{.OrgID}}`,
urlTemplate: `${host.includes("localhost") ? "http://" : "https://"}${host}/password/set?code={{.Code}}&userId={{.UserID}}&organization={{.OrgID}}`,
};
}
@@ -590,6 +594,7 @@ export async function passwordReset(userId: string, host: string | null) {
export async function setPassword(
userId: string,
password: string,
user: User,
code?: string,
) {
let payload = create(SetPasswordRequestSchema, {
@@ -605,9 +610,8 @@ export async function setPassword(
// if the user has no authmethods set, we can set a password otherwise we need a code
if (
!authmethods ||
!authmethods.authMethodTypes ||
authmethods.authMethodTypes.length === 0
!(authmethods.authMethodTypes.length === 0) &&
user.state !== UserState.INITIAL
) {
return { error: "Provide a code to set a password" };
}
@@ -623,7 +627,14 @@ export async function setPassword(
};
}
return userService.setPassword(payload, {});
return userService.setPassword(payload, {}).catch((error) => {
// throw error if failed precondition (ex. User is not yet initialized)
if (error.code === 9 && error.message) {
return { error: error.message };
} else {
throw error;
}
});
}
/**