Skip to content

armgit / ente

Public Imported from GitHub
0

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

Integrate social auth flow

Anand Baburajan9fc5fb246a29
diff --git a/web/apps/social/.env.development b/web/apps/social/.env.development
new file mode 100644
index 0000000000..26b087a9b7
--- /dev/null
+++ b/web/apps/social/.env.development
@@ -0,0 +1,7 @@
+# Copy this file into `.env.local` or `.env.development.local` and uncomment
+# these to develop against apps and server running on localhost.
+#
+# For details, please see `apps/photos/.env.development`.
+
+#NEXT_PUBLIC_ENTE_ENDPOINT = http://localhost:8080
+#NEXT_PUBLIC_ENTE_PHOTOS_ENDPOINT = http://localhost:3000
diff --git a/web/apps/social/src/pages/login.tsx b/web/apps/social/src/pages/login.tsx
index 4ac1ef2fa8..f6fec5968f 100644
--- a/web/apps/social/src/pages/login.tsx
+++ b/web/apps/social/src/pages/login.tsx
@@ -1,21 +1,38 @@
import { SocialPageMeta } from "components/SocialPageMeta";
import { useRouter } from "next/router";
-import React from "react";
+import React, { useState } from "react";
import { LoginScreen, loginBackground } from "screens/LoginScreen";
+import { completeSocialLogin } from "services/socialLogin";
import { socialRoutes } from "utils/socialRoutes";
const Page: React.FC = () => {
const router = useRouter();
+ const [loginError, setLoginError] = useState<string>();
+ const [isSubmitting, setIsSubmitting] = useState(false);
return (
<>
<SocialPageMeta themeColor={loginBackground} />
<LoginScreen
+ errorMessage={loginError}
+ isSubmitting={isSubmitting}
onBack={() => void router.push(socialRoutes.onboarding)}
- onContinue={() =>
- void router.push(socialRoutes.setupProfile("login"))
- }
- onSignup={() => void router.push(socialRoutes.signup)}
+ onContinue={async (credentials) => {
+ setIsSubmitting(true);
+ setLoginError(undefined);
+ try {
+ await completeSocialLogin(credentials);
+ void router.push(socialRoutes.setupProfile("login"));
+ } catch (error) {
+ console.error("Social login failed", error);
+ setLoginError(
+ error instanceof Error
+ ? error.message
+ : "Couldn't sign in. Please try again.",
+ );
+ setIsSubmitting(false);
+ }
+ }}
/>
</>
);
diff --git a/web/apps/social/src/pages/signup.tsx b/web/apps/social/src/pages/signup.tsx
index bc5b4e1a03..4c4c9d29ad 100644
--- a/web/apps/social/src/pages/signup.tsx
+++ b/web/apps/social/src/pages/signup.tsx
@@ -1,6 +1,6 @@
import { SocialPageMeta } from "components/SocialPageMeta";
import { useRouter } from "next/router";
-import React from "react";
+import React, { useState } from "react";
import {
CreateAccountScreen,
createAccountBackground,
@@ -13,8 +13,10 @@ import { socialRoutes } from "utils/socialRoutes";
const Page: React.FC = () => {
const router = useRouter();
const { setIsLiveSignupVerification, setSignupEmail } = useSocialAppState();
+ const [isSubmitting, setIsSubmitting] = useState(false);
const createAccount = async (input: CreateAccountInput) => {
+ setIsSubmitting(true);
try {
await beginSocialSignup(input);
setSignupEmail(input.email);
@@ -22,6 +24,7 @@ const Page: React.FC = () => {
void router.push(socialRoutes.verify);
} catch (error) {
console.error("Social signup failed", error);
+ setIsSubmitting(false);
}
};
@@ -29,9 +32,9 @@ const Page: React.FC = () => {
<>
<SocialPageMeta themeColor={createAccountBackground} />
<CreateAccountScreen
+ isSubmitting={isSubmitting}
onBack={() => void router.push(socialRoutes.onboarding)}
onCreateAccount={(input) => void createAccount(input)}
- onLogin={() => void router.push(socialRoutes.login)}
/>
</>
);
diff --git a/web/apps/social/src/screens/CreateAccountScreen.tsx b/web/apps/social/src/screens/CreateAccountScreen.tsx
index 06cb93d28c..9a2dccac48 100644
--- a/web/apps/social/src/screens/CreateAccountScreen.tsx
+++ b/web/apps/social/src/screens/CreateAccountScreen.tsx
@@ -6,7 +6,6 @@ export const createAccountBackground = "#FAFAFA";
const green = "#08C225";
const textBase = "#000";
const textLight = "#969696";
-const textMuted = "#666";
const warning = "#F63A3A";
export interface CreateAccountInput {
@@ -16,9 +15,9 @@ export interface CreateAccountInput {
}
interface CreateAccountScreenProps {
+ isSubmitting?: boolean;
onBack: () => void;
onCreateAccount: (input: CreateAccountInput) => void;
- onLogin?: () => void;
}
interface TextInputProps {
@@ -194,9 +193,9 @@ const TextInput: React.FC<TextInputProps> = ({
};
export const CreateAccountScreen: React.FC<CreateAccountScreenProps> = ({
+ isSubmitting = false,
onBack,
onCreateAccount,
- onLogin,
}) => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
@@ -205,6 +204,7 @@ export const CreateAccountScreen: React.FC<CreateAccountScreenProps> = ({
const [acceptedTerms, setAcceptedTerms] = useState(false);
const canCreateAccount =
+ !isSubmitting &&
email.trim().length > 0 &&
password.length > 0 &&
confirmPassword.length > 0 &&
@@ -474,37 +474,9 @@ export const CreateAccountScreen: React.FC<CreateAccountScreenProps> = ({
: undefined,
}}
>
- Create an account
- </Box>
- <Box
- sx={{
- color: textMuted,
- fontFamily: '"Inter Variable", Inter, sans-serif',
- fontSize: 14,
- fontWeight: 500,
- lineHeight: "20px",
- opacity: 0.8,
- textAlign: "center",
- width: "100%",
- }}
- >
- Already have an account?{" "}
- <Box
- component="button"
- type="button"
- onClick={onLogin}
- sx={{
- bgcolor: "transparent",
- border: 0,
- color: green,
- cursor: onLogin ? "pointer" : "default",
- font: "inherit",
- p: 0,
- textDecoration: "underline",
- }}
- >
- Login
- </Box>
+ {isSubmitting
+ ? "Creating account..."
+ : "Create an account"}
</Box>
</Box>
</Box>
diff --git a/web/apps/social/src/screens/LoginScreen.tsx b/web/apps/social/src/screens/LoginScreen.tsx
index b51ef7bd7e..b5e8b6e0e8 100644
--- a/web/apps/social/src/screens/LoginScreen.tsx
+++ b/web/apps/social/src/screens/LoginScreen.tsx
@@ -6,15 +6,18 @@ export const loginBackground = "#FAFAFA";
const green = "#08C225";
const textBase = "#000";
const textLight = "#969696";
-const textMuted = "#666";
const warning = "#F63A3A";
-const mockLoginData = { email: "[email protected]", password: "password123" };
+export interface SocialLoginCredentials {
+ email: string;
+ password: string;
+}
interface LoginScreenProps {
+ errorMessage?: string;
+ isSubmitting?: boolean;
onBack: () => void;
- onContinue?: () => void;
- onSignup: () => void;
+ onContinue?: (credentials: SocialLoginCredentials) => Promise<void> | void;
}
interface TextInputProps {
@@ -172,17 +175,19 @@ const TextInput: React.FC<TextInputProps> = ({
};
export const LoginScreen: React.FC<LoginScreenProps> = ({
+ errorMessage,
+ isSubmitting = false,
onBack,
onContinue,
- onSignup,
}) => {
- const [email, setEmail] = useState(mockLoginData.email);
- const [password, setPassword] = useState(mockLoginData.password);
+ const [email, setEmail] = useState("");
+ const [password, setPassword] = useState("");
- const canContinue = email.trim().length > 0 && password.length > 0;
+ const canContinue =
+ !isSubmitting && email.trim().length > 0 && password.length > 0;
const submitLogin = () => {
- if (canContinue) onContinue?.();
+ if (canContinue) void onContinue?.({ email, password });
};
const handleSubmit: React.FormEventHandler<HTMLFormElement> = (event) => {
@@ -296,6 +301,22 @@ export const LoginScreen: React.FC<LoginScreenProps> = ({
type="password"
value={password}
/>
+ {errorMessage && (
+ <Box
+ role="alert"
+ sx={{
+ color: warning,
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 500,
+ lineHeight: "18px",
+ mt: "-8px",
+ }}
+ >
+ {errorMessage}
+ </Box>
+ )}
</Box>
<Box
@@ -345,37 +366,7 @@ export const LoginScreen: React.FC<LoginScreenProps> = ({
: undefined,
}}
>
- Continue
- </Box>
- <Box
- sx={{
- color: textMuted,
- fontFamily: '"Inter Variable", Inter, sans-serif',
- fontSize: 14,
- fontWeight: 500,
- lineHeight: "20px",
- opacity: 0.8,
- textAlign: "center",
- width: "100%",
- }}
- >
- Don&apos;t have an account?{" "}
- <Box
- component="button"
- type="button"
- onClick={onSignup}
- sx={{
- bgcolor: "transparent",
- border: 0,
- color: green,
- cursor: "pointer",
- font: "inherit",
- p: 0,
- textDecoration: "underline",
- }}
- >
- Signup
- </Box>
+ {isSubmitting ? "Signing in..." : "Continue"}
</Box>
</Box>
</Box>
diff --git a/web/apps/social/src/services/socialLogin.ts b/web/apps/social/src/services/socialLogin.ts
new file mode 100644
index 0000000000..ecabeb04a7
--- /dev/null
+++ b/web/apps/social/src/services/socialLogin.ts
@@ -0,0 +1,119 @@
+import {
+ replaceSavedLocalUser,
+ saveIsFirstLogin,
+ saveKeyAttributes,
+ saveSRPAttributes,
+ updateSavedLocalUser,
+} from "ente-accounts-rs/services/accounts-db";
+import { decryptBox, deriveKey } from "ente-accounts-rs/services/crypto";
+import {
+ saveMasterKeyInSessionAndSafeStore,
+ stashKeyEncryptionKeyInSessionStore,
+} from "ente-accounts-rs/services/session-storage";
+import {
+ getSRPAttributes,
+ srpVerificationUnauthorizedErrorMessage,
+ verifySRP,
+} from "ente-accounts-rs/services/srp";
+import {
+ decryptAndStoreTokenIfNeeded,
+ generateAndSaveInteractiveKeyAttributes,
+} from "ente-accounts-rs/services/user";
+import { saveAuthToken } from "ente-base/token";
+
+export interface SocialLoginInput {
+ email: string;
+ password: string;
+}
+
+export const completeSocialLogin = async ({
+ email,
+ password,
+}: SocialLoginInput) => {
+ const cleanedEmail = email.trim();
+ const srpAttributes = await getSRPAttributes(cleanedEmail);
+ if (!srpAttributes) throw new Error("No account found for this email.");
+ if (srpAttributes.isEmailMFAEnabled) {
+ throw new Error(
+ "This account requires email verification during login.",
+ );
+ }
+
+ replaceSavedLocalUser({ email: cleanedEmail });
+ saveSRPAttributes(srpAttributes);
+
+ const kek = await deriveKey(
+ password,
+ srpAttributes.kekSalt,
+ srpAttributes.opsLimit,
+ srpAttributes.memLimit,
+ );
+
+ let verification;
+ try {
+ verification = await verifySRP(srpAttributes, kek);
+ } catch (error) {
+ if (
+ error instanceof Error &&
+ error.message == srpVerificationUnauthorizedErrorMessage
+ ) {
+ throw new Error("Incorrect email or password.");
+ }
+ throw error;
+ }
+
+ const {
+ encryptedToken,
+ id,
+ keyAttributes,
+ passkeySessionID,
+ token,
+ twoFactorSessionID,
+ twoFactorSessionIDV2,
+ } = verification;
+
+ const secondFactorSessionID = twoFactorSessionID || twoFactorSessionIDV2;
+
+ if (passkeySessionID || secondFactorSessionID) {
+ await stashKeyEncryptionKeyInSessionStore(kek);
+ updateSavedLocalUser({
+ passkeySessionID,
+ twoFactorSessionID: secondFactorSessionID,
+ isTwoFactorEnabled: true,
+ });
+ throw new Error("This account needs a second factor to sign in.");
+ }
+
+ updateSavedLocalUser({
+ id,
+ token,
+ encryptedToken,
+ isTwoFactorEnabled: undefined,
+ twoFactorSessionID: undefined,
+ passkeySessionID: undefined,
+ });
+ if (token) await saveAuthToken(token);
+ if (!keyAttributes) {
+ throw new Error("This account has not finished setup.");
+ }
+
+ saveIsFirstLogin();
+ saveKeyAttributes(keyAttributes);
+
+ const masterKey = await decryptBox(
+ {
+ encryptedData: keyAttributes.encryptedKey,
+ nonce: keyAttributes.keyDecryptionNonce,
+ },
+ kek,
+ );
+ const updatedKeyAttributes = await generateAndSaveInteractiveKeyAttributes(
+ password,
+ keyAttributes,
+ masterKey,
+ );
+ await saveMasterKeyInSessionAndSafeStore(masterKey);
+ await decryptAndStoreTokenIfNeeded(updatedKeyAttributes, masterKey);
+
+ return { email: cleanedEmail };
+};