use super::funejson::Value; use super::auth_normalise_email::normalise_email; fn is_ascii_space(ch: char) -> bool { matches!(ch, ' ' | '\t' | '\n' | '\r' | '\u{0C}' | '\u{0B}') } /// A login form checked for shape only: the email to look up when valid, and /// a message for each field that needs fixing. The password is never judged /// against a policy here, so a password set under older rules still logs in. pub fn validate_login(email: &str, password: &str) -> LoginCheck { let mut fields: Vec<(String, String)> = Vec::new(); let normalised = normalise_email(email); if normalised.is_none() { let message = if email.trim_matches(is_ascii_space).is_empty() { "Enter your email address." } else { "Enter a valid email address, like name@example.com." }; fields.push(("email".to_string(), message.to_string())); } if password.is_empty() { fields.push(("password".to_string(), "Enter your password.".to_string())); } LoginCheck { valid: fields.is_empty(), email: normalised, fields } } pub fn login_check_to_value(check: &LoginCheck) -> Value { Value::obj(vec![ ("valid", Value::Bool(check.valid)), ("email", check.email.as_deref().map(Value::str).unwrap_or(Value::Null)), ( "fields", Value::Obj(check.fields.iter().map(|(k, v)| (k.clone(), Value::str(v))).collect()), ), ]) } pub fn fune_vector(args: &[Value]) -> Value { // A non-string is an empty field, as in TypeScript and Python. login_check_to_value(&validate_login(args[0].as_str(), args[1].as_str())) }