Add email + OTP sign-in flow for cloud sync
Replace the "drop a session token in a file" workflow with a real Chrome-style email login. The Cloudflare worker already had Better Auth's `email-otp` plugin wired into `SEND_EMAIL`; this commit builds the renderer-side counterpart. Worker side: - Move the OTP sender from `auth@elydora.com` to `browser@elydora.com` (wrangler.toml `allowed_sender_addresses` + better_auth.ts `EMAIL_OTP_FROM_ADDRESS`). Worker must be redeployed to pick this up. Client side (`ely_sync_client::email_otp`): - `send_email_otp(config, email)` POSTs `/api/auth/email-otp/send-verification-otp` with `{ email, type: "sign-in" }`. - `verify_email_otp(config, email, otp)` POSTs `/api/auth/sign-in/email-otp`, reads the Better Auth session token from the JSON body's `token` field with the `Set-Cookie: better-auth.session_token=…` header as the documented fallback channel, and returns it as a `BearerToken`. Shell side (`shell/auth.rs` + `shell/internal_pages/sync.rs`): - New `AuthFlowPhase` (Idle / SendingCode / AwaitingOtp / Verifying / Error) tracks the in-flight form. Two off-thread workers run the HTTP exchanges so the GPUI render loop never blocks. - Successful verify saves the bearer via `SyncEngine::install_bearer` and triggers an immediate snapshot upload, so the user is signed in + initial-synced in one click. - Sync settings page replaces the bare "Sync now" button row with an account card: when SignedOut → email field + Send code → OTP field + Verify / Resend; when signed in → an account chip + Sign out. - `trigger_cloud_sync_upload` no longer takes a `Context` param so the post-auth path can fire it from the inbox-drain pass without needing a window context.
This commit is contained in:
@@ -0,0 +1,267 @@
|
||||
//! Email + OTP sign-in flow plumbing for the shell.
|
||||
//!
|
||||
//! The HTTP work runs on a dedicated `ely-sync-auth` thread so the
|
||||
//! GPUI render loop never blocks on the network — same invariant the
|
||||
//! Servo IPC worker enforces. Results flow back to the shell through
|
||||
//! `SyncStateUpdate` messages drained by `tick_external_web_surfaces`,
|
||||
//! so the existing 8 ms tick is the single point that reconciles
|
||||
//! background-task state with `BrowserCore`.
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
use ely_browser_core::SyncEngine;
|
||||
use ely_sync_client::{ApiClientConfig, BearerToken, send_email_otp, verify_email_otp};
|
||||
use gpui::Context;
|
||||
|
||||
use crate::services::servo_profile_data::{default_profile_data_root, profile_data_dir};
|
||||
|
||||
use super::{ElyShell, ShellState, SyncStateUpdate};
|
||||
|
||||
/// Where the user is in the email OTP form. Tracked on `ElyShell` so
|
||||
/// the Sync settings page can pick the right widget cluster (only the
|
||||
/// email row, OTP row + email row, signed-in account chip, etc.) on
|
||||
/// every render without re-deriving it from disk.
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub(crate) enum AuthFlowPhase {
|
||||
/// No sign-in attempt in progress. The form shows just the email
|
||||
/// field and a "Send code" button.
|
||||
#[default]
|
||||
Idle,
|
||||
/// `send_email_otp` is in flight. UI disables the form so the
|
||||
/// user can't resend before the worker confirms acceptance.
|
||||
SendingCode { email: String },
|
||||
/// The worker accepted the request and Cloudflare's `SEND_EMAIL`
|
||||
/// binding handed the message to the recipient's MTA. UI now
|
||||
/// reveals the OTP field.
|
||||
AwaitingOtp { email: String },
|
||||
/// `verify_email_otp` is in flight. UI shows a transient
|
||||
/// "Verifying…" state.
|
||||
Verifying { email: String },
|
||||
/// Last attempt failed. UI surfaces the message inline so the
|
||||
/// user knows what to retry.
|
||||
Error { email: String, message: String },
|
||||
}
|
||||
|
||||
impl AuthFlowPhase {
|
||||
pub(crate) fn email(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::Idle => None,
|
||||
Self::SendingCode { email }
|
||||
| Self::AwaitingOtp { email }
|
||||
| Self::Verifying { email }
|
||||
| Self::Error { email, .. } => Some(email),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn error_message(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::Error { message, .. } => Some(message.as_str()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_busy(&self) -> bool {
|
||||
matches!(self, Self::SendingCode { .. } | Self::Verifying { .. })
|
||||
}
|
||||
}
|
||||
|
||||
impl ElyShell {
|
||||
/// Hand the typed email to the worker thread that calls
|
||||
/// `send_email_otp`. The thread reports success / failure back
|
||||
/// through the shared `SyncStateUpdate` channel, which the next
|
||||
/// shell tick reconciles into the `auth_flow_phase`.
|
||||
pub(crate) fn submit_email_otp_request(&mut self, cx: &mut Context<Self>) {
|
||||
let email = self.read_auth_email_input(cx);
|
||||
let Some(email) = normalize_email(&email) else {
|
||||
self.auth_flow_phase = AuthFlowPhase::Error {
|
||||
email: String::new(),
|
||||
message: "Enter a valid email to receive a code.".to_string(),
|
||||
};
|
||||
return;
|
||||
};
|
||||
self.auth_flow_phase = AuthFlowPhase::SendingCode { email: email.clone() };
|
||||
let tx = self.sync_inbox_tx.clone();
|
||||
spawn_send_otp(email, tx);
|
||||
}
|
||||
|
||||
/// Hand the typed OTP to the worker thread that calls
|
||||
/// `verify_email_otp`, persists the bearer token, and triggers
|
||||
/// the first snapshot upload on success.
|
||||
pub(crate) fn submit_email_otp_verify(&mut self, cx: &mut Context<Self>) {
|
||||
let email = match self.auth_flow_phase.clone() {
|
||||
AuthFlowPhase::AwaitingOtp { email }
|
||||
| AuthFlowPhase::Error { email, .. }
|
||||
| AuthFlowPhase::Verifying { email } => email,
|
||||
_ => return,
|
||||
};
|
||||
let otp = self.read_auth_otp_input(cx);
|
||||
let normalized_otp = otp.trim().replace(['-', ' '], "");
|
||||
if normalized_otp.is_empty() {
|
||||
self.auth_flow_phase =
|
||||
AuthFlowPhase::Error { email, message: "Enter the code you received.".to_string() };
|
||||
return;
|
||||
}
|
||||
let active_profile_id = match active_profile_id_for(&self.state) {
|
||||
Some(id) => id,
|
||||
None => return,
|
||||
};
|
||||
let Some(profile_root) = default_profile_data_root() else {
|
||||
self.auth_flow_phase = AuthFlowPhase::Error {
|
||||
email,
|
||||
message: "Profile data root is unavailable on this machine.".to_string(),
|
||||
};
|
||||
return;
|
||||
};
|
||||
let profile_dir = profile_data_dir(&profile_root, &active_profile_id);
|
||||
self.auth_flow_phase = AuthFlowPhase::Verifying { email: email.clone() };
|
||||
let tx = self.sync_inbox_tx.clone();
|
||||
spawn_verify_otp(email, normalized_otp, profile_dir, tx);
|
||||
}
|
||||
|
||||
/// Drop the persisted bearer token and reset the local form.
|
||||
/// The bearer file is removed synchronously — there is no network
|
||||
/// call to make, the token is the only artefact we own.
|
||||
pub(crate) fn submit_sign_out(&mut self, _cx: &mut Context<Self>) {
|
||||
self.auth_flow_phase = AuthFlowPhase::Idle;
|
||||
let active_profile_id = match active_profile_id_for(&self.state) {
|
||||
Some(id) => id,
|
||||
None => return,
|
||||
};
|
||||
let Some(profile_root) = default_profile_data_root() else {
|
||||
return;
|
||||
};
|
||||
let profile_dir = profile_data_dir(&profile_root, &active_profile_id);
|
||||
match SyncEngine::for_profile_dir(&profile_dir, "ELY", super::sync_platform_label()) {
|
||||
Ok(mut engine) => {
|
||||
let _ = engine.install_bearer("");
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(target: "ely::sync", error = %error, "sign-out failed to load engine");
|
||||
}
|
||||
}
|
||||
if let ShellState::Ready(core) = &mut self.state {
|
||||
core.set_sync_connection_state(ely_domain::SyncConnectionState::SignedOut);
|
||||
}
|
||||
}
|
||||
|
||||
fn read_auth_email_input(&self, cx: &Context<Self>) -> String {
|
||||
self.auth_email_input.read(cx).value().to_string()
|
||||
}
|
||||
|
||||
fn read_auth_otp_input(&self, cx: &Context<Self>) -> String {
|
||||
self.auth_otp_input.read(cx).value().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_email(raw: &str) -> Option<String> {
|
||||
let trimmed = raw.trim();
|
||||
if !trimmed.contains('@') || trimmed.starts_with('@') || trimmed.ends_with('@') {
|
||||
return None;
|
||||
}
|
||||
Some(trimmed.to_lowercase())
|
||||
}
|
||||
|
||||
fn active_profile_id_for(state: &ShellState) -> Option<ely_domain::ProfileId> {
|
||||
let ShellState::Ready(core) = state else {
|
||||
return None;
|
||||
};
|
||||
core.snapshot().ok().map(|snapshot| snapshot.active_profile_id.clone())
|
||||
}
|
||||
|
||||
fn spawn_send_otp(email: String, tx: Sender<SyncStateUpdate>) {
|
||||
std::thread::Builder::new()
|
||||
.name("ely-sync-auth-send".to_string())
|
||||
.spawn(move || {
|
||||
let config = ApiClientConfig::production();
|
||||
match send_email_otp(&config, &email) {
|
||||
Ok(()) => {
|
||||
let _ = tx.send(SyncStateUpdate::AuthOtpSent { email });
|
||||
}
|
||||
Err(error) => {
|
||||
let _ =
|
||||
tx.send(SyncStateUpdate::AuthError { email, message: error.to_string() });
|
||||
}
|
||||
}
|
||||
})
|
||||
.map(|_| ())
|
||||
.unwrap_or_else(|error| {
|
||||
tracing::warn!(target: "ely::sync", error = %error, "spawn ely-sync-auth-send failed");
|
||||
});
|
||||
}
|
||||
|
||||
fn spawn_verify_otp(
|
||||
email: String,
|
||||
otp: String,
|
||||
profile_dir: std::path::PathBuf,
|
||||
tx: Sender<SyncStateUpdate>,
|
||||
) {
|
||||
std::thread::Builder::new()
|
||||
.name("ely-sync-auth-verify".to_string())
|
||||
.spawn(move || {
|
||||
let config = ApiClientConfig::production();
|
||||
let token: BearerToken = match verify_email_otp(&config, &email, &otp) {
|
||||
Ok(token) => token,
|
||||
Err(error) => {
|
||||
let _ = tx.send(SyncStateUpdate::AuthError {
|
||||
email,
|
||||
message: error.to_string(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mut engine =
|
||||
match SyncEngine::for_profile_dir(&profile_dir, "ELY", super::sync_platform_label())
|
||||
{
|
||||
Ok(engine) => engine,
|
||||
Err(error) => {
|
||||
let _ = tx.send(SyncStateUpdate::AuthError {
|
||||
email,
|
||||
message: error.to_string(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
if let Err(error) = engine.install_bearer(token.as_str()) {
|
||||
let _ = tx.send(SyncStateUpdate::AuthError { email, message: error.to_string() });
|
||||
return;
|
||||
}
|
||||
let _ = tx.send(SyncStateUpdate::AuthSucceeded { email });
|
||||
})
|
||||
.map(|_| ())
|
||||
.unwrap_or_else(|error| {
|
||||
tracing::warn!(target: "ely::sync", error = %error, "spawn ely-sync-auth-verify failed");
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{AuthFlowPhase, normalize_email};
|
||||
|
||||
#[test]
|
||||
fn normalize_lowercases_and_trims() {
|
||||
assert_eq!(normalize_email(" User@Example.COM "), Some("user@example.com".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_rejects_obviously_broken() {
|
||||
assert_eq!(normalize_email("noatsign"), None);
|
||||
assert_eq!(normalize_email("@no-local-part"), None);
|
||||
assert_eq!(normalize_email("missing-domain@"), None);
|
||||
assert_eq!(normalize_email(""), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auth_phase_helpers() {
|
||||
let phase = AuthFlowPhase::Verifying { email: "you@there".to_string() };
|
||||
assert_eq!(phase.email(), Some("you@there"));
|
||||
assert!(phase.is_busy());
|
||||
assert_eq!(phase.error_message(), None);
|
||||
|
||||
let phase = AuthFlowPhase::Error {
|
||||
email: "you@there".to_string(),
|
||||
message: "rate limited".to_string(),
|
||||
};
|
||||
assert_eq!(phase.error_message(), Some("rate limited"));
|
||||
assert!(!phase.is_busy());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user