);
-export const AuthOptionsLoading = () => ;
-
-const AuthPageLayout = ({ chip, title, subtitle, hideExplore, children }: Props) => {
+// The way back from a page reached off the sign-in path, such as administrator sign-in or a failed SSO return.
+export const AuthBackLink = ({ to = ROUTES.AUTH }: { to?: string }) => {
const t = useTranslate();
- const { profile } = useInstance();
- const showExplore = profile.accessMode === InstanceAccessMode.PUBLIC && !hideExplore;
-
return (
-
-
-
-
- {chip &&
{chip}
}
-
{title}
- {subtitle &&
{subtitle}
}
-
{children}
- {showExplore && (
-
-
-
- {t("auth.explore-public-memos")}
-
-
-
- )}
-
-
-
-
+
+
+
+ {t("auth.back-to-sign-in")}
+
+
);
};
+export const AuthOptionsLoading = () => ;
+
+/**
+ * Every auth page: the instance's identity, the task's heading, the task, and a footer row with the
+ * visitor's language and theme. A card on wider screens; on a phone the frame drops and the footer
+ * sits at the bottom of the screen.
+ */
+const AuthPageLayout = ({ title, description, children }: Props) => (
+
+
+
+
+ {title &&
{title}
}
+ {description &&
{description}
}
+
{children}
+
+ {/* Same side padding as the content above, so the footer's controls share its edges. */}
+
+
+
+);
+
export default AuthPageLayout;
diff --git a/web/src/components/CredentialFields.tsx b/web/src/components/CredentialFields.tsx
index 809572b2f..3745b017a 100644
--- a/web/src/components/CredentialFields.tsx
+++ b/web/src/components/CredentialFields.tsx
@@ -1,4 +1,6 @@
-import { Field, Input } from "@usememos/ui";
+import { Button, Field, Input, Tooltip } from "@usememos/ui";
+import { EyeIcon, EyeOffIcon } from "lucide-react";
+import { useState } from "react";
import { useTranslate } from "@/utils/i18n";
interface Props {
@@ -7,23 +9,35 @@ interface Props {
password: string;
passwordAutoComplete: "current-password" | "new-password";
readOnly?: boolean;
+ // Marks both fields, for a server answer that rejects the pair rather than one of them.
+ invalid?: boolean;
onUsernameChange: (username: string) => void;
onPasswordChange: (password: string) => void;
}
// Username + password field pair shared by the sign-in and sign-up forms.
-const CredentialFields = ({ idPrefix, username, password, passwordAutoComplete, readOnly, onUsernameChange, onPasswordChange }: Props) => {
+const CredentialFields = ({
+ idPrefix,
+ username,
+ password,
+ passwordAutoComplete,
+ readOnly,
+ invalid = false,
+ onUsernameChange,
+ onPasswordChange,
+}: Props) => {
const t = useTranslate();
+ const [passwordVisible, setPasswordVisible] = useState(false);
+ const visibilityLabel = passwordVisible ? t("auth.hide-password") : t("auth.show-password");
return (
<>
-
+ {t("common.username")}
-
+ {t("common.password")}
onPasswordChange(e.target.value)}
required
+ suffix={
+
+ : }
+ aria-label={visibilityLabel}
+ aria-pressed={passwordVisible}
+ onClick={() => setPasswordVisible((visible) => !visible)}
+ />
+ }
+ />
+ {visibilityLabel}
+
+ }
/>
>
diff --git a/web/src/components/LocalePicker.tsx b/web/src/components/LocalePicker.tsx
index 72607f8b0..f8c3af54e 100644
--- a/web/src/components/LocalePicker.tsx
+++ b/web/src/components/LocalePicker.tsx
@@ -1,6 +1,6 @@
-import { Combobox, Link } from "@usememos/ui";
-import { GlobeIcon } from "lucide-react";
-import { useCallback } from "react";
+import { Button, Combobox, Link, Popover } from "@usememos/ui";
+import { ChevronsUpDownIcon, GlobeIcon } from "lucide-react";
+import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { locales } from "@/i18n";
import { getLocaleDisplayName, localeMatchesSearch, useTranslate } from "@/utils/i18n";
@@ -25,6 +25,36 @@ const localeLabel = (locale: string): string => {
return label;
};
+// A stable filter keeps the Combobox from re-filtering every locale on each render.
+const useLocaleFilter = () => {
+ const { i18n } = useTranslation();
+ return useCallback((locale: string, query: string) => localeMatchesSearch(locale, query, i18n.language), [i18n.language]);
+};
+
+/** The list half of both pickers: the matching locales, or a link to ask for a missing one. */
+const LocaleOptions = () => {
+ const t = useTranslate();
+ return (
+ <>
+
+
+ {t("locale-picker.no-language-feedback")}
+
+
+
+ {(locale: Locale) => (
+
+
+ {localeLabel(locale)}
+ {locale}
+
+
+ )}
+
+ >
+ );
+};
+
/**
* Picks the interface language from the long list of locales: a Combobox, so typing a
* language's name in English, in itself or in the interface language finds it. When nothing
@@ -32,9 +62,7 @@ const localeLabel = (locale: string): string => {
*/
const LocalePicker = ({ value, onChange, className }: LocalePickerProps) => {
const t = useTranslate();
- const { i18n } = useTranslation();
- // A stable filter keeps the Combobox from re-filtering every locale on each render.
- const filter = useCallback((locale: string, query: string) => localeMatchesSearch(locale, query, i18n.language), [i18n.language]);
+ const filter = useLocaleFilter();
return (
{
className={className}
/>
-
-
- {t("locale-picker.no-language-feedback")}
-
-
-
- {(locale: Locale) => (
-
-
- {localeLabel(locale)}
- {locale}
-
-
- )}
-
+
);
};
+/**
+ * The same picker behind a quiet ghost button that names the current language, for a row of
+ * page-level settings such as the auth card's footer. The search and list open in a popover.
+ */
+export const LocalePickerButton = ({ value, onChange }: Omit) => {
+ const t = useTranslate();
+ const filter = useLocaleFilter();
+ const [open, setOpen] = useState(false);
+ const searchLabel = `${t("common.search")} ${t("common.language").toLowerCase()}`;
+
+ return (
+
+ } iconEnd={} aria-label={t("common.language")}>
+ {localeLabel(value)}
+
+ }
+ />
+
+ {
+ if (!next) return;
+ onChange(next as Locale);
+ setOpen(false);
+ }}
+ itemToStringLabel={localeLabel}
+ filter={filter}
+ >
+
+
+
+
+
+ );
+};
+
export default LocalePicker;
diff --git a/web/src/components/PasswordSignInForm.tsx b/web/src/components/PasswordSignInForm.tsx
index 0ae60f2f2..626ea4201 100644
--- a/web/src/components/PasswordSignInForm.tsx
+++ b/web/src/components/PasswordSignInForm.tsx
@@ -1,15 +1,16 @@
import { timestampDate } from "@bufbuild/protobuf/wkt";
+import { Code } from "@connectrpc/connect";
import { Button } from "@usememos/ui";
import { useState } from "react";
import { setAccessToken } from "@/auth-state";
+import AuthNotice from "@/components/AuthNotice";
import ChallengeWidget, { CHALLENGE_TOKEN_HEADER } from "@/components/ChallengeWidget";
import CredentialFields from "@/components/CredentialFields";
import { authServiceClient } from "@/connect";
import { useAuth } from "@/contexts/AuthContext";
import useLoading from "@/hooks/useLoading";
import useNavigateTo from "@/hooks/useNavigateTo";
-import { ERROR_REASON_CHALLENGE_REQUIRED, handleError, hasErrorReason } from "@/lib/error";
-import { showErrorToast } from "@/lib/toast";
+import { ERROR_REASON_CHALLENGE_REQUIRED, getErrorMessage, hasConnectCode, hasErrorReason } from "@/lib/error";
import { ROUTES } from "@/router/routes";
import { useTranslate } from "@/utils/i18n";
@@ -17,6 +18,12 @@ interface PasswordSignInFormProps {
redirectPath?: string;
}
+interface SignInError {
+ message: string;
+ // The server rejected the username and password themselves, so both fields are marked.
+ credentials: boolean;
+}
+
function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
const t = useTranslate();
const navigateTo = useNavigateTo();
@@ -24,6 +31,7 @@ function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
const actionBtnLoadingState = useLoading(false);
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
+ const [error, setError] = useState();
const [challengeToken, setChallengeToken] = useState(null);
const [challengeResetKey, setChallengeResetKey] = useState(0);
@@ -40,6 +48,7 @@ function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
try {
actionBtnLoadingState.setLoading();
+ setError(undefined);
const response = await authServiceClient.signIn(
{
credentials: {
@@ -55,17 +64,25 @@ function PasswordSignInForm({ redirectPath }: PasswordSignInFormProps) {
}
await initialize();
navigateTo(redirectPath || ROUTES.HOME, { replace: true });
- } catch (error: unknown) {
- if (hasErrorReason(error, ERROR_REASON_CHALLENGE_REQUIRED)) {
+ } catch (err: unknown) {
+ console.error(err);
+ if (hasErrorReason(err, ERROR_REASON_CHALLENGE_REQUIRED)) {
setChallengeResetKey((key) => key + 1);
+ setError({ message: getErrorMessage(err), credentials: false });
+ } else if (hasConnectCode(err, Code.InvalidArgument)) {
+ // The server answers an unknown user and a wrong password alike, so the message names neither.
+ setError({ message: t("auth.incorrect-credentials"), credentials: true });
+ } else {
+ setError({ message: getErrorMessage(err, t("error.sign-in-failed")), credentials: false });
}
- handleError(error, showErrorToast, {
- fallbackMessage: t("error.sign-in-failed"),
- });
}
actionBtnLoadingState.setFinish();
};
+ const clearCredentialError = () => {
+ if (error?.credentials) setError(undefined);
+ };
+
return (