返回 CodeWhale
public-auth-routes.ts
根目录 / web / lib / public-auth-routes.ts
1 /**
2 * Public-site account-entry aliases (#5767).
3 *
4 * codewhale.net is not the signed-in CWC app. `/signin`, `/signup`, and
5 * `/auth/callback` used to locale-prefix into `/en/...` and 404. This module
6 * is the one mapping: callback hops to the CWC app immediately (query
7 * preserved); sign-in is a locale-aware public page and create-account
8 * redirects there. The terminal needs no account.
9 */
10 import { APP_URL } from "./i18n/links";
11 import { pathLocale } from "./i18n/path";
12
13 /**
14 * App-icon raster (1254×1254) used on public account-entry pages: the canonical
15 * mark in the current brand treatment — blue gradient glyph on a white rounded
16 * tile. The master vectors are the canonical family in
17 * `codewhale-apps/packages/brand/svg/` (byte copies in `web/public/brand/`) —
18 * never hand-edit this PNG, regenerate it from the family. Pinned by hash so a
19 * redrawn or stale substitute fails the test rather than shipping.
20 */
21 export const CANONICAL_MARK_SRC = "/brand/codewhale-mark.png";
22 export const CANONICAL_MARK_SHA256 =
23 "7c174d1125b279d6c8200c776a6ed7b89a21e18bbf1c6e7dccf486a7a6879d8f";
24
25 export const APP_AUTH_CALLBACK_URL = `${APP_URL}/auth/callback`;
26
27 const SIGN_IN_SLUGS = new Set(["signin"]);
28 const SIGN_IN_ALIASES = new Set(["login"]);
29 const SIGN_UP_SLUGS = new Set(["signup"]);
30 const SIGN_UP_ALIASES = new Set(["register", "create-account"]);
31
32 export type PublicAuthKind = "sign-in" | "sign-up" | "callback";
33
34 /** Path after a routed locale prefix, with no leading slash. */
35 export function publicAuthRemainder(pathname: string): string {
36 const segments = pathname.split("/").filter(Boolean);
37 if (pathLocale(pathname)) segments.shift();
38 return segments.join("/");
39 }
40
41 export function publicAuthKind(pathname: string): PublicAuthKind | null {
42 const remainder = publicAuthRemainder(pathname);
43 if (remainder === "auth/callback") return "callback";
44 if (SIGN_IN_SLUGS.has(remainder) || SIGN_IN_ALIASES.has(remainder)) return "sign-in";
45 if (SIGN_UP_SLUGS.has(remainder) || SIGN_UP_ALIASES.has(remainder)) return "sign-up";
46 return null;
47 }
48
49 /**
50 * Fold `/login`, `/register`, and `/create-account` onto `/signin`, keeping the
51 * locale prefix when one is already present. Bare aliases still go through
52 * the locale-prefix branch after this rewrite so `/login` becomes `/en/signin`
53 * rather than a 404.
54 */
55 export function canonicalPublicAuthPath(pathname: string): string | null {
56 const remainder = publicAuthRemainder(pathname);
57 const canonical =
58 SIGN_IN_ALIASES.has(remainder) ? "signin"
59 : SIGN_UP_ALIASES.has(remainder) ? "signin"
60 : null;
61 if (!canonical) return null;
62 const locale = pathLocale(pathname);
63 return locale ? `/${locale}/${canonical}` : `/${canonical}`;
64 }
65
66 /** CWC app destination. Chinese public pages keep the `/zh/` app entry. */
67 export function publicAuthAppDestination(
68 locale: string | null,
69 ): string {
70 const localized = locale === "zh" ? "/zh" : "";
71 return `${APP_URL}${localized}/login`;
72 }
73
74 /** OAuth/email callbacks belong on the CWC app, never a localized 404. */
75 export function publicAuthCallbackDestination(url: URL): string | null {
76 if (publicAuthKind(url.pathname) !== "callback") return null;
77 const destination = new URL(APP_AUTH_CALLBACK_URL);
78 destination.search = url.search;
79 return destination.toString();
80 }
81
81 lines TYPESCRIPT