Files
outline/server/utils/redirectOnClient.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

74 lines
2.0 KiB
TypeScript

import { escape } from "es-toolkit/compat";
import { isbot } from "isbot";
import { addMissingUrlPort } from "@shared/utils/urls";
import type { AppContext } from "@server/types";
/**
* Performs a redirect on the browser so that the user's auth cookies are
* included in the request. Assigned to the Koa context as `redirectOnClient`.
*
* @param redirectTo the URL to redirect to.
* @param method the HTTP method to use for the redirect. Use POST when
* preventing links in emails from being clicked by bots. Otherwise, use GET.
*/
export function redirectOnClient(
this: AppContext,
redirectTo: string,
method: "GET" | "POST" = "GET"
) {
const url = addMissingUrlPort(redirectTo);
this.type = "text/html";
if (method === "POST") {
// For POST method, create a form that auto-submits
const urlObj = new URL(url);
const formAction = `${urlObj.origin}${urlObj.pathname}`;
const searchParams = urlObj.searchParams;
let formFields = "";
searchParams.forEach((value, key) => {
formFields += `<input type="hidden" name="${escape(
key
)}" value="${escape(value)}" />`;
});
// Bots are shown a manual submit button instead of auto-submitting, so that
// link scanners in email clients don't follow the redirect.
const isBot = isbot(this.get("user-agent"));
if (isBot) {
formFields += `
<p>If you are not redirected automatically, please click the button below.</p>
<input type="submit" value="Continue" />
`;
}
const script = isBot
? ""
: `<script nonce="${this.state.cspNonce}">
document.getElementById('redirect-form').submit();
</script>`;
this.body = `
<html lang="en">
<head>
<title>Redirecting…</title>
</head>
<body>
<form id="redirect-form" method="POST" action="${formAction}">
${formFields}
</form>
${script}
</body>
</html>`;
} else {
// Default GET method using meta refresh
this.body = `
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL='${escape(url)}'" />
</head>
</html>`;
}
}