53 lines
2.1 KiB
TypeScript
53 lines
2.1 KiB
TypeScript
// VA-P23 · Microsoft OAuth callback (M09 Block B). Mirrors the Gmail callback —
|
|
// fixed Azure-registered URI, provider-pinned signed state, token sealing +
|
|
// mailbox upsert in the connection service.
|
|
|
|
import { type LoaderFunctionArgs, redirect } from "react-router";
|
|
import { completeOAuthCallback } from "~/modules/emailInbox/services/MailboxConnectionService.server";
|
|
import { verifyEmailOAuthState } from "~/modules/emailInbox/services/oauthState.server";
|
|
import { emailSettingsPath } from "~/modules/emailInbox/routes/emailInboxRoutePaths";
|
|
import { runAuthenticatedProviderOAuthCallback } from "~/utils/integrations/providerOAuthCallbackAccess.server";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
function settingsRedirect(state: string | null, flag: "connected" | "error"): Response {
|
|
if (state) {
|
|
const verified = verifyEmailOAuthState(state, "microsoft");
|
|
if (verified.ok) {
|
|
return redirect(`${emailSettingsPath(verified.payload.tenantSlug)}?email=${flag}`);
|
|
}
|
|
}
|
|
return redirect(`/?email=${flag}`);
|
|
}
|
|
|
|
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
|
const url = new URL(request.url);
|
|
const code = url.searchParams.get("code");
|
|
const state = url.searchParams.get("state");
|
|
const providerError = url.searchParams.get("error");
|
|
|
|
if (!state) {
|
|
return settingsRedirect(state, "error");
|
|
}
|
|
const verified = verifyEmailOAuthState(state, "microsoft");
|
|
if (!verified.ok || providerError || !code) return settingsRedirect(state, "error");
|
|
try {
|
|
const { tenantSlug } = await runAuthenticatedProviderOAuthCallback({
|
|
request,
|
|
tenantId: verified.payload.tenantId,
|
|
userId: verified.payload.userId,
|
|
callback: () => completeOAuthCallback({ code, state, provider: "microsoft" }),
|
|
});
|
|
return redirect(`${emailSettingsPath(tenantSlug)}?email=connected`);
|
|
} catch {
|
|
return settingsRedirect(state, "error");
|
|
}
|
|
};
|
|
|
|
export default function MicrosoftOAuthCallbackRoute() {
|
|
return <main data-ui-id="ui.email.oauth.callback.microsoft" />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|