zerobyte/app/server/lib/auth/utils/sso-context.ts
2026-02-27 17:47:55 +01:00

31 lines
715 B
TypeScript

import type { GenericEndpointContext } from "@better-auth/core";
export function normalizeEmail(email: string): string {
return email.trim().toLowerCase();
}
export function extractProviderIdFromUrl(url: string): string | null {
try {
const pathname = new URL(url, "http://localhost").pathname;
const match = pathname.match(/\/sso\/(?:saml2\/)?callback\/([^/]+)$/);
return match?.[1] ?? null;
} catch {
return null;
}
}
export function extractProviderIdFromContext(ctx?: GenericEndpointContext | null) {
if (!ctx) {
return null;
}
if (ctx.params?.providerId) {
return ctx.params.providerId;
}
if (ctx.request?.url) {
return extractProviderIdFromUrl(ctx.request.url);
}
return null;
}