Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

refactor(auth): consolidate web login into shared LoginPage component

Naiyuan Qing547b8839b25b
diff --git a/apps/web/app/(auth)/login/page.test.tsx b/apps/web/app/(auth)/login/page.test.tsx
index 4ecb511dcd..91030ac203 100644
--- a/apps/web/app/(auth)/login/page.test.tsx
+++ b/apps/web/app/(auth)/login/page.test.tsx
@@ -2,37 +2,51 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
+const { mockSendCode, mockVerifyCode, mockHydrateWorkspace } = vi.hoisted(
+ () => ({
+ mockSendCode: vi.fn(),
+ mockVerifyCode: vi.fn(),
+ mockHydrateWorkspace: vi.fn(),
+ }),
+);
+
// Mock next/navigation
vi.mock("next/navigation", () => ({
- useRouter: () => ({ push: vi.fn() }),
+ useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
usePathname: () => "/login",
useSearchParams: () => new URLSearchParams(),
}));
-// Mock auth store
-const mockSendCode = vi.fn();
-const mockVerifyCode = vi.fn();
-vi.mock("@multica/core/auth", () => ({
- useAuthStore: (selector: (s: any) => any) =>
- selector({
- sendCode: mockSendCode,
- verifyCode: mockVerifyCode,
- }),
-}));
+// Mock auth store — shared LoginPage uses getState().sendCode/verifyCode,
+// web wrapper uses useAuthStore((s) => s.user/isLoading)
+vi.mock("@multica/core/auth", () => {
+ const authState = {
+ sendCode: mockSendCode,
+ verifyCode: mockVerifyCode,
+ user: null,
+ isLoading: false,
+ };
+ const useAuthStore = Object.assign(
+ (selector: (s: typeof authState) => unknown) => selector(authState),
+ { getState: () => authState },
+ );
+ return { useAuthStore };
+});
// Mock auth-cookie
vi.mock("@/features/auth/auth-cookie", () => ({
setLoggedInCookie: vi.fn(),
}));
-// Mock workspace store
-const mockHydrateWorkspace = vi.fn();
-vi.mock("@multica/core/workspace", () => ({
- useWorkspaceStore: (selector: (s: any) => any) =>
- selector({
- hydrateWorkspace: mockHydrateWorkspace,
- }),
-}));
+// Mock workspace store — shared LoginPage uses getState().hydrateWorkspace
+vi.mock("@multica/core/workspace", () => {
+ const wsState = { hydrateWorkspace: mockHydrateWorkspace };
+ const useWorkspaceStore = Object.assign(
+ (selector: (s: typeof wsState) => unknown) => selector(wsState),
+ { getState: () => wsState },
+ );
+ return { useWorkspaceStore };
+});
// Mock api
vi.mock("@multica/core/api", () => ({
@@ -54,8 +68,8 @@ describe("LoginPage", () => {
it("renders login form with email input and continue button", () => {
render(<LoginPage />);
- expect(screen.getByText("Multica")).toBeInTheDocument();
- expect(screen.getByText("Turn coding agents into real teammates")).toBeInTheDocument();
+ expect(screen.getByText("Sign in to Multica")).toBeInTheDocument();
+ expect(screen.getByText("Enter your email to get a login code")).toBeInTheDocument();
expect(screen.getByLabelText("Email")).toBeInTheDocument();
expect(
screen.getByRole("button", { name: "Continue" })
diff --git a/apps/web/app/(auth)/login/page.tsx b/apps/web/app/(auth)/login/page.tsx
index 30df4edfbb..58d15f02c2 100644
--- a/apps/web/app/(auth)/login/page.tsx
+++ b/apps/web/app/(auth)/login/page.tsx
@@ -1,390 +1,58 @@
"use client";
-import { Suspense, useState, useEffect, useCallback } from "react";
+import { Suspense, useEffect } from "react";
import { useSearchParams, useRouter } from "next/navigation";
import { useAuthStore } from "@multica/core/auth";
import { setLoggedInCookie } from "@/features/auth/auth-cookie";
-import { useWorkspaceStore } from "@multica/core/workspace";
-import { api } from "@multica/core/api";
-import {
- Card,
- CardHeader,
- CardTitle,
- CardDescription,
- CardContent,
- CardFooter,
-} from "@multica/ui/components/ui/card";
-import { Input } from "@multica/ui/components/ui/input";
-import { Button } from "@multica/ui/components/ui/button";
-import { Label } from "@multica/ui/components/ui/label";
-import {
- InputOTP,
- InputOTPGroup,
- InputOTPSlot,
-} from "@multica/ui/components/ui/input-otp";
-import type { User } from "@multica/core/types";
+import { LoginPage, validateCliCallback } from "@multica/views/auth";
-function validateCliCallback(cliCallback: string): boolean {
- try {
- const cbUrl = new URL(cliCallback);
- if (cbUrl.protocol !== "http:") return false;
- if (cbUrl.hostname !== "localhost" && cbUrl.hostname !== "127.0.0.1")
- return false;
- return true;
- } catch {
- return false;
- }
-}
-
-function redirectToCliCallback(
- cliCallback: string,
- token: string,
- cliState: string
-) {
- const separator = cliCallback.includes("?") ? "&" : "?";
- window.location.href = `${cliCallback}${separator}token=${encodeURIComponent(token)}&state=${encodeURIComponent(cliState)}`;
-}
+const googleClientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID;
function LoginPageContent() {
const router = useRouter();
const user = useAuthStore((s) => s.user);
const isLoading = useAuthStore((s) => s.isLoading);
- const sendCode = useAuthStore((s) => s.sendCode);
- const verifyCode = useAuthStore((s) => s.verifyCode);
- const hydrateWorkspace = useWorkspaceStore((s) => s.hydrateWorkspace);
const searchParams = useSearchParams();
- // Already authenticated — redirect to dashboard
- useEffect(() => {
- if (!isLoading && user && !searchParams.get("cli_callback")) {
- router.replace(searchParams.get("next") || "/issues");
- }
- }, [isLoading, user, router, searchParams]);
-
- const [step, setStep] = useState<"email" | "code" | "cli_confirm">("email");
- const [email, setEmail] = useState("");
- const [code, setCode] = useState("");
- const [error, setError] = useState("");
- const [submitting, setSubmitting] = useState(false);
- const [cooldown, setCooldown] = useState(0);
- const [existingUser, setExistingUser] = useState<User | null>(null);
+ const cliCallbackRaw = searchParams.get("cli_callback");
+ const cliState = searchParams.get("cli_state") || "";
+ const nextUrl = searchParams.get("next") || "/issues";
- // Check for existing session when CLI callback is present.
+ // Already authenticated — redirect to dashboard (skip if CLI callback)
useEffect(() => {
- const cliCallback = searchParams.get("cli_callback");
- if (!cliCallback) return;
-
- const token = localStorage.getItem("multica_token");
- if (!token) return;
-
- if (!validateCliCallback(cliCallback)) return;
-
- // Verify the existing token is still valid.
- api.setToken(token);
- api
- .getMe()
- .then((user) => {
- setExistingUser(user);
- setStep("cli_confirm");
- })
- .catch(() => {
- // Token expired/invalid — clear and fall through to normal login.
- api.setToken(null);
- localStorage.removeItem("multica_token");
- });
- }, [searchParams]);
-
- useEffect(() => {
- if (cooldown <= 0) return;
- const timer = setTimeout(() => setCooldown((c) => c - 1), 1000);
- return () => clearTimeout(timer);
- }, [cooldown]);
-
- const handleCliAuthorize = async () => {
- const cliCallback = searchParams.get("cli_callback");
- const token = localStorage.getItem("multica_token");
- if (!cliCallback || !token) return;
- const cliState = searchParams.get("cli_state") || "";
- setSubmitting(true);
- redirectToCliCallback(cliCallback, token, cliState);
- };
-
- const handleSendCode = async (e?: React.FormEvent) => {
- e?.preventDefault();
- if (!email) {
- setError("Email is required");
- return;
+ if (!isLoading && user && !cliCallbackRaw) {
+ router.replace(nextUrl);
}
- setError("");
- setSubmitting(true);
- try {
- await sendCode(email);
- setStep("code");
- setCode("");
- setCooldown(10);
- } catch (err) {
- setError(
- err instanceof Error
- ? err.message
- : "Failed to send code. Make sure the server is running."
- );
- } finally {
- setSubmitting(false);
- }
- };
-
- const handleVerifyCode = useCallback(
- async (value: string) => {
- if (value.length !== 6) return;
- setError("");
- setSubmitting(true);
- try {
- const cliCallback = searchParams.get("cli_callback");
- if (cliCallback) {
- if (!validateCliCallback(cliCallback)) {
- setError("Invalid callback URL");
- setSubmitting(false);
- return;
- }
- const { token } = await api.verifyCode(email, value);
- // Persist session in the browser so the web app stays logged in
- localStorage.setItem("multica_token", token);
- api.setToken(token);
- setLoggedInCookie();
- const cliState = searchParams.get("cli_state") || "";
- redirectToCliCallback(cliCallback, token, cliState);
- return;
- }
-
- await verifyCode(email, value);
- const wsList = await api.listWorkspaces();
- const lastWsId = localStorage.getItem("multica_workspace_id");
- await hydrateWorkspace(wsList, lastWsId);
- router.push(searchParams.get("next") || "/issues");
- } catch (err) {
- setError(
- err instanceof Error ? err.message : "Invalid or expired code"
- );
- setCode("");
- setSubmitting(false);
- }
- },
- [email, verifyCode, hydrateWorkspace, router, searchParams]
- );
-
- const handleResend = async () => {
- if (cooldown > 0) return;
- setError("");
- try {
- await sendCode(email);
- setCooldown(10);
- } catch (err) {
- setError(
- err instanceof Error ? err.message : "Failed to resend code"
- );
- }
- };
-
- // CLI confirm step: user is already logged in, just authorize.
- if (step === "cli_confirm" && existingUser) {
- return (
- <div className="flex min-h-screen items-center justify-center">
- <Card className="w-full max-w-sm">
- <CardHeader className="text-center">
- <CardTitle className="text-2xl">Authorize CLI</CardTitle>
- <CardDescription>
- Allow the CLI to access Multica as{" "}
- <span className="font-medium text-foreground">
- {existingUser.email}
- </span>
- ?
- </CardDescription>
- </CardHeader>
- <CardContent className="flex flex-col gap-3">
- <Button
- onClick={handleCliAuthorize}
- disabled={submitting}
- className="w-full"
- size="lg"
- >
- {submitting ? "Authorizing..." : "Authorize"}
- </Button>
- <Button
- variant="ghost"
- className="w-full"
- onClick={() => {
- setExistingUser(null);
- setStep("email");
- }}
- >
- Use a different account
- </Button>
- </CardContent>
- </Card>
- </div>
- );
- }
-
- if (step === "code") {
- return (
- <div className="flex min-h-screen items-center justify-center">
- <Card className="w-full max-w-sm">
- <CardHeader className="text-center">
- <CardTitle className="text-2xl">Check your email</CardTitle>
- <CardDescription>
- We sent a verification code to{" "}
- <span className="font-medium text-foreground">{email}</span>
- </CardDescription>
- </CardHeader>
- <CardContent className="flex flex-col items-center gap-4">
- <InputOTP
- maxLength={6}
- value={code}
- onChange={(value) => {
- setCode(value);
- if (value.length === 6) handleVerifyCode(value);
- }}
- disabled={submitting}
- >
- <InputOTPGroup>
- <InputOTPSlot index={0} />
- <InputOTPSlot index={1} />
- <InputOTPSlot index={2} />
- <InputOTPSlot index={3} />
- <InputOTPSlot index={4} />
- <InputOTPSlot index={5} />
- </InputOTPGroup>
- </InputOTP>
- {error && (
- <p className="text-sm text-destructive">{error}</p>
- )}
- <div className="flex items-center gap-2 text-sm text-muted-foreground">
- <button
- type="button"
- onClick={handleResend}
- disabled={cooldown > 0}
- className="text-primary underline-offset-4 hover:underline disabled:text-muted-foreground disabled:no-underline disabled:cursor-not-allowed"
- >
- {cooldown > 0 ? `Resend in ${cooldown}s` : "Resend code"}
- </button>
- </div>
- </CardContent>
- <CardFooter>
- <Button
- variant="ghost"
- className="w-full"
- onClick={() => {
- setStep("email");
- setCode("");
- setError("");
- }}
- >
- Back
- </Button>
- </CardFooter>
- </Card>
- </div>
- );
- }
+ }, [isLoading, user, router, nextUrl, cliCallbackRaw]);
- const googleClientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID;
-
- const handleGoogleLogin = () => {
- if (!googleClientId) return;
- const redirectUri = `${window.location.origin}/auth/callback`;
- const params = new URLSearchParams({
- client_id: googleClientId,
- redirect_uri: redirectUri,
- response_type: "code",
- scope: "openid email profile",
- access_type: "offline",
- prompt: "select_account",
- });
- window.location.href = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;
- };
+ const lastWorkspaceId =
+ typeof window !== "undefined"
+ ? localStorage.getItem("multica_workspace_id")
+ : null;
return (
- <div className="flex min-h-screen items-center justify-center">
- <Card className="w-full max-w-sm">
- <CardHeader className="text-center">
- <CardTitle className="text-2xl">Multica</CardTitle>
- <CardDescription>Turn coding agents into real teammates</CardDescription>
- </CardHeader>
- <CardContent>
- <form id="login-form" onSubmit={handleSendCode} className="space-y-4">
- <div className="space-y-2">
- <Label htmlFor="email">Email</Label>
- <Input
- id="email"
- type="email"
- placeholder="[email protected]"
- value={email}
- onChange={(e) => setEmail(e.target.value)}
- required
- />
- </div>
- {error && (
- <p className="text-sm text-destructive">{error}</p>
- )}
- </form>
- </CardContent>
- <CardFooter className="flex flex-col gap-3">
- <Button
- type="submit"
- form="login-form"
- disabled={submitting}
- className="w-full"
- size="lg"
- >
- {submitting ? "Sending code..." : "Continue"}
- </Button>
- {googleClientId && (
- <>
- <div className="relative w-full">
- <div className="absolute inset-0 flex items-center">
- <span className="w-full border-t" />
- </div>
- <div className="relative flex justify-center text-xs uppercase">
- <span className="bg-card px-2 text-muted-foreground">or</span>
- </div>
- </div>
- <Button
- type="button"
- variant="outline"
- className="w-full"
- size="lg"
- onClick={handleGoogleLogin}
- disabled={submitting}
- >
- <svg className="mr-2 h-4 w-4" viewBox="0 0 24 24">
- <path
- d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z"
- fill="#4285F4"
- />
- <path
- d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
- fill="#34A853"
- />
- <path
- d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
- fill="#FBBC05"
- />
- <path
- d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
- fill="#EA4335"
- />
- </svg>
- Continue with Google
- </Button>
- </>
- )}
- </CardFooter>
- </Card>
- </div>
+ <LoginPage
+ onSuccess={() => router.push(nextUrl)}
+ google={
+ googleClientId
+ ? {
+ clientId: googleClientId,
+ redirectUri: `${window.location.origin}/auth/callback`,
+ }
+ : undefined
+ }
+ cliCallback={
+ cliCallbackRaw && validateCliCallback(cliCallbackRaw)
+ ? { url: cliCallbackRaw, state: cliState }
+ : undefined
+ }
+ lastWorkspaceId={lastWorkspaceId}
+ onTokenObtained={setLoggedInCookie}
+ />
);
}
-export default function LoginPage() {
+export default function Page() {
return (
<Suspense fallback={null}>
<LoginPageContent />
diff --git a/packages/views/auth/index.ts b/packages/views/auth/index.ts
index 1cb1254d8b..979798bee1 100644
--- a/packages/views/auth/index.ts
+++ b/packages/views/auth/index.ts
@@ -1 +1 @@
-export { LoginPage } from "./login-page";
+export { LoginPage, validateCliCallback } from "./login-page";
diff --git a/packages/views/auth/login-page.tsx b/packages/views/auth/login-page.tsx
index 73ba9d2201..64ccee6eba 100644
--- a/packages/views/auth/login-page.tsx
+++ b/packages/views/auth/login-page.tsx
@@ -20,6 +20,7 @@ import {
import { useAuthStore } from "@multica/core/auth";
import { useWorkspaceStore } from "@multica/core/workspace";
import { api } from "@multica/core/api";
+import type { User } from "@multica/core/types";
// ---------------------------------------------------------------------------
// Types
@@ -30,6 +31,13 @@ interface GoogleAuthConfig {
redirectUri: string;
}
+interface CliCallbackConfig {
+ /** Validated localhost callback URL */
+ url: string;
+ /** Opaque state to pass back to CLI */
+ state: string;
+}
+
interface LoginPageProps {
/** Logo element rendered above the title */
logo?: ReactNode;
@@ -37,19 +45,74 @@ interface LoginPageProps {
onSuccess: () => void;
/** Google OAuth config. Omit to disable Google login. */
google?: GoogleAuthConfig;
+ /** CLI callback config for authorizing CLI tools. */
+ cliCallback?: CliCallbackConfig;
+ /** Preferred workspace ID to restore after login. */
+ lastWorkspaceId?: string | null;
+ /** Called after a token is obtained (e.g. to set cookies). */
+ onTokenObtained?: () => void;
+}
+
+// ---------------------------------------------------------------------------
+// Helpers
+// ---------------------------------------------------------------------------
+
+function redirectToCliCallback(url: string, token: string, state: string) {
+ const separator = url.includes("?") ? "&" : "?";
+ window.location.href = `${url}${separator}token=${encodeURIComponent(token)}&state=${encodeURIComponent(state)}`;
+}
+
+/** Validate that a CLI callback URL points to localhost over HTTP. */
+export function validateCliCallback(cliCallback: string): boolean {
+ try {
+ const cbUrl = new URL(cliCallback);
+ if (cbUrl.protocol !== "http:") return false;
+ if (cbUrl.hostname !== "localhost" && cbUrl.hostname !== "127.0.0.1")
+ return false;
+ return true;
+ } catch {
+ return false;
+ }
}
// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------
-export function LoginPage({ logo, onSuccess, google }: LoginPageProps) {
- const [step, setStep] = useState<"email" | "code">("email");
+export function LoginPage({
+ logo,
+ onSuccess,
+ google,
+ cliCallback,
+ lastWorkspaceId,
+ onTokenObtained,
+}: LoginPageProps) {
+ const [step, setStep] = useState<"email" | "code" | "cli_confirm">("email");
const [email, setEmail] = useState("");
const [code, setCode] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [cooldown, setCooldown] = useState(0);
+ const [existingUser, setExistingUser] = useState<User | null>(null);
+
+ // Check for existing session when CLI callback is present
+ useEffect(() => {
+ if (!cliCallback) return;
+ const token = localStorage.getItem("multica_token");
+ if (!token) return;
+
+ api.setToken(token);
+ api
+ .getMe()
+ .then((user) => {
+ setExistingUser(user);
+ setStep("cli_confirm");
+ })
+ .catch(() => {
+ api.setToken(null);
+ localStorage.removeItem("multica_token");
+ });
+ }, [cliCallback]);
// Cooldown timer for resend
useEffect(() => {
@@ -91,9 +154,21 @@ export function LoginPage({ logo, onSuccess, google }: LoginPageProps) {
setLoading(true);
setError("");
try {
+ if (cliCallback) {
+ // CLI path: get token directly for the redirect URL
+ const { token } = await api.verifyCode(email, value);
+ localStorage.setItem("multica_token", token);
+ api.setToken(token);
+ onTokenObtained?.();
+ redirectToCliCallback(cliCallback.url, token, cliCallback.state);
+ return;
+ }
+
+ // Normal path
await useAuthStore.getState().verifyCode(email, value);
const wsList = await api.listWorkspaces();
- useWorkspaceStore.getState().hydrateWorkspace(wsList);
+ useWorkspaceStore.getState().hydrateWorkspace(wsList, lastWorkspaceId);
+ onTokenObtained?.();
onSuccess();
} catch (err) {
setError(
@@ -103,7 +178,7 @@ export function LoginPage({ logo, onSuccess, google }: LoginPageProps) {
setLoading(false);
}
},
- [email, onSuccess],
+ [email, onSuccess, cliCallback, lastWorkspaceId, onTokenObtained],
);
const handleResend = async () => {
@@ -119,6 +194,15 @@ export function LoginPage({ logo, onSuccess, google }: LoginPageProps) {
}
};
+ const handleCliAuthorize = () => {
+ if (!cliCallback) return;
+ const token = localStorage.getItem("multica_token");
+ if (!token) return;
+ setLoading(true);
+ onTokenObtained?.();
+ redirectToCliCallback(cliCallback.url, token, cliCallback.state);
+ };
+
const handleGoogleLogin = () => {
if (!google) return;
const params = new URLSearchParams({
@@ -132,13 +216,57 @@ export function LoginPage({ logo, onSuccess, google }: LoginPageProps) {
window.location.href = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;
};
+ // -------------------------------------------------------------------------
+ // CLI confirm step
+ // -------------------------------------------------------------------------
+
+ if (step === "cli_confirm" && existingUser) {
+ return (
+ <div className="flex min-h-svh items-center justify-center">
+ <Card className="w-full max-w-sm">
+ <CardHeader className="text-center">
+ {logo && <div className="mx-auto mb-4">{logo}</div>}
+ <CardTitle className="text-2xl">Authorize CLI</CardTitle>
+ <CardDescription>
+ Allow the CLI to access Multica as{" "}
+ <span className="font-medium text-foreground">
+ {existingUser.email}
+ </span>
+ ?
+ </CardDescription>
+ </CardHeader>
+ <CardContent className="flex flex-col gap-3">
+ <Button
+ onClick={handleCliAuthorize}
+ disabled={loading}
+ className="w-full"
+ size="lg"
+ >
+ {loading ? "Authorizing..." : "Authorize"}
+ </Button>
+ <Button
+ variant="ghost"
+ className="w-full"
+ onClick={() => {
+ setExistingUser(null);
+ setStep("email");
+ }}
+ >
+ Use a different account
+ </Button>
+ </CardContent>
+ </Card>
+ </div>
+ );
+ }
+
// -------------------------------------------------------------------------
// Code verification step
// -------------------------------------------------------------------------
if (step === "code") {
return (
- <div className="flex flex-1 items-center justify-center">
+ <div className="flex min-h-svh items-center justify-center">
<Card className="w-full max-w-sm">
<CardHeader className="text-center">
{logo && <div className="mx-auto mb-4">{logo}</div>}
@@ -205,7 +333,7 @@ export function LoginPage({ logo, onSuccess, google }: LoginPageProps) {
// -------------------------------------------------------------------------
return (
- <div className="flex flex-1 items-center justify-center">
+ <div className="flex min-h-svh items-center justify-center">
<Card className="w-full max-w-sm">
<CardHeader className="text-center">
{logo && <div className="mx-auto mb-4">{logo}</div>}