Skip to content

Commit

Permalink
refactor(loader): Unify session updates in one function
Browse files Browse the repository at this point in the history
  • Loading branch information
frederike-ramin committed Feb 28, 2024
1 parent bf1294a commit a3a49ae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
15 changes: 6 additions & 9 deletions app/routes/shared/formular.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
} from "~/services/session.server/arrayDeletion";
import { interpolateDeep } from "~/util/fillTemplate";
import { stepMeta } from "~/services/meta/formStepMeta";
import { updateSessionInHeader } from "~/services/session.server/updateSessionInHeader";

const structureCmsContent = (
formPageContent: z.infer<CollectionSchemas["form-flow-pages"]>,
Expand Down Expand Up @@ -170,16 +171,12 @@ export const loader = async ({
}),
);

const { session, csrf } = await updateSessionWithCsrfToken(request);

// update session with last valid step
session.set(lastStepKey, { [flowId]: stepId });

// set session in header
const sessionContext = getSessionForContext("main");
const headers = { "Set-Cookie": await sessionContext.commitSession(session) };
const { headers, csrf } = await updateSessionInHeader({
request,
flowId,
stepId,
});

// get navigation destinations + labels
const buttonNavigationProps = getButtonNavigationProps({
flowController,
stepId,
Expand Down
14 changes: 6 additions & 8 deletions app/routes/shared/vorabcheck.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { isStrapiHeadingComponent } from "~/services/cms/models/StrapiHeading";
import { getButtonNavigationProps } from "~/util/buttonProps";
import { stepMeta } from "~/services/meta/formStepMeta";
import { getProgressProps } from "~/services/flow/server/progress";
import { updateSessionInHeader } from "~/services/session.server/updateSessionInHeader";

export const loader = async ({
params,
Expand Down Expand Up @@ -98,14 +99,11 @@ export const loader = async ({
const fieldNames = formElements.map((entry) => entry.name);
const stepData = _.pick(userDataFromRedis, fieldNames);

const { session, csrf } = await updateSessionWithCsrfToken(request);

// update session with last valid step
session.set(lastStepKey, { [flowId]: stepId });

// set session in header
const sessionContext = getSessionForContext("main");
const headers = { "Set-Cookie": await sessionContext.commitSession(session) };
const { headers, csrf } = await updateSessionInHeader({
request,
flowId,
stepId,
});

const buttonNavigationProps = getButtonNavigationProps({
flowController,
Expand Down
24 changes: 24 additions & 0 deletions app/services/session.server/updateSessionInHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { updateSessionWithCsrfToken } from "~/services/security/csrf.server";
import { lastStepKey } from "~/services/flow/constants";
import { getSessionForContext } from "~/services/session.server/index";
import type { FlowId } from "~/models/flows/contexts";

export const updateSessionInHeader = async ({
request,
flowId,
stepId,
}: {
request: Request;
flowId: FlowId;
stepId: string;
}) => {
const { session, csrf } = await updateSessionWithCsrfToken(request);

// update session with last valid step
session.set(lastStepKey, { [flowId]: stepId });

const sessionContext = getSessionForContext("main");
const headers = { "Set-Cookie": await sessionContext.commitSession(session) };

return { headers, csrf };
};

0 comments on commit a3a49ae

Please sign in to comment.