mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
Check the link before the code is redeemed and refuse a live session first, so a refused claim spends nothing at the provider.
103 lines
2.9 KiB
TypeScript
103 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* v1.42 (#959) — the anonymous side of a managed-profile handover: the
|
|
* preview of a link and the claim itself.
|
|
*
|
|
* The token travels in request BODIES only, never in a query key: a cache key
|
|
* is somewhere a credential must not be written, and the preview key is one
|
|
* fixed value because one claim page reads one link.
|
|
*/
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
|
import { apiPost } from "@/lib/api/api-fetch";
|
|
import type { HandoverAccess } from "@/lib/managed-profiles/handover-access";
|
|
import type { HandoverMethod } from "@/lib/validations/claim-sso";
|
|
import { queryKeys } from "@/lib/query-keys";
|
|
|
|
export interface ProfileClaimPreview {
|
|
displayName: string | null;
|
|
expiresAt: string;
|
|
guardians: {
|
|
displayName: string | null;
|
|
proposal: HandoverAccess;
|
|
}[];
|
|
/**
|
|
* v1.43 — the ways in this server offers. Absent from an older server,
|
|
* which only ever offered the password.
|
|
*/
|
|
methods?: HandoverMethod[];
|
|
/** v1.43 — the provider's name for the button, or null. */
|
|
ssoProviderName?: string | null;
|
|
}
|
|
|
|
export function useProfileClaimPreview(token: string | null, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: queryKeys.profileClaimPreview(),
|
|
queryFn: () =>
|
|
apiPost<ProfileClaimPreview>("/api/auth/claim/preview", { token }),
|
|
enabled: enabled && token !== null,
|
|
// A 404 is an answer, not a blip, and a retry would only spend the
|
|
// per-address budget the claim itself needs.
|
|
retry: false,
|
|
staleTime: Infinity,
|
|
gcTime: 0,
|
|
});
|
|
}
|
|
|
|
/** Exactly the body `POST /api/auth/claim` accepts. */
|
|
export interface ProfileClaimInput {
|
|
token: string;
|
|
username: string;
|
|
email: string;
|
|
password: string;
|
|
}
|
|
|
|
export function claimBody(input: ProfileClaimInput): ProfileClaimInput {
|
|
return {
|
|
token: input.token,
|
|
username: input.username,
|
|
email: input.email,
|
|
password: input.password,
|
|
};
|
|
}
|
|
|
|
export function useClaimProfile() {
|
|
return useMutation({
|
|
mutationKey: queryKeys.profileClaim(),
|
|
mutationFn: (input: ProfileClaimInput) =>
|
|
apiPost<{ userId: string; username: string }>(
|
|
"/api/auth/claim",
|
|
claimBody(input),
|
|
),
|
|
});
|
|
}
|
|
|
|
/** Exactly the body `POST /api/auth/claim/sso` accepts (strict). */
|
|
export interface ProfileClaimSsoInput {
|
|
token: string;
|
|
username: string;
|
|
}
|
|
|
|
export function claimSsoBody(
|
|
input: ProfileClaimSsoInput,
|
|
): ProfileClaimSsoInput {
|
|
return { token: input.token, username: input.username };
|
|
}
|
|
|
|
/**
|
|
* v1.43 (#959) — start claiming through the identity provider. The answer
|
|
* sets the sealed claim cookie and names where the browser goes next; the
|
|
* caller navigates there as a full page load.
|
|
*/
|
|
export function useClaimProfileSso() {
|
|
return useMutation({
|
|
mutationKey: queryKeys.profileClaimSso(),
|
|
mutationFn: (input: ProfileClaimSsoInput) =>
|
|
apiPost<{ redirectTo: string }>(
|
|
"/api/auth/claim/sso",
|
|
claimSsoBody(input),
|
|
),
|
|
});
|
|
}
|