mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 11:07:40 +01:00
fix(build): lazy-init Resend SDK to fix Vercel build crash; centralize auth routes
Resend SDK throws at construction when given an empty string API key, which crashed next build on routes that import email.ts. Fix: lazy-initialize via getResend() so the Resend instance is only created at send time, not module evaluation time. Route SSOT: create src/config/auth.ts with ROUTES and PUBLIC_NAV_LINKS constants. Replace 25+ hardcoded "/sign-in", "/today", "/onboarding" etc. strings across all auth pages, public pages, AuthShell, SignInForm, and SidebarFooter. Nav links for public surface (landing + auth) now come from one place. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
d2ee8b993a
commit
60c858dc89
14 files changed
+78
-47
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
AuthFooterLink, AuthHeading,
|
||||
} from "@/components/auth/AuthShell";
|
||||
import { postJson } from "@/lib/api/fetch";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
|
||||
export default function ForgotPasswordPage() {
|
||||
const [email, setEmail] = useState("");
|
||||
@@ -35,7 +36,7 @@ export default function ForgotPasswordPage() {
|
||||
title="Check your email"
|
||||
description={`If an account exists for ${email}, we've sent a reset link. Check your inbox (and spam folder).`}
|
||||
/>
|
||||
<AuthFooterLink href="/sign-in">← Back to sign in</AuthFooterLink>
|
||||
<AuthFooterLink href={ROUTES.SIGN_IN}>← Back to sign in</AuthFooterLink>
|
||||
</AuthShell>
|
||||
);
|
||||
}
|
||||
@@ -74,7 +75,7 @@ export default function ForgotPasswordPage() {
|
||||
|
||||
<p className="mt-4 text-center text-xs text-text-muted">
|
||||
Remember it?{" "}
|
||||
<Link href="/sign-in" className="text-text-secondary underline underline-offset-2 hover:text-text-primary">
|
||||
<Link href={ROUTES.SIGN_IN} className="text-text-secondary underline underline-offset-2 hover:text-text-primary">
|
||||
Sign in
|
||||
</Link>
|
||||
</p>
|
||||
|
||||
@@ -5,6 +5,7 @@ import { useRouter } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import { signIn } from "next-auth/react";
|
||||
import { getJson, postJson } from "@/lib/api/fetch";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
import {
|
||||
AuthShell, AuthCard, AuthField, AuthInput, AuthSubmitButton, AuthIconBadge, AuthHeading,
|
||||
} from "@/components/auth/AuthShell";
|
||||
@@ -53,9 +54,9 @@ export default function InvitePage({ params }: { params: Promise<{ token: string
|
||||
|
||||
const result = await signIn("user-password", { userId: data.userId, password, redirect: false });
|
||||
if (result?.ok) {
|
||||
router.push("/today");
|
||||
router.push(ROUTES.APP_HOME);
|
||||
} else {
|
||||
router.push("/sign-in");
|
||||
router.push(ROUTES.SIGN_IN);
|
||||
}
|
||||
} catch {
|
||||
setError("Something went wrong.");
|
||||
@@ -88,7 +89,7 @@ export default function InvitePage({ params }: { params: Promise<{ token: string
|
||||
|
||||
{isError && (
|
||||
<p className="ui-auth-note">
|
||||
<Link href="/sign-in" className="ui-auth-inline-link">Sign in</Link>{" "}
|
||||
<Link href={ROUTES.SIGN_IN} className="ui-auth-inline-link">Sign in</Link>{" "}
|
||||
if you already have an account.
|
||||
</p>
|
||||
)}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { useSession } from "next-auth/react";
|
||||
import { Loader2, GitBranch, Star, Lock } from "lucide-react";
|
||||
import { normalizeUsername } from "@/lib/username";
|
||||
import { postJson, patchJson, throwApiError } from "@/lib/api/fetch";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
import {
|
||||
AuthShell,
|
||||
AuthCard,
|
||||
@@ -137,7 +138,7 @@ export default function OnboardingPage() {
|
||||
if (orgsData.orgs && orgsData.orgs.length > 0) {
|
||||
await patchJson("/api/me", { onboardedAt: new Date().toISOString() });
|
||||
await update();
|
||||
router.push("/today");
|
||||
router.push(ROUTES.APP_HOME);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -168,7 +169,7 @@ export default function OnboardingPage() {
|
||||
}
|
||||
await patchJson("/api/me", { onboardedAt: new Date().toISOString() });
|
||||
await update();
|
||||
router.push("/today");
|
||||
router.push(ROUTES.APP_HOME);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Something went wrong");
|
||||
} finally {
|
||||
|
||||
+8
-13
@@ -12,30 +12,25 @@ import {
|
||||
LANDING_WHITEPAPER_LABEL,
|
||||
} from "@/config/marketing";
|
||||
import { isStripeReady } from "@/lib/stripe";
|
||||
|
||||
const NAV_LINKS = [
|
||||
{ label: "Features", href: "#features" },
|
||||
{ label: "Pricing", href: "#pricing" },
|
||||
{ label: "Whitepaper", href: "/whitepaper" },
|
||||
];
|
||||
import { ROUTES, PUBLIC_NAV_LINKS } from "@/config/auth";
|
||||
|
||||
export default async function LandingPage() {
|
||||
if ((await getUserCount()) === 0) redirect("/setup");
|
||||
|
||||
const session = await auth();
|
||||
if (session?.user) redirect("/today");
|
||||
if (session?.user) redirect(ROUTES.APP_HOME);
|
||||
|
||||
const stripeReady = isStripeReady();
|
||||
|
||||
return (
|
||||
<PublicSurface
|
||||
navLinks={NAV_LINKS}
|
||||
navLinks={PUBLIC_NAV_LINKS}
|
||||
right={(
|
||||
<div className="flex items-center gap-2">
|
||||
<Link href="/sign-in" className="ui-public-nav-link hidden sm:block">
|
||||
<Link href={ROUTES.SIGN_IN} className="ui-public-nav-link hidden sm:block">
|
||||
Sign in
|
||||
</Link>
|
||||
<Link href="/sign-in" className="ui-public-primary-action !py-2 !px-5 text-sm">
|
||||
<Link href={ROUTES.SIGN_IN} className="ui-public-primary-action !py-2 !px-5 text-sm">
|
||||
Get started →
|
||||
</Link>
|
||||
</div>
|
||||
@@ -55,7 +50,7 @@ export default async function LandingPage() {
|
||||
</p>
|
||||
|
||||
<div className="mt-10 flex flex-wrap items-center justify-center gap-3">
|
||||
<Link href="/sign-in" className="ui-public-primary-action">
|
||||
<Link href={ROUTES.SIGN_IN} className="ui-public-primary-action">
|
||||
Get started →
|
||||
</Link>
|
||||
<Link href="/whitepaper" className="ui-public-nav-action px-8 py-3">
|
||||
@@ -116,8 +111,8 @@ export default async function LandingPage() {
|
||||
<Link
|
||||
href={
|
||||
stripeReady
|
||||
? `/sign-in?callbackUrl=${encodeURIComponent(`/api/checkout/${tier.name.toLowerCase()}`)}`
|
||||
: "/sign-in"
|
||||
? `${ROUTES.SIGN_IN}?callbackUrl=${encodeURIComponent(`/api/checkout/${tier.name.toLowerCase()}`)}`
|
||||
: ROUTES.SIGN_IN
|
||||
}
|
||||
className={
|
||||
tier.highlighted
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
AuthHeading,
|
||||
} from "@/components/auth/AuthShell";
|
||||
import { postJson } from "@/lib/api/fetch";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
|
||||
export default function ResetPasswordPage({ params }: { params: Promise<{ token: string }> }) {
|
||||
const { token } = use(params);
|
||||
@@ -30,7 +31,7 @@ export default function ResetPasswordPage({ params }: { params: Promise<{ token:
|
||||
const data = await res.json();
|
||||
if (!res.ok) { setError(data.error ?? "Reset failed."); return; }
|
||||
setDone(true);
|
||||
setTimeout(() => router.push("/sign-in"), 2000);
|
||||
setTimeout(() => router.push(ROUTES.SIGN_IN), 2000);
|
||||
} catch {
|
||||
setError("Something went wrong. Please try again.");
|
||||
} finally {
|
||||
@@ -94,7 +95,7 @@ export default function ResetPasswordPage({ params }: { params: Promise<{ token:
|
||||
|
||||
<p className="mt-4 text-center text-xs text-text-muted">
|
||||
Link expired?{" "}
|
||||
<Link href="/forgot-password" className="text-text-secondary underline underline-offset-2 hover:text-text-primary">
|
||||
<Link href={ROUTES.FORGOT_PASSWORD} className="text-text-secondary underline underline-offset-2 hover:text-text-primary">
|
||||
Request a new one
|
||||
</Link>
|
||||
</p>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import { postJson } from "@/lib/api/fetch";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
import {
|
||||
AuthShell, AuthCard, AuthField, AuthInput, AuthSubmitButton, AuthIconBadge, AuthHeading,
|
||||
} from "@/components/auth/AuthShell";
|
||||
@@ -27,7 +28,7 @@ export default function SetupPage() {
|
||||
const res = await postJson("/api/setup", { name, password });
|
||||
const data = await res.json();
|
||||
if (!res.ok) { setError(data.error ?? "Setup failed."); return; }
|
||||
router.push("/sign-in");
|
||||
router.push(ROUTES.SIGN_IN);
|
||||
} catch {
|
||||
setError("Something went wrong.");
|
||||
} finally {
|
||||
@@ -91,7 +92,7 @@ export default function SetupPage() {
|
||||
|
||||
<p className="ui-auth-footer">
|
||||
Already have an account?{" "}
|
||||
<Link href="/sign-in" className="ui-auth-footer-link">
|
||||
<Link href={ROUTES.SIGN_IN} className="ui-auth-footer-link">
|
||||
Sign in
|
||||
</Link>
|
||||
</p>
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
AuthFooterLink, AuthHeading,
|
||||
} from "@/components/auth/AuthShell";
|
||||
import { postJson } from "@/lib/api/fetch";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
|
||||
export default function SignUpPage() {
|
||||
const router = useRouter();
|
||||
@@ -32,9 +33,9 @@ export default function SignUpPage() {
|
||||
|
||||
const result = await signIn("email-password", { email, password, redirect: false });
|
||||
if (result?.ok) {
|
||||
router.push("/onboarding");
|
||||
router.push(ROUTES.ONBOARDING);
|
||||
} else {
|
||||
router.push("/sign-in");
|
||||
router.push(ROUTES.SIGN_IN);
|
||||
}
|
||||
} catch {
|
||||
setError("Something went wrong. Please try again.");
|
||||
@@ -107,7 +108,7 @@ export default function SignUpPage() {
|
||||
</form>
|
||||
</AuthCard>
|
||||
|
||||
<AuthFooterLink href="/sign-in">
|
||||
<AuthFooterLink href={ROUTES.SIGN_IN}>
|
||||
Already have an account? Sign in →
|
||||
</AuthFooterLink>
|
||||
</AuthShell>
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
AuthFooterLink, AuthHeading,
|
||||
} from "@/components/auth/AuthShell";
|
||||
import { postJson } from "@/lib/api/fetch";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
|
||||
function VerifyEmailInner() {
|
||||
const params = useSearchParams();
|
||||
@@ -80,7 +81,7 @@ function VerifyEmailInner() {
|
||||
</form>
|
||||
</AuthCard>
|
||||
)}
|
||||
<AuthFooterLink href="/sign-in">← Back to sign in</AuthFooterLink>
|
||||
<AuthFooterLink href={ROUTES.SIGN_IN}>← Back to sign in</AuthFooterLink>
|
||||
</AuthShell>
|
||||
);
|
||||
}
|
||||
@@ -122,7 +123,7 @@ function VerifyEmailInner() {
|
||||
</form>
|
||||
</AuthCard>
|
||||
)}
|
||||
<AuthFooterLink href="/sign-in">← Back to sign in</AuthFooterLink>
|
||||
<AuthFooterLink href={ROUTES.SIGN_IN}>← Back to sign in</AuthFooterLink>
|
||||
</AuthShell>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import Link from "next/link";
|
||||
import type { Metadata } from "next";
|
||||
import { PublicSurface } from "@/components/public/PublicSurface";
|
||||
import { parseThoughtBlocks } from "@/lib/thoughts-content";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Whitepaper — Cockpit",
|
||||
@@ -44,7 +45,7 @@ export default function WhitepaperPage() {
|
||||
return (
|
||||
<PublicSurface
|
||||
right={(
|
||||
<Link href="/sign-in" className="ui-public-nav-action">
|
||||
<Link href={ROUTES.SIGN_IN} className="ui-public-nav-action">
|
||||
Get started →
|
||||
</Link>
|
||||
)}
|
||||
@@ -147,7 +148,7 @@ export default function WhitepaperPage() {
|
||||
<p className="mb-2 text-sm font-medium text-white/60">Ready to close the execution gap?</p>
|
||||
<p className="mb-8 text-sm text-white/30">Start using Cockpit as your builder operating system.</p>
|
||||
<div className="flex flex-wrap items-center justify-center gap-3">
|
||||
<Link href="/sign-in" className="ui-public-primary-action">
|
||||
<Link href={ROUTES.SIGN_IN} className="ui-public-primary-action">
|
||||
Get started →
|
||||
</Link>
|
||||
<Link href="/" className="ui-public-nav-action px-6 py-2.5 text-sm">
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import Link from "next/link";
|
||||
import { PublicSurface, type NavLink } from "@/components/public/PublicSurface";
|
||||
import { PublicSurface } from "@/components/public/PublicSurface";
|
||||
import { PUBLIC_SURFACE } from "@/config/ui";
|
||||
|
||||
const AUTH_NAV_LINKS: NavLink[] = [
|
||||
{ label: "Features", href: "/#features" },
|
||||
{ label: "Pricing", href: "/#pricing" },
|
||||
{ label: "Whitepaper", href: "/whitepaper" },
|
||||
];
|
||||
import { PUBLIC_NAV_LINKS } from "@/config/auth";
|
||||
|
||||
export function AuthField({
|
||||
label,
|
||||
@@ -92,7 +87,7 @@ export function AuthShell({
|
||||
navRight?: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<PublicSurface navLinks={AUTH_NAV_LINKS} right={navRight}>
|
||||
<PublicSurface navLinks={PUBLIC_NAV_LINKS} right={navRight}>
|
||||
<main
|
||||
className="relative z-10 flex items-center justify-center px-4 pb-16"
|
||||
style={{ minHeight: `calc(100vh - ${PUBLIC_SURFACE.navHeightPx}px)` }}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { signIn } from "next-auth/react";
|
||||
import { useState, Suspense } from "react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
import {
|
||||
AuthShell, AuthCard, AuthField, AuthInput, AuthSubmitButton,
|
||||
AuthDivider, AuthHeading, AuthSecondaryButton,
|
||||
@@ -48,8 +49,8 @@ function FormInner({
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const callbackUrl = searchParams.get("callbackUrl") ?? "/today";
|
||||
const safeCallback = callbackUrl.startsWith("/") && !callbackUrl.startsWith("//") ? callbackUrl : "/today";
|
||||
const callbackUrl = searchParams.get("callbackUrl") ?? ROUTES.APP_HOME;
|
||||
const safeCallback = callbackUrl.startsWith("/") && !callbackUrl.startsWith("//") ? callbackUrl : ROUTES.APP_HOME;
|
||||
|
||||
const [mode, setMode] = useState<Mode>("email");
|
||||
const [email, setEmail] = useState("");
|
||||
@@ -200,7 +201,7 @@ function FormInner({
|
||||
/>
|
||||
<div className="text-right">
|
||||
<Link
|
||||
href="/forgot-password"
|
||||
href={ROUTES.FORGOT_PASSWORD}
|
||||
className="text-xs text-text-muted hover:text-text-secondary underline underline-offset-2"
|
||||
>
|
||||
Forgot password?
|
||||
@@ -217,7 +218,7 @@ function FormInner({
|
||||
/>
|
||||
<p className="text-center text-sm text-white/35">
|
||||
No account?{" "}
|
||||
<Link href="/sign-up" className="text-white/60 underline underline-offset-2 hover:text-white/80 transition-colors">
|
||||
<Link href={ROUTES.SIGN_UP} className="text-white/60 underline underline-offset-2 hover:text-white/80 transition-colors">
|
||||
Create one free →
|
||||
</Link>
|
||||
</p>
|
||||
|
||||
@@ -5,6 +5,7 @@ import { signOut } from "next-auth/react";
|
||||
import { ThemeToggle } from "../ThemeToggle";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { usePrivateZone } from "@/hooks/use-private-zone";
|
||||
import { ROUTES } from "@/config/auth";
|
||||
|
||||
export function SidebarFooter({
|
||||
collapsed,
|
||||
@@ -43,7 +44,7 @@ export function SidebarFooter({
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={() => signOut({ callbackUrl: "/sign-in" })}
|
||||
onClick={() => signOut({ callbackUrl: ROUTES.SIGN_IN })}
|
||||
className={cn(
|
||||
"ui-sidebar-utility w-full",
|
||||
collapsed && "justify-center px-2",
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { NavLink } from "@/components/public/PublicSurface";
|
||||
|
||||
// Single source of truth for all auth and app route strings.
|
||||
// Import from here — never hardcode "/sign-in" or "/today" in components.
|
||||
export const ROUTES = {
|
||||
// Public app
|
||||
HOME: "/",
|
||||
// Auth
|
||||
SIGN_IN: "/sign-in",
|
||||
SIGN_UP: "/sign-up",
|
||||
FORGOT_PASSWORD: "/forgot-password",
|
||||
RESET_PASSWORD: "/reset-password",
|
||||
VERIFY_EMAIL: "/verify-email",
|
||||
ONBOARDING: "/onboarding",
|
||||
SIGN_OUT: "/sign-out",
|
||||
// Post-auth default
|
||||
APP_HOME: "/today",
|
||||
} as const;
|
||||
|
||||
// Nav links shared by landing page and all auth pages
|
||||
export const PUBLIC_NAV_LINKS: NavLink[] = [
|
||||
{ label: "Features", href: "/#features" },
|
||||
{ label: "Pricing", href: "/#pricing" },
|
||||
{ label: "Whitepaper", href: "/whitepaper" },
|
||||
];
|
||||
+9
-3
@@ -1,6 +1,12 @@
|
||||
import { Resend } from "resend";
|
||||
|
||||
const resend = new Resend(process.env.RESEND_API_KEY ?? "");
|
||||
// Lazy — Resend throws at construction if key is empty string, which breaks next build
|
||||
let _resend: Resend | null = null;
|
||||
function getResend(): Resend {
|
||||
if (!_resend) _resend = new Resend(process.env.RESEND_API_KEY!);
|
||||
return _resend;
|
||||
}
|
||||
|
||||
const FROM = process.env.EMAIL_FROM ?? "Cockpit <noreply@cockpit.app>";
|
||||
|
||||
function appUrl(): string {
|
||||
@@ -14,7 +20,7 @@ export function sendEmailFire(to: string, subject: string, html: string, text: s
|
||||
console.log(` To: ${to}\n Subject: ${subject}\n Text: ${text}`);
|
||||
return;
|
||||
}
|
||||
resend.emails.send({ from: FROM, to, subject, html, text }).catch((err) => {
|
||||
getResend().emails.send({ from: FROM, to, subject, html, text }).catch((err) => {
|
||||
console.error("[email] send error:", err);
|
||||
});
|
||||
}
|
||||
@@ -25,7 +31,7 @@ export async function sendEmail(to: string, subject: string, html: string, text:
|
||||
console.log("[email] no RESEND_API_KEY — skipping send");
|
||||
return;
|
||||
}
|
||||
const { error } = await resend.emails.send({ from: FROM, to, subject, html, text });
|
||||
const { error } = await getResend().emails.send({ from: FROM, to, subject, html, text });
|
||||
if (error) throw new Error(`Resend error: ${error.message}`);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user