Skip to main content

signup_user

Function signup_user 

Source
pub async fn signup_user(
    db: &DB,
    username: &str,
    email: Option<&str>,
    password: &str,
    email_backup_codes: Option<Vec<String>>,
) -> Result<User, Box<dyn Error + Send + Sync>>
Expand description

Creates a new user account in the database

§Arguments

  • db - A reference to the database connection
  • username - The desired username for the new account
  • email - An optional email address for the new account
  • password_hash - The hashed password for the new account

§Returns

  • Ok(User) - The newly created user object if the operation was successful
  • Err(Box<dyn Error>) - An error if the operation failed, such as if the username is already taken or if there was a database error

§Errors

  • “Failed to create user” - If the database query did not return a user object, indicating that the user creation failed

§Example

use crate::db::DB;
use crate::db::queries::auth;
async fn example_signup(db: &DB) {
    let username = "new_user";
    let email = Some("123@example.com");
    let password_hash = "hashed_password";
    match auth::signup_user(db, username, email, password_hash).await {
        Ok(user) => println!("User created: {:?}", user),
        Err(e) => eprintln!("Error creating user: {}", e),
    }
}