All files / src/lib/patient session-username.ts

100% Statements 4/4
85.71% Branches 6/7
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                                36x               36x 35x                
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
 
/**
 * The signed-in patient's Keycloak login name, or null.
 *
 * AuthSession does not carry `preferredUsername`, so the routes that need the
 * login (to match a pairing or a connected phone to it) read the full session,
 * as /api/patient/ehr-sync does. The session callback in `lib/auth.ts` puts it
 * on `session.user`, for a Keycloak and an EUDI sign-in alike; an EUDI session
 * has no mail and its name is the record's pseudonym, so without that the
 * login of a wallet sign-in was lost (ADR-067). A session without it falls
 * back to the mail's local part (`patient1@...`), and only then to the name,
 * which is usually a display name and would match no Keycloak login.
 */
export async function sessionUsername(): Promise<string | null> {
  const session = (await getServerSession(authOptions)) as {
    preferredUsername?: string | null;
    user?: {
      name?: string | null;
      email?: string | null;
      preferredUsername?: string | null;
    } | null;
  } | null;
  if (!session) return null;
  return (
    session.preferredUsername ||
    session.user?.preferredUsername ||
    session.user?.email?.split("@")[0] ||
    session.user?.name ||
    null
  );
}