45 lines
1.7 KiB
TypeScript
45 lines
1.7 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 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 (providerError || !code || !state) {
|
|
return settingsRedirect(state, "error");
|
|
}
|
|
try {
|
|
const { tenantSlug } = await 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 />;
|
|
}
|