Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

update(photos):change-password-and-public-reset

Aswin Asok2e129420acf9
diff --git a/web/apps/photos/src/components/auth/SetPasswordForm.tsx b/web/apps/photos/src/components/auth/SetPasswordForm.tsx
new file mode 100644
index 0000000000..7cfcbdedd8
--- /dev/null
+++ b/web/apps/photos/src/components/auth/SetPasswordForm.tsx
@@ -0,0 +1,130 @@
+import { styled } from "@mui/material";
+import type { NewPasswordPresentationProps } from "ente-accounts/components/NewPasswordForm";
+import { pt } from "ente-base/i18n";
+import { t } from "i18next";
+import type React from "react";
+import { Button } from "./Button";
+import { Form } from "./Form";
+import { FormFields } from "./FormFields";
+import { FormFooter } from "./FormFooter";
+import { Message, type MessageKind } from "./Message";
+import { ScreenHeader } from "./ScreenHeader";
+import { TextField } from "./TextField";
+import { TextLink } from "./TextLink";
+
+export function SetPasswordForm({
+ userEmail,
+ password,
+ confirmPassword,
+ passwordError,
+ confirmPasswordError,
+ passwordStrength,
+ isSubmitting,
+ isSubmitDisabled,
+ submitButtonTitle,
+ onBack,
+ onPasswordChange,
+ onConfirmPasswordChange,
+ onSubmit,
+}: NewPasswordPresentationProps): React.JSX.Element {
+ const passwordStrengthKind: MessageKind =
+ passwordStrength === "weak"
+ ? "error"
+ : passwordStrength === "moderate"
+ ? "warning"
+ : "success";
+
+ return (
+ <>
+ <ScreenHeader
+ title={submitButtonTitle}
+ subtitle={pt(
+ "We don't store your password. If you forget it, the only way back to your photos is your recovery key.",
+ )}
+ />
+ <Form onSubmit={onSubmit}>
+ <HiddenEmail
+ name="email"
+ type="email"
+ autoComplete="username"
+ value={userEmail}
+ readOnly
+ tabIndex={-1}
+ />
+ <FormFields>
+ <PasswordField>
+ <TextField
+ name="password"
+ label={t("password")}
+ placeholder={pt("Choose a strong password")}
+ autoComplete="new-password"
+ showPasswordToggle
+ value={password}
+ onChange={onPasswordChange}
+ error={Boolean(passwordError)}
+ helperText={passwordError}
+ disabled={isSubmitting}
+ autoFocus
+ />
+ {password ? (
+ <Message kind={passwordStrengthKind}>
+ {t("password_strength", {
+ context: passwordStrength,
+ })}
+ </Message>
+ ) : null}
+ </PasswordField>
+ <TextField
+ name="confirmPassword"
+ label={t("confirm_password")}
+ placeholder={pt("Type it once more")}
+ autoComplete="new-password"
+ showPasswordToggle
+ value={confirmPassword}
+ onChange={onConfirmPasswordChange}
+ error={Boolean(confirmPasswordError)}
+ helperText={confirmPasswordError}
+ disabled={isSubmitting}
+ />
+ </FormFields>
+ <FormFooter>
+ <Button
+ fullWidth
+ type="submit"
+ loading={isSubmitting}
+ disabled={isSubmitDisabled}
+ >
+ {submitButtonTitle}
+ </Button>
+ {isSubmitting ? (
+ <SubmittingMessage>
+ <Message>{t("key_generation_in_progress")}</Message>
+ </SubmittingMessage>
+ ) : null}
+ {onBack ? (
+ <BackAction>
+ <TextLink onClick={onBack} disabled={isSubmitting}>
+ {t("go_back")}
+ </TextLink>
+ </BackAction>
+ ) : null}
+ </FormFooter>
+ </Form>
+ </>
+ );
+}
+
+const HiddenEmail = styled("input")({ display: "none" });
+
+const PasswordField = styled("div")({
+ display: "flex",
+ flexDirection: "column",
+ gap: "8px",
+});
+
+const SubmittingMessage = styled("div")({
+ display: "flex",
+ justifyContent: "center",
+});
+
+const BackAction = styled("div")({ display: "flex", justifyContent: "center" });
diff --git a/web/apps/photos/src/pages/change-password.tsx b/web/apps/photos/src/pages/change-password.tsx
index 0e494377ee..1eefb2f8c8 100644
--- a/web/apps/photos/src/pages/change-password.tsx
+++ b/web/apps/photos/src/pages/change-password.tsx
@@ -1 +1,30 @@
-export { default } from "ente-accounts/pages/change-password";
+import { SetPasswordForm } from "@/components/auth/SetPasswordForm";
+import { PhotosAuthShell } from "@/components/PhotosAuthShell";
+import { featureFlags } from "@/featureFlags";
+import type { NewPasswordPresentationProps } from "ente-accounts/components/NewPasswordForm";
+import AccountsChangePasswordPage from "ente-accounts/pages/change-password";
+import type React from "react";
+
+function ResetPasswordPresentation(
+ props: NewPasswordPresentationProps,
+): React.JSX.Element {
+ return (
+ <PhotosAuthShell>
+ <SetPasswordForm {...props} />
+ </PhotosAuthShell>
+ );
+}
+
+function ChangePasswordPage(): React.JSX.Element {
+ if (!featureFlags.enableNewPhotosAuthFlow) {
+ return <AccountsChangePasswordPage />;
+ }
+
+ return (
+ <AccountsChangePasswordPage
+ resetPresentation={ResetPasswordPresentation}
+ />
+ );
+}
+
+export default ChangePasswordPage;
diff --git a/web/apps/photos/src/pages/generate.tsx b/web/apps/photos/src/pages/generate.tsx
index ded9ea1457..fdf9f9b082 100644
--- a/web/apps/photos/src/pages/generate.tsx
+++ b/web/apps/photos/src/pages/generate.tsx
@@ -1,6 +1,8 @@
import { RecoveryKeyForm } from "@/components/auth/RecoveryKeyForm";
+import { SetPasswordForm } from "@/components/auth/SetPasswordForm";
import { PhotosAuthShell } from "@/components/PhotosAuthShell";
import { featureFlags } from "@/featureFlags";
+import type { NewPasswordPresentationProps } from "ente-accounts/components/NewPasswordForm";
import type { RecoveryKeyPresentationProps } from "ente-accounts/components/RecoveryKey";
import AccountsGeneratePage from "ente-accounts/pages/generate";
import type React from "react";
@@ -15,6 +17,16 @@ function RecoveryKeyPresentation(
);
}
+function SetPasswordPresentation(
+ props: NewPasswordPresentationProps,
+): React.JSX.Element {
+ return (
+ <PhotosAuthShell>
+ <SetPasswordForm {...props} />
+ </PhotosAuthShell>
+ );
+}
+
function GeneratePage(): React.JSX.Element {
if (!featureFlags.enableNewPhotosAuthFlow) {
return <AccountsGeneratePage />;
@@ -22,6 +34,7 @@ function GeneratePage(): React.JSX.Element {
return (
<AccountsGeneratePage
+ passwordPresentation={SetPasswordPresentation}
recoveryKeyPresentation={RecoveryKeyPresentation}
/>
);
diff --git a/web/packages/accounts/components/NewPasswordForm.tsx b/web/packages/accounts/components/NewPasswordForm.tsx
index 561fbcbb5d..cc70962f76 100644
--- a/web/packages/accounts/components/NewPasswordForm.tsx
+++ b/web/packages/accounts/components/NewPasswordForm.tsx
@@ -1,26 +1,53 @@
import { Input, TextField, Typography } from "@mui/material";
-import { isWeakPassword } from "ente-accounts/utils/password";
+import {
+ estimatePasswordStrength,
+ type PasswordStrength,
+} from "ente-accounts/utils/password";
import { LoadingButton } from "ente-base/components/mui/LoadingButton";
import { ShowHidePasswordInputAdornment } from "ente-base/components/mui/PasswordInputAdornment";
import log from "ente-base/log";
import { useFormik } from "formik";
import { t } from "i18next";
-import { useCallback, useState } from "react";
+import { useCallback, useMemo, useState, type ComponentType } from "react";
import { Trans } from "react-i18next";
import { PasswordStrengthHint } from "./PasswordStrength";
export interface NewPasswordFormProps {
userEmail: string;
submitButtonTitle: string;
+ presentation?: ComponentType<NewPasswordPresentationProps>;
+ onBack?: () => void;
onSubmit: (
password: string,
setPasswordsFieldError: (message: string) => void,
) => Promise<void>;
}
+export interface NewPasswordPresentationProps {
+ userEmail: string;
+ password: string;
+ confirmPassword: string;
+ passwordError: string | undefined;
+ confirmPasswordError: string | undefined;
+ passwordStrength: PasswordStrength;
+ isSubmitting: boolean;
+ isSubmitDisabled: boolean;
+ submitButtonTitle: string;
+ onBack: (() => void) | undefined;
+ onPasswordChange: React.ChangeEventHandler<
+ HTMLInputElement | HTMLTextAreaElement
+ >;
+ onConfirmPasswordChange: React.ChangeEventHandler<
+ HTMLInputElement | HTMLTextAreaElement
+ >;
+ onSubmit: React.SubmitEventHandler<HTMLFormElement>;
+}
+
export const NewPasswordForm: React.FC<NewPasswordFormProps> = ({
userEmail,
submitButtonTitle,
+ presentation: Presentation,
+ onBack,
onSubmit,
}) => {
const [showPassword, setShowPassword] = useState(false);
@@ -60,6 +87,31 @@ export const NewPasswordForm: React.FC<NewPasswordFormProps> = ({
},
});
+ const passwordStrength = useMemo(
+ () => estimatePasswordStrength(formik.values.password),
+ [formik.values.password],
+ );
+
+ if (Presentation) {
+ return (
+ <Presentation
+ userEmail={userEmail}
+ password={formik.values.password}
+ confirmPassword={formik.values.confirmPassword}
+ passwordError={formik.errors.password}
+ confirmPasswordError={formik.errors.confirmPassword}
+ passwordStrength={passwordStrength}
+ isSubmitting={formik.isSubmitting}
+ isSubmitDisabled={passwordStrength == "weak"}
+ submitButtonTitle={submitButtonTitle}
+ onBack={onBack}
+ onPasswordChange={formik.handleChange}
+ onConfirmPasswordChange={formik.handleChange}
+ onSubmit={formik.handleSubmit}
+ />
+ );
+ }
+
return (
<form onSubmit={formik.handleSubmit}>
<Typography variant="small" sx={{ mb: 2, color: "text.muted" }}>
@@ -118,7 +170,10 @@ export const NewPasswordForm: React.FC<NewPasswordFormProps> = ({
},
}}
/>
- <PasswordStrengthHint password={formik.values.password} />
+ <PasswordStrengthHint
+ password={formik.values.password}
+ strength={passwordStrength}
+ />
<Typography
variant="small"
@@ -131,7 +186,7 @@ export const NewPasswordForm: React.FC<NewPasswordFormProps> = ({
color="accent"
type="submit"
loading={formik.isSubmitting}
- disabled={isWeakPassword(formik.values.password)}
+ disabled={passwordStrength == "weak"}
fullWidth
>
{submitButtonTitle}
diff --git a/web/packages/accounts/components/PasswordStrength.tsx b/web/packages/accounts/components/PasswordStrength.tsx
index 5b106a9a05..7eaa3b0879 100644
--- a/web/packages/accounts/components/PasswordStrength.tsx
+++ b/web/packages/accounts/components/PasswordStrength.tsx
@@ -1,18 +1,23 @@
import { Typography, useTheme } from "@mui/material";
import { t } from "i18next";
import React, { useMemo } from "react";
-import { estimatePasswordStrength } from "../utils/password";
+import {
+ estimatePasswordStrength,
+ type PasswordStrength,
+} from "../utils/password";
interface PasswordStrengthHintProps {
password: string;
+ strength?: PasswordStrength;
}
export const PasswordStrengthHint: React.FC<PasswordStrengthHintProps> = ({
password,
+ strength,
}) => {
const passwordStrength = useMemo(
- () => estimatePasswordStrength(password),
- [password],
+ () => strength ?? estimatePasswordStrength(password),
+ [password, strength],
);
const theme = useTheme();
diff --git a/web/packages/accounts/pages/change-password.tsx b/web/packages/accounts/pages/change-password.tsx
index 709442dd0f..826d556184 100644
--- a/web/packages/accounts/pages/change-password.tsx
+++ b/web/packages/accounts/pages/change-password.tsx
@@ -12,19 +12,29 @@ import { LoadingIndicator } from "ente-base/components/loaders";
import log from "ente-base/log";
import { t } from "i18next";
import { useRouter } from "next/router";
-import React, { useCallback, useEffect, useState } from "react";
+import React, {
+ useCallback,
+ useEffect,
+ useState,
+ type ComponentType,
+} from "react";
import {
NewPasswordForm,
type NewPasswordFormProps,
+ type NewPasswordPresentationProps,
} from "../components/NewPasswordForm";
import { savedLocalUser } from "../services/accounts-db";
-const Page: React.FC = () => {
+export interface ChangePasswordPageProps {
+ resetPresentation?: ComponentType<NewPasswordPresentationProps>;
+}
+
+const Page: React.FC<ChangePasswordPageProps> = ({ resetPresentation }) => {
const [user, setUser] = useState<LocalUser | undefined>(undefined);
const router = useRouter();
- const isReset = router.query.op == "reset";
+ const isReset = router.isReady && router.query.op == "reset";
useEffect(() => {
const user = savedLocalUser();
@@ -36,8 +46,8 @@ const Page: React.FC = () => {
}
}, [router]);
- return user ? (
- <PageContents {...{ user, isReset }} />
+ return user && (!resetPresentation || router.isReady) ? (
+ <PageContents {...{ user, isReset, resetPresentation }} />
) : (
<LoadingIndicator />
);
@@ -48,9 +58,14 @@ export default Page;
interface PageContentsProps {
user: LocalUser;
isReset: boolean;
+ resetPresentation?: ComponentType<NewPasswordPresentationProps>;
}
-const PageContents: React.FC<PageContentsProps> = ({ user, isReset }) => {
+const PageContents: React.FC<PageContentsProps> = ({
+ user,
+ isReset,
+ resetPresentation: ResetPresentation,
+}) => {
const router = useRouter();
const handleSubmit: NewPasswordFormProps["onSubmit"] = useCallback(
@@ -69,6 +84,17 @@ const PageContents: React.FC<PageContentsProps> = ({ user, isReset }) => {
[router],
);
+ if (isReset && ResetPresentation) {
+ return (
+ <NewPasswordForm
+ userEmail={user.email}
+ submitButtonTitle={t("change_password")}
+ onSubmit={handleSubmit}
+ presentation={ResetPresentation}
+ />
+ );
+ }
+
return (
<AccountsPageContents>
<AccountsPageTitle>{t("change_password")}</AccountsPageTitle>
diff --git a/web/packages/accounts/pages/generate.tsx b/web/packages/accounts/pages/generate.tsx
index 21dd00aad0..64346b28ba 100644
--- a/web/packages/accounts/pages/generate.tsx
+++ b/web/packages/accounts/pages/generate.tsx
@@ -41,13 +41,18 @@ import { useCallback, useEffect, useState, type ComponentType } from "react";
import {
NewPasswordForm,
type NewPasswordFormProps,
+ type NewPasswordPresentationProps,
} from "../components/NewPasswordForm";
export interface GeneratePageProps {
+ passwordPresentation?: ComponentType<NewPasswordPresentationProps>;
recoveryKeyPresentation?: ComponentType<RecoveryKeyPresentationProps>;
}
-const Page: React.FC<GeneratePageProps> = ({ recoveryKeyPresentation }) => {
+const Page: React.FC<GeneratePageProps> = ({
+ passwordPresentation,
+ recoveryKeyPresentation,
+}) => {
const { logout, showMiniDialog } = useBaseContext();
const [userEmail, setUserEmail] = useState("");
@@ -122,6 +127,14 @@ const Page: React.FC<GeneratePageProps> = ({ recoveryKeyPresentation }) => {
onClose={handleRecoveryKeyClose}
showMiniDialog={showMiniDialog}
/>
+ ) : userEmail && passwordPresentation ? (
+ <NewPasswordForm
+ userEmail={userEmail}
+ submitButtonTitle={t("set_password")}
+ onSubmit={handleSubmit}
+ onBack={logout}
+ presentation={passwordPresentation}
+ />
) : userEmail ? (
<AccountsPageContents>
<AccountsPageTitle>{t("set_password")}</AccountsPageTitle>