Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"input-otp": "^1.4.2",
"lucide-react": "^1.23.0",
"next-themes": "^0.4.6",
"qrcode.react": "^4.2.0",
"react": "^19.2.7",
"react-day-picker": "^10.0.1",
"react-dom": "^19.2.7",
Expand Down
103 changes: 52 additions & 51 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions src/lib/ory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import axios from "axios";

/// Types and Interfaces

type SubmitBody = Partial<Flow> & { error?: { id?: string }; redirect_browser_to?: string };
export type FlowType = "login" | "registration" | "recovery" | "verification" | "settings";

export type SubmitResponse =
| { kind: "success" }
| { flow?: Flow; kind: "success" }
| { flow: Flow; kind: "validation" }
| { kind: "redirect"; url: string }
| { kind: "refresh"; url: string }
| { kind: "expired" };

export interface Message {
Expand All @@ -23,7 +25,7 @@ export interface OryNode {
id?: string;
name?: string;
src?: string;
text?: { text?: string };
text?: { context?: { secrets?: { text?: string }[] }; text?: string };
value?: unknown;
};
messages: Message[];
Expand All @@ -42,6 +44,13 @@ export const ORY_URL =

const ACCEPT_JSON = { headers: { Accept: "application/json" } };

/// Helper functions

function toFlow(body: SubmitBody): Flow | undefined {
const { id, return_to, ui } = body;
return id === undefined || ui === undefined ? undefined : { id, return_to, ui };
}

/// Exported functions

export function csrfToken(flow: Flow): string {
Expand Down Expand Up @@ -73,12 +82,18 @@ export async function orySubmit(
action: string,
body: Record<string, unknown>,
): Promise<SubmitResponse> {
const { status, data } = await axios.post<Flow & { redirect_browser_to?: string }>(action, body, {
const { status, data } = await axios.post<SubmitBody>(action, body, {
...ACCEPT_JSON,
validateStatus: () => true,
});
const flow = toFlow(data);

if (status < 300) return { kind: "success" };
if (data.redirect_browser_to) return { kind: "redirect", url: data.redirect_browser_to };
return status === 400 ? { flow: data, kind: "validation" } : { kind: "expired" };
if (status < 300) return { flow, kind: "success" };
if (data.redirect_browser_to) {
const reason = data.error?.id;
return reason === "session_refresh_required" || reason === "session_aal2_required"
? { kind: "refresh", url: data.redirect_browser_to }
: { kind: "redirect", url: data.redirect_browser_to };
}
return status === 400 && flow ? { flow, kind: "validation" } : { kind: "expired" };
}
Loading