All files / lib oauth-redirect-uris.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 2/2
100% Lines 13/13

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                    1x             1x             1x 1x 1x                       52x 52x                     3x 3x 12x 24x 48x                         3x    
/**
 * Computes the redirect_uri values Better Auth sends to OAuth providers.
 *
 * Format mirrors Better Auth's social-provider behavior:
 *   `<BETTER_AUTH_URL><basePath>/callback/<provider>`
 *
 * Source of truth for basePath:
 *   - pro:       apps/api/src/lib/auth.ts            (basePath defaults to `/api/auth`)
 *   - homeowner: apps/api/src/lib/homeowner-auth.ts  (basePath = `/api/homeowner/auth`)
 *
 * Used by the operator script `scripts/print-oauth-redirect-uris.ts`
 * and the runbook docs/operations/oauth-redirect-uris.md.
 */
 
export type OAuthProvider = "google" | "facebook";
export type AuthInstance = "pro" | "homeowner";
export type EnvName = "local" | "dev" | "preview" | "production";
 
export const AUTH_INSTANCE_BASE_PATHS: Record<AuthInstance, string> = {
	pro: "/api/auth",
	homeowner: "/api/homeowner/auth",
};
 
// BETTER_AUTH_URL per environment. Kept in sync with apps/api/wrangler.jsonc;
// a drift-guard test in oauth-redirect-uris.test.ts asserts the match.
export const BETTER_AUTH_URL_BY_ENV: Record<EnvName, string> = {
	local: "http://localhost:7001",
	dev: "https://api-dev.decorrocket.com",
	preview: "https://api-preview.decorrocket.com",
	production: "https://api.interioring.com",
};
 
export const ALL_AUTH_INSTANCES: readonly AuthInstance[] = ["pro", "homeowner"];
export const ALL_PROVIDERS: readonly OAuthProvider[] = ["google", "facebook"];
export const ALL_ENVS: readonly EnvName[] = [
	"local",
	"dev",
	"preview",
	"production",
];
 
export function buildOAuthRedirectUri(
	betterAuthUrl: string,
	instance: AuthInstance,
	provider: OAuthProvider,
): string {
	const trimmed = betterAuthUrl.replace(/\/+$/, "");
	return `${trimmed}${AUTH_INSTANCE_BASE_PATHS[instance]}/callback/${provider}`;
}
 
export interface RedirectUriRow {
	env: EnvName;
	instance: AuthInstance;
	provider: OAuthProvider;
	uri: string;
}
 
export function getAllRedirectUris(): RedirectUriRow[] {
	const rows: RedirectUriRow[] = [];
	for (const env of ALL_ENVS) {
		for (const instance of ALL_AUTH_INSTANCES) {
			for (const provider of ALL_PROVIDERS) {
				rows.push({
					env,
					instance,
					provider,
					uri: buildOAuthRedirectUri(
						BETTER_AUTH_URL_BY_ENV[env],
						instance,
						provider,
					),
				});
			}
		}
	}
	return rows;
}