import { escape } from "es-toolkit/compat"; import type { Context } from "koa"; /** * 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 url 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: Context, url: string, method: "GET" | "POST" = "GET" ) { 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 += ``; }); if (this.userAgent.isBot) { formFields += `
If you are not redirected automatically, please click the button below.
`; } this.body = `