Skip to content

armgit / ente

Public Imported from GitHub
0

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

Wire social signup flow

Anand Baburajan067042bf5468
diff --git a/web/apps/social/package.json b/web/apps/social/package.json
index 2466e4b345..66ac26e9c7 100644
--- a/web/apps/social/package.json
+++ b/web/apps/social/package.json
@@ -5,7 +5,9 @@
"dependencies": {
"@hugeicons/core-free-icons": "^1.1.0",
"@hugeicons/react": "^1.1.4",
+ "ente-accounts-rs": "*",
"ente-base": "*",
+ "ente-wasm": "*",
"photoswipe": "^5.4.4"
},
"devDependencies": {
diff --git a/web/apps/social/src/pages/index.tsx b/web/apps/social/src/pages/index.tsx
index 466f5364ad..3f5380957a 100644
--- a/web/apps/social/src/pages/index.tsx
+++ b/web/apps/social/src/pages/index.tsx
@@ -5,6 +5,7 @@ import React, { useEffect, useState } from "react";
import {
CreateAccountScreen,
createAccountBackground,
+ type CreateAccountInput,
} from "screens/CreateAccountScreen";
import { FriendsScreen, friendsBackground } from "screens/FriendsScreen";
import { HomeScreen, homeBackground } from "screens/HomeScreen";
@@ -36,6 +37,11 @@ import {
VerifyEmailScreen,
verifyEmailBackground,
} from "screens/VerifyEmailScreen";
+import {
+ beginSocialSignup,
+ completeSocialSignup,
+ resendSocialSignupCode,
+} from "services/socialSignup";
type Screen =
| "onboarding"
@@ -132,6 +138,8 @@ const Page: React.FC = () => {
const [selectedFriendID, setSelectedFriendID] = useState<string | null>(
null,
);
+ const [isLiveSignupVerification, setIsLiveSignupVerification] =
+ useState(false);
const openSetupProfile = (backScreen: ProfileBackScreen) => {
setProfileBackScreen(backScreen);
@@ -160,6 +168,32 @@ const Page: React.FC = () => {
);
}, []);
+ const createAccount = async (input: CreateAccountInput) => {
+ try {
+ await beginSocialSignup(input);
+ setEmail(input.email || "[email protected]");
+ setIsLiveSignupVerification(true);
+ setScreen("verify-email");
+ } catch (error) {
+ console.error("Social signup failed", error);
+ }
+ };
+
+ const verifySignupEmail = async (code: string) => {
+ if (!isLiveSignupVerification) {
+ openSetupProfile("verify-email");
+ return;
+ }
+
+ try {
+ await completeSocialSignup(email, code);
+ setIsLiveSignupVerification(false);
+ openSetupProfile("verify-email");
+ } catch (error) {
+ console.error("Social signup verification failed", error);
+ }
+ };
+
const selectedFriend = selectedFriendID
? (friends.find((friend) => friend.id == selectedFriendID) ??
sampleFriends.find((friend) => friend.id == selectedFriendID))
@@ -240,10 +274,9 @@ const Page: React.FC = () => {
{screen == "create-account" && (
<CreateAccountScreen
onBack={() => setScreen("onboarding")}
- onCreateAccount={(nextEmail) => {
- setEmail(nextEmail || "[email protected]");
- setScreen("verify-email");
- }}
+ onCreateAccount={(input) =>
+ void createAccount(input)
+ }
onLogin={() => setScreen("login")}
/>
)}
@@ -257,9 +290,17 @@ const Page: React.FC = () => {
{screen == "verify-email" && (
<VerifyEmailScreen
email={email}
+ initialCode={
+ isLiveSignupVerification ? "" : undefined
+ }
onBack={() => setScreen("create-account")}
onChangeEmail={() => setScreen("create-account")}
- onVerify={() => openSetupProfile("verify-email")}
+ onResendCode={
+ isLiveSignupVerification
+ ? () => void resendSocialSignupCode(email)
+ : undefined
+ }
+ onVerify={(code) => void verifySignupEmail(code)}
/>
)}
{screen == "setup-profile" && (
diff --git a/web/apps/social/src/screens/CreateAccountScreen.tsx b/web/apps/social/src/screens/CreateAccountScreen.tsx
index 7fa9f34fbf..06cb93d28c 100644
--- a/web/apps/social/src/screens/CreateAccountScreen.tsx
+++ b/web/apps/social/src/screens/CreateAccountScreen.tsx
@@ -9,15 +9,15 @@ const textLight = "#969696";
const textMuted = "#666";
const warning = "#F63A3A";
-const mockCreateAccountData = {
- email: "[email protected]",
- password: "password123",
- referralSource: "A friend",
-};
+export interface CreateAccountInput {
+ email: string;
+ password: string;
+ referralSource: string;
+}
interface CreateAccountScreenProps {
onBack: () => void;
- onCreateAccount: (email: string) => void;
+ onCreateAccount: (input: CreateAccountInput) => void;
onLogin?: () => void;
}
@@ -198,15 +198,11 @@ export const CreateAccountScreen: React.FC<CreateAccountScreenProps> = ({
onCreateAccount,
onLogin,
}) => {
- const [email, setEmail] = useState(mockCreateAccountData.email);
- const [password, setPassword] = useState(mockCreateAccountData.password);
- const [confirmPassword, setConfirmPassword] = useState(
- mockCreateAccountData.password,
- );
- const [referralSource, setReferralSource] = useState(
- mockCreateAccountData.referralSource,
- );
- const [acceptedTerms, setAcceptedTerms] = useState(true);
+ const [email, setEmail] = useState("");
+ const [password, setPassword] = useState("");
+ const [confirmPassword, setConfirmPassword] = useState("");
+ const [referralSource, setReferralSource] = useState("");
+ const [acceptedTerms, setAcceptedTerms] = useState(false);
const canCreateAccount =
email.trim().length > 0 &&
@@ -216,7 +212,9 @@ export const CreateAccountScreen: React.FC<CreateAccountScreenProps> = ({
acceptedTerms;
const submitCreateAccount = () => {
- if (canCreateAccount) onCreateAccount(email.trim());
+ if (canCreateAccount) {
+ onCreateAccount({ email: email.trim(), password, referralSource });
+ }
};
const handleSubmit: React.FormEventHandler<HTMLFormElement> = (event) => {
diff --git a/web/apps/social/src/screens/VerifyEmailScreen.tsx b/web/apps/social/src/screens/VerifyEmailScreen.tsx
index 72a3a3d300..d6799f4d51 100644
--- a/web/apps/social/src/screens/VerifyEmailScreen.tsx
+++ b/web/apps/social/src/screens/VerifyEmailScreen.tsx
@@ -13,9 +13,11 @@ const mockVerificationCode = "888888";
interface VerifyEmailScreenProps {
email: string;
+ initialCode?: string;
onBack: () => void;
onChangeEmail: () => void;
- onVerify?: () => void;
+ onResendCode?: () => void;
+ onVerify?: (code: string) => void;
}
const BackIcon: React.FC = () => (
@@ -114,11 +116,20 @@ const OtpInput: React.FC<OtpInputProps> = ({
export const VerifyEmailScreen: React.FC<VerifyEmailScreenProps> = ({
email,
+ initialCode = mockVerificationCode,
onBack,
onChangeEmail,
+ onResendCode,
onVerify,
}) => {
- const [otp, setOtp] = useState<string[]>(mockVerificationCode.split(""));
+ const [otp, setOtp] = useState<string[]>(
+ initialCode
+ .replace(/\D/g, "")
+ .slice(0, 6)
+ .padEnd(6, " ")
+ .split("")
+ .map((digit) => (digit == " " ? "" : digit)),
+ );
const inputRefs = useRef<(HTMLInputElement | null)[]>([]);
const activeIndex = Math.max(
@@ -134,19 +145,18 @@ export const VerifyEmailScreen: React.FC<VerifyEmailScreenProps> = ({
useEffect(() => {
const initialFocusIndex =
- mockVerificationCode.replace(/\D/g, "").slice(0, 6).length >= 6
+ initialCode.replace(/\D/g, "").slice(0, 6).length >= 6
? 5
: Math.max(
0,
- mockVerificationCode.replace(/\D/g, "").slice(0, 6)
- .length,
+ initialCode.replace(/\D/g, "").slice(0, 6).length,
);
const animationFrame = window.requestAnimationFrame(() =>
focusInput(initialFocusIndex),
);
return () => window.cancelAnimationFrame(animationFrame);
- }, []);
+ }, [initialCode]);
const setDigitsFrom = (startIndex: number, rawValue: string) => {
const digits = rawValue.replace(/\D/g, "").slice(0, 6 - startIndex);
@@ -202,6 +212,7 @@ export const VerifyEmailScreen: React.FC<VerifyEmailScreenProps> = ({
const handleResendCode = () => {
setOtp(Array(6).fill(""));
focusInput(0);
+ onResendCode?.();
};
return (
@@ -437,7 +448,7 @@ export const VerifyEmailScreen: React.FC<VerifyEmailScreenProps> = ({
component="button"
type="button"
disabled={!canVerify}
- onClick={onVerify}
+ onClick={() => onVerify?.(otp.join(""))}
sx={{
alignItems: "center",
bgcolor: canVerify ? green : "#F5F5F5",
diff --git a/web/apps/social/src/services/socialSignup.ts b/web/apps/social/src/services/socialSignup.ts
new file mode 100644
index 0000000000..7a69ac2fa9
--- /dev/null
+++ b/web/apps/social/src/services/socialSignup.ts
@@ -0,0 +1,111 @@
+import {
+ replaceSavedLocalUser,
+ savedOriginalKeyAttributes,
+ saveIsFirstLogin,
+ saveJustSignedUp,
+ saveKeyAttributes,
+ saveOriginalKeyAttributes,
+ stashReferralSource,
+ stashSRPSetupAttributes,
+ unstashAfterUseSRPSetupAttributes,
+ unstashReferralSource,
+} from "ente-accounts-rs/services/accounts-db";
+import {
+ masterKeyFromSession,
+ saveMasterKeyInSessionAndSafeStore,
+} from "ente-accounts-rs/services/session-storage";
+import {
+ generateSRPSetupAttributes,
+ getAndSaveSRPAttributes,
+ setupSRP,
+} from "ente-accounts-rs/services/srp";
+import {
+ decryptAndStoreTokenIfNeeded,
+ generateAndSaveInteractiveKeyAttributes,
+ generateKeysAndAttributes,
+ putUserKeyAttributes,
+ sendOTT,
+ verifyEmail,
+} from "ente-accounts-rs/services/user";
+import { saveAuthToken } from "ente-base/token";
+
+export interface SocialSignupInput {
+ email: string;
+ password: string;
+ referralSource: string;
+}
+
+export const beginSocialSignup = async ({
+ email,
+ password,
+ referralSource,
+}: SocialSignupInput) => {
+ const cleanedEmail = email.trim();
+ const cleanedReferralSource = referralSource.trim();
+ if (cleanedReferralSource) stashReferralSource(cleanedReferralSource);
+
+ await sendOTT(cleanedEmail, "signup");
+ replaceSavedLocalUser({ email: cleanedEmail });
+
+ const { masterKey, kek, keyAttributes } =
+ await generateKeysAndAttributes(password);
+ saveOriginalKeyAttributes(keyAttributes);
+ stashSRPSetupAttributes(await generateSRPSetupAttributes(kek));
+ await generateAndSaveInteractiveKeyAttributes(
+ password,
+ keyAttributes,
+ masterKey,
+ );
+ await saveMasterKeyInSessionAndSafeStore(masterKey);
+ saveJustSignedUp();
+};
+
+export const completeSocialSignup = async (email: string, code: string) => {
+ const referralSource = unstashReferralSource();
+ const cleanedReferralSource = referralSource
+ ? `web:${referralSource}`
+ : undefined;
+ const {
+ keyAttributes,
+ encryptedToken,
+ token,
+ id,
+ twoFactorSessionID,
+ passkeySessionID,
+ } = await verifyEmail(email, code, cleanedReferralSource);
+
+ if (passkeySessionID || twoFactorSessionID) {
+ throw new Error("Unexpected second factor during signup");
+ }
+
+ if (token) await saveAuthToken(token);
+ replaceSavedLocalUser({ id, email, token, encryptedToken });
+
+ if (keyAttributes) {
+ saveKeyAttributes(keyAttributes);
+ saveOriginalKeyAttributes(keyAttributes);
+ const masterKey = await masterKeyFromSession();
+ if (!token && encryptedToken && masterKey) {
+ await decryptAndStoreTokenIfNeeded(keyAttributes, masterKey);
+ }
+ } else {
+ const originalKeyAttributes = savedOriginalKeyAttributes();
+ if (originalKeyAttributes) {
+ await putUserKeyAttributes(originalKeyAttributes);
+ const masterKey = await masterKeyFromSession();
+ if (!token && encryptedToken && masterKey) {
+ await decryptAndStoreTokenIfNeeded(
+ originalKeyAttributes,
+ masterKey,
+ );
+ }
+ }
+ await unstashAfterUseSRPSetupAttributes(setupSRP);
+ await getAndSaveSRPAttributes(email);
+ }
+
+ saveIsFirstLogin();
+};
+
+export const resendSocialSignupCode = (email: string) =>
+ sendOTT(email, undefined);
diff --git a/web/package.json b/web/package.json
index 3da3c070af..29e1fd8bc0 100644
--- a/web/package.json
+++ b/web/package.json
@@ -20,7 +20,7 @@
"build:payments": "yarn workspace payments build",
"build:photos": "yarn build:wasm && yarn workspace photos next build",
"build:share": "yarn build:wasm && yarn workspace share next build && yarn workspace share build:post",
- "build:social": "yarn workspace social next build",
+ "build:social": "yarn build:wasm && yarn workspace social next build",
"build:twoof3": "yarn workspace twoof3 next build",
"build:wasm": "yarn workspace ente-wasm build --no-pack",
"dev": "yarn dev:photos",
@@ -36,7 +36,7 @@
"dev:payments": "yarn workspace payments dev",
"dev:photos": "yarn build:wasm && yarn workspace photos next dev -p 3000",
"dev:share": "yarn build:wasm && yarn workspace share next dev -p 3005",
- "dev:social": "yarn workspace social next dev -p 3012",
+ "dev:social": "yarn build:wasm && yarn workspace social next dev -p 3012",
"dev:twoof3": "yarn workspace twoof3 next dev -p 3009",
"lint": "concurrently --names 'prettier,eslint,tsc' \"yarn prettier --check --log-level warn .\" \"yarn workspaces run eslint\" \"yarn workspaces run tsc\"",
"lint-fix": "concurrently --names 'prettier,eslint,tsc' \"yarn prettier --write --log-level warn .\" \"yarn workspaces run eslint --fix\" \"yarn workspaces run tsc\"",