dimah-surveyv0.5.0
Server

Authorization and identity

Guard fill and editor independently, then establish response ownership on the server.

dimah-survey has no account or role system. Your guard authorizes each request; the returned principal establishes response ownership.

Isolate the audiences

AudienceGuard contractAccess
FillReturn { respondentId } or { anonymous: true }A published survey and that caller's responses
EditorReturn nothing; throw to rejectAuthoring, publish, settings, and full response reads

A fill instance requires a principal. An editor guard returns nothing or throws. Keep the routes separate even when both use the same store.

getPublishedSurvey is on the fill handler. It does not stamp a respondent and does not require a response id. The editor draft is not on that route.

Only the editor may request include: "full", which returns answer data and each referenced survey once. Fill handles response writes, so submit hooks and validateResult belong there. Publish hooks belong to the editor.

Stamp an identified respondent

import type { SurveyPrincipal } from "@dimah-survey/core";

Prop

Type

guardRespondent(id) returns this principal. The server then:

  • stamps that id onto startResponse and listResponses
  • rejects a different id in the body or query
  • rejects include: "full"
  • rejects get, partial save, submit, abandon, and reopen when the stored respondentId differs
lib/survey.ts
import {
  APIError,
  SURVEY_ERROR_CODES,
  dimahSurvey,
  guardRespondent,
} from "@dimah-survey/server";

export const fill = dimahSurvey({
  audience: "fill",
  database,
  guard: async (context) => {
    const session = context.request
      ? await getSession(context.request)
      : null;
    if (!session) {
      throw APIError.from("FORBIDDEN", SURVEY_ERROR_CODES.FORBIDDEN);
    }
    return guardRespondent(session.user.id)(context);
  },
});

The browser never chooses ownership. A supplied respondentId must match the guard; omitting it still stamps the guarded id.

Identified start follows settings.responses. See Settings.


Treat anonymous ids as capabilities

import type { AnonymousPrincipal } from "@dimah-survey/core";

Prop

Type

guardAnonymous() returns this principal. Each start inserts a row. The response id itself becomes the capability for read and mutation, and the row must have a null respondentId.

lib/survey.ts
import { guardAnonymous, guardRespondent } from "@dimah-survey/server";

export const fill = dimahSurvey({
  audience: "fill",
  database,
  guard: (context) => {
    const userId = userIdFromSession(context.request);
    if (!userId) return guardAnonymous()(context);
    return guardRespondent(userId)(context);
  },
});

Store and transmit an anonymous response id like a secret. Anyone who has it can read and mutate that response.


Protect the editor route

lib/survey.ts
import { APIError, SURVEY_ERROR_CODES, dimahSurvey } from "@dimah-survey/server";

export const editor = dimahSurvey({
  audience: "editor",
  database,
  guard: async ({ request }) => {
    if (!request || !(await isEditor(request))) {
      throw APIError.from("FORBIDDEN", SURVEY_ERROR_CODES.FORBIDDEN);
    }
  },
});

Returning a principal from this guard is an error. Editor authorization either allows the request or throws.

include: "full" belongs on this route. It returns data and each referenced survey once.

On this page