Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

21.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { useAppShellReadySignal } from "@/components/app-readiness";
import { apiUrl, isTauri } from "@/lib/api-base";
import { normalizeAccountUsername, transitionBrowserAccount } from "@/lib/account-transition";
import { sessionAccount, useLoginMode } from "../account-session";
import { fetchAuthStatus, loginFromForm, loginWithPassword, setLoginMode, type TokenResponse } from "../login-client";
import { Button } from "@/components/ui/button";
import { MascotImg } from "@/components/mascot-img";
import { Input } from "@/components/ui/input";
import { useT } from "@/i18n";
import { Label } from "@/components/ui/label";
import { Link, useNavigate } from "@tanstack/react-router";
import { useEffect, useRef, useState } from "react";
import type { ReactElement } from "react";
import type { SyntheticEvent } from "react";
import { refreshSession, refreshSessionOutcome } from "../api";
import { clearDesktopSignedOut, continueAsDesktopOwner } from "../tauri-auto-auth";
import {
  deadlineFromStatus,
  formatCountdown,
  hasExpired,
} from "../bootstrap-deadline";

// Injected into index.html by the backend while the default admin must change its password.
declare global {
  interface Window {
    __UNSLOTH_BOOTSTRAP__?: { username: string; password: string };
  }
}

import {
  clearAuthTokens,
  getAuthToken,
  getPostAuthRoute,
  hasAuthToken,
  hasRefreshToken,
  mustChangePassword,
  setMustChangePassword,
  storeAuthTokens,
} from "../session";
import { ViewIcon, ViewOffSlashIcon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";

type AuthMode = "login" | "change-password";

type AuthFormProps = {
  mode: AuthMode;
};

const HIDDEN_LOGIN_USERNAME = "unsloth";

export function AuthForm({ mode }: AuthFormProps): ReactElement | null {
  const signalReady = useAppShellReadySignal();
  const t = useT();
  const navigate = useNavigate();
  const isLoginMode = mode === "login";
  const [showPassword, setShowPassword] = useState(false);
  const [showNewPassword, setShowNewPassword] = useState(false);
  const loginMode = useLoginMode();
  const [enteredUsername, setEnteredUsername] = useState("");
  const username = loginMode === "multi" ? enteredUsername : HIDDEN_LOGIN_USERNAME;
  const [password, setPassword] = useState("");
  const [newPassword, setNewPassword] = useState("");
  const [confirmPassword, setConfirmPassword] = useState("");
  const [loading, setLoading] = useState(false);
  const [statusLoading, setStatusLoading] = useState(true);
  const [initialized, setInitialized] = useState<boolean | null>(null);
  const [requiresPasswordChange, setRequiresPasswordChange] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [deadlineAt, setDeadlineAt] = useState<number | null>(null);
  const [nowMs, setNowMs] = useState<number>(() => Date.now());
  const reloadReadySent = useRef(false);
  // Keep the issued session so retrying a blocked cleanup does not consume the one-use setup code twice.
  const pendingLogin = useRef<{
    username: string;
    password: string;
    token: TokenResponse;
  } | null>(null);

  useEffect(() => {
    if (deadlineAt === null) {
      return;
    }
    const id = window.setInterval(() => setNowMs(Date.now()), 1000);
    return () => window.clearInterval(id);
  }, [deadlineAt]);

  useEffect(() => {
    let canceled = false;

    async function initializeAuthForm(): Promise<void> {
      // Server first: localStorage flags can be stale (e.g. tokens from a previous install).
      try {
        const result = await fetchAuthStatus();
        const requiresChange = result.login_mode === "multi" || isTauri
          ? hasAuthToken() && mustChangePassword()
          : result.requires_password_change;
        if (!canceled) {
          setInitialized(result.initialized);
          setRequiresPasswordChange(requiresChange);
          // One clock sample, or the request duration skews the countdown.
          const sampledNow = Date.now();
          setNowMs(sampledNow);
          setDeadlineAt(
            deadlineFromStatus(result.bootstrap_deadline_seconds, sampledNow),
          );

          if (requiresChange !== mustChangePassword()) {
            setMustChangePassword(requiresChange);
          }

          if (mode === "login" && requiresChange) {
            navigate({ to: "/change-password" });
            return;
          }
          if (mode === "change-password" && !requiresChange) {
            navigate({ to: "/login" });
            return;
          }

          if (isLoginMode && !requiresChange) {
            if (hasRefreshToken()) {
              const refreshed = await refreshSession();
              if (refreshed) {
                if (!canceled) setStatusLoading(false);
                navigate({ to: getPostAuthRoute() });
                return;
              }
            }
            if (hasAuthToken()) {
              if (!canceled) setStatusLoading(false);
              navigate({ to: getPostAuthRoute() });
              return;
            }
          }
        }
      } catch (err: unknown) {
        if (!canceled) {
          setError(err instanceof Error ? err.message : t("auth.loadFailed"));
        }
      } finally {
        if (!canceled) setStatusLoading(false);
      }
    }

    void initializeAuthForm();

    return () => {
      canceled = true;
    };
  }, [navigate]);

  useEffect(() => {
    if (statusLoading || reloadReadySent.current) return;
    reloadReadySent.current = true;
    signalReady();
  }, [statusLoading, signalReady]);

  useEffect(() => {
    function loadBootstrap() {
      const bootstrap = window.__UNSLOTH_BOOTSTRAP__;
      if (bootstrap && !isLoginMode && !password) {
        setPassword(bootstrap.password);
      }
    }
    loadBootstrap();
  }, []);

  const blockedByState =
    initialized === false ||
    (mode === "login" && requiresPasswordChange) ||
    (mode === "change-password" && !requiresPasswordChange);

  let helperText: string | null = null;
  if (initialized === false) {
    helperText = t("auth.bootstrapping");
  } else if (isLoginMode && requiresPasswordChange) {
    helperText = t("auth.signInToChange");
  } else if (!isLoginMode && !requiresPasswordChange) {
    helperText = t("auth.alreadyUpdated");
  }
  const title = isLoginMode ? t("auth.welcomeBack") : t("auth.setupTitle");
  const subtitle = isLoginMode ? t("auth.loginSubtitle") : t("auth.setupSubtitle");
  const submitLabel = isLoginMode ? t("auth.login") : t("auth.changePassword");
  const showSwitchLink = !isLoginMode;
  const switchText = t("auth.alreadySetUp");
  const switchLinkTo = "/login";
  const switchLinkText = t("auth.backToLogin");
  const currentPassword = password || window.__UNSLOTH_BOOTSTRAP__?.password || "";
  // First boot reuses the injected bootstrap password; the Current password field is only for the
  // admin-forced change path.
  const hasBootstrapPassword = Boolean(window.__UNSLOTH_BOOTSTRAP__?.password);
  const changingFromSetupCode =
    !isLoginMode && sessionAccount(getAuthToken())?.isOwner === false;
  const passwordManagerUsername =
    isLoginMode && loginMode === "multi"
      ? null
      : sessionAccount(getAuthToken())?.username || username || null;
  const invalidChangePasswordForm =
    !isLoginMode &&
    (currentPassword.length < 8 ||
      newPassword.length < 8 ||
      /\s/.test(newPassword) ||
      newPassword !== confirmPassword ||
      currentPassword === newPassword);
  const showWhitespaceWarning = !isLoginMode && /\s/.test(newPassword);
  const showPasswordMismatchWarning =
    !isLoginMode &&
    newPassword.length > 0 &&
    confirmPassword.length > 0 &&
    newPassword !== confirmPassword;

  async function handleSubmit(event: SyntheticEvent<HTMLFormElement>) {
    event.preventDefault();
    setError(null);

    if (!isLoginMode) {
      // Mirror the disable gate: Enter / autofill can bypass the button.
      if (currentPassword.length < 8) {
        setError(
          currentPassword
            ? t("auth.currentTooShort")
            : t("auth.setupInitFailed"),
        );
        return;
      }
      if (newPassword.length < 8) {
        setError(t("auth.newTooShort"));
        return;
      }
      if (/\s/.test(newPassword)) {
        setError(t("auth.noSpaces"));
        return;
      }
      if (newPassword !== confirmPassword) {
        setError(t("auth.mismatch"));
        return;
      }
      if (currentPassword === newPassword) {
        setError(t("auth.mustDiffer"));
        return;
      }
    }

    setLoading(true);
    try {
      let token: TokenResponse;

      if (isLoginMode) {
        const normalizedUsername = normalizeAccountUsername(username);
        const pending = pendingLogin.current;
        const result = pending?.username === normalizedUsername && pending.password === password
          ? pending.token
          : await loginFromForm(loginMode, username, password);
        if (!result) {
          setPassword("");
          return;
        }
        pendingLogin.current = { username: normalizedUsername, password, token: result };
        token = result;
      } else {
        let accessToken = getAuthToken();

        if (hasRefreshToken()) {
          const outcome = await refreshSessionOutcome();
          if (outcome === "unavailable") {
            throw new Error(t("auth.busy"));
          }
          accessToken = getAuthToken();
          if (outcome === "rejected") {
            clearAuthTokens();
            accessToken = null;
          }
        }

        if (!accessToken) {
          const bootstrapToken = await loginWithPassword(username, currentPassword);
          const replaced = await transitionBrowserAccount(
            { username, accountId: bootstrapToken.account_id },
            "/change-password",
            () => {
              storeAuthTokens(bootstrapToken.access_token, bootstrapToken.refresh_token);
              setMustChangePassword(bootstrapToken.must_change_password);
            },
          );
          if (replaced) return;
          accessToken = bootstrapToken.access_token;
        }

        const response = await fetch(apiUrl("/api/auth/change-password"), {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${accessToken}`,
          },
          body: JSON.stringify({
            current_password: currentPassword,
            new_password: newPassword,
          }),
        });

        if (!response.ok) {
          let message = t("auth.updateFailed");
          const errorPayload = (await response
            .json()
            .catch(() => null)) as { detail?: string } | null;
          if (errorPayload?.detail) message = errorPayload.detail;
          throw new Error(message);
        }

        token = (await response.json()) as TokenResponse;
      }

      // A failed clear only shows login on the next launch.
      if (isTauri) await clearDesktopSignedOut().catch(() => undefined);
      const finishSession = () => {
        setLoginMode(loginMode);
        if (!isLoginMode) setRequiresPasswordChange(false);
        setMustChangePassword(isLoginMode && token.must_change_password);
        storeAuthTokens(token.access_token, token.refresh_token);
      };
      // The transition keys on the immutable account id: a recreated username must not inherit the old browser data.
      const signedInUsername = sessionAccount(token.access_token)?.username ?? username;
      const route = isLoginMode && token.must_change_password ? "/change-password" : "/chat";
      const replaced = await transitionBrowserAccount(
        { username: signedInUsername, accountId: token.account_id },
        route,
        finishSession,
      );
      pendingLogin.current = null;
      if (replaced) return;
      navigate({ to: getPostAuthRoute() });
    } catch (err: unknown) {
      // Show the backend's PATH-based command as-is; a relative Windows path only resolves in the
      // Unsloth home dir.
      const msg = err instanceof Error ? err.message : t("auth.failed");
      setError(msg);
    } finally {
      setLoading(false);
    }
  }

  async function handleContinueAsOwner() {
    setError(null);
    setLoading(true);
    try {
      if (await continueAsDesktopOwner()) {
        navigate({ to: getPostAuthRoute() });
      } else {
        setError(t("auth.signInWithUsername"));
      }
    } finally {
      setLoading(false);
    }
  }

  if (statusLoading && initialized === null && error === null) return null;

  return (
    <div className="w-full max-w-sm space-y-6">
      <div className="space-y-1.5 text-center">
        <MascotImg
          src="Sloth emojis/large sloth wave.png"
          className="mx-auto mb-2 h-20 w-20 object-contain"
        />
        <h2 className="text-[calc(1.5rem*var(--ui-font-scale,1))] font-semibold text-foreground">{title}</h2>
        <p className="text-muted-foreground">{subtitle}</p>
      </div>
      {/* Not a live region: it re-renders every second, so it would be read aloud on every tick. */}
      {deadlineAt !== null && (
        <p className="rounded-lg border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-center text-sm text-amber-600">
          {hasExpired(deadlineAt - nowMs) ? (
            t("auth.shuttingDown")
          ) : (
            t("auth.shutdownCountdown", { time: formatCountdown(deadlineAt - nowMs) })
          )}
        </p>
      )}
      <form className="space-y-5" onSubmit={handleSubmit}>
        {passwordManagerUsername && (
          <input
            type="text"
            name="username"
            autoComplete="username"
            value={passwordManagerUsername}
            readOnly
            tabIndex={-1}
            aria-hidden="true"
            className="sr-only"
          />
        )}
        {isLoginMode && loginMode === "multi" && (
          <div className="space-y-2">
            <Label htmlFor="username">{t("auth.username")}</Label>
            <Input
              id="username"
              name="username"
              autoComplete="username"
              autoCapitalize="none"
              spellCheck={false}
              value={enteredUsername}
              onChange={(event) => setEnteredUsername(event.target.value)}
              required
            />
            <p id="setup-code-hint" className="text-sm text-muted-foreground">
              {t("auth.setupCodeHint")}
            </p>
            <p className="text-sm text-muted-foreground">
              {t("auth.ownerHint")}
            </p>
          </div>
        )}
        {isLoginMode && (
          <div className="space-y-2">
            <Label htmlFor="password">{t("auth.password")}</Label>
            <div className="relative">
              <Input
                id="password"
                aria-describedby={loginMode === "multi" ? "setup-code-hint" : undefined}
                type={showPassword ? "text" : "password"}
                className="pr-10"
                autoComplete="current-password"
                value={password}
                onChange={(event) => setPassword(event.target.value)}
                minLength={8}
                required
              />
              <Button
                type="button"
                variant="ghost"
                size="icon"
                className="absolute right-0 top-0 h-full px-3 text-muted-foreground hover:bg-transparent"
                onClick={() => setShowPassword((prev) => !prev)}
              >
                {showPassword ? (
                  <HugeiconsIcon icon={ViewOffSlashIcon} className="h-4 w-4" />
                ) : (
                  <HugeiconsIcon icon={ViewIcon} className="h-4 w-4" />
                )}
              </Button>
            </div>
          </div>
        )}

        {!isLoginMode && (
          <>
            {!hasBootstrapPassword && (
              <div className="space-y-2">
                <Label htmlFor="current-password">{t("auth.currentPassword")}</Label>
                <div className="relative">
                  <Input
                    id="current-password"
                    aria-describedby={
                      changingFromSetupCode ? "current-setup-code-hint" : undefined
                    }
                    type={showPassword ? "text" : "password"}
                    className="pr-10"
                    autoComplete="current-password"
                    value={password}
                    onChange={(event) => setPassword(event.target.value)}
                    minLength={8}
                    required
                  />
                  <Button
                    type="button"
                    variant="ghost"
                    size="icon"
                    className="absolute right-0 top-0 h-full px-3 text-muted-foreground hover:bg-transparent"
                    onClick={() => setShowPassword((prev) => !prev)}
                  >
                    {showPassword ? (
                      <HugeiconsIcon icon={ViewOffSlashIcon} className="h-4 w-4" />
                    ) : (
                      <HugeiconsIcon icon={ViewIcon} className="h-4 w-4" />
                    )}
                  </Button>
                </div>
                {changingFromSetupCode && (
                  <p
                    id="current-setup-code-hint"
                    className="text-sm text-muted-foreground"
                  >
                    {t("auth.currentSetupCodeHint")}
                  </p>
                )}
              </div>
            )}
            <div className="space-y-2">
              <Label htmlFor="new-password">{t("auth.newPassword")}</Label>
              <div className="relative">
                <Input
                  id="new-password"
                  type={showNewPassword ? "text" : "password"}
                  className="pr-10"
                  autoComplete="new-password"
                  value={newPassword}
                  onChange={(event) => setNewPassword(event.target.value)}
                  minLength={8}
                  required
                />
                <Button
                  type="button"
                  variant="ghost"
                  size="icon"
                  className="absolute right-0 top-0 h-full px-3 text-muted-foreground hover:bg-transparent"
                  onClick={() => setShowNewPassword((prev) => !prev)}
                >
                  {showNewPassword ? (
                    <HugeiconsIcon icon={ViewOffSlashIcon} className="h-4 w-4" />
                  ) : (
                    <HugeiconsIcon icon={ViewIcon} className="h-4 w-4" />
                  )}
                </Button>
              </div>
            </div>
            <div className="space-y-2">
              <Label htmlFor="confirm-password">{t("auth.confirmPassword")}</Label>
              <Input
                id="confirm-password"
                type="password"
                autoComplete="new-password"
                value={confirmPassword}
                onChange={(event) => setConfirmPassword(event.target.value)}
                minLength={8}
                required
              />
            </div>
            <p
              className={`min-h-4 text-xs ${
                showWhitespaceWarning || showPasswordMismatchWarning
                  ? "text-destructive"
                  : "text-muted-foreground"
              }`}
              aria-live="polite"
            >
              {showWhitespaceWarning
                ? t("auth.noSpaces")
                : showPasswordMismatchWarning
                  ? t("auth.ensureMatch")
                  : t("auth.minLength")}
            </p>
          </>
        )}

        {helperText && (
          <p className="text-center text-sm text-amber-600">{helperText}</p>
        )}
        {error && (
          <p className="text-center text-sm text-destructive [overflow-wrap:anywhere]">
            {error}
          </p>
        )}

        <Button
          variant="primary"
          type="submit"
          size="hero"
          className="mx-auto flex h-[2.967em] w-fit bg-control-accent px-[1.56em] pt-0 pb-[0.07em] [font-family:var(--font-heading)] text-[0.875rem] font-semibold tracking-[0.02em] text-control-accent-foreground hover:bg-control-accent/85"
          disabled={
            loading ||
            statusLoading ||
            blockedByState ||
            (isLoginMode && (password.length < 8 || (loginMode === "multi" && !username.trim()))) ||
            invalidChangePasswordForm
          }
        >
          {loading ? t("auth.pleaseWait") : submitLabel}
        </Button>
        {/* Single mode only: there the shell secret proves the owner, who may never have set a password. */}
        {isTauri && isLoginMode && loginMode === "single" && (
          <Button
            type="button"
            variant="ghost"
            className="mx-auto flex w-fit"
            disabled={loading || statusLoading}
            onClick={() => void handleContinueAsOwner()}
          >
            {t("auth.continueAsOwner")}
          </Button>
        )}
      </form>

      {showSwitchLink && (
        <p className="text-center text-sm text-muted-foreground">
          {switchText}
          <Link to={switchLinkTo} className="text-primary hover:underline">
            {switchLinkText}
          </Link>
        </p>
      )}
    </div>
  );
}