fn signup(context: &Context, input: SignupInput) -> Result<String, GraphQLError> {
        let SignupInput { email, password } = input;

        // Check email validity
        if !regex::Regex::new(r"^\S+@\S+\.\S+$")
            .unwrap()
            .is_match(&email)
        {
            return Err(GraphQLError::InvalidEmailAddress);
        }

        // Check password validity
        if !(8..=64).contains(&password.graphemes(true).count()) {
            return Err(GraphQLError::InvalidPassword);
        }

        // Check email availability
        match repositories::UserRepository::find_one_by_email(&email[..], &context.db_pool) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(Some(_)) => return Err(GraphQLError::AlreadyUsedEmail),
            _ => (),
        }

        // Prepare the new user's data
        let user_id = uuid::Uuid::new_v4();
        let hash = match security::hash_password(
            password.as_bytes(),
            context.config.security().hash_salt(),
        ) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(hash) => hash,
        };
        let new_user = repositories::NewUser {
            id: user_id,
            email,
            password: hash,
        };

        let user = match repositories::UserRepository::save(&new_user, &context.db_pool) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(u) => u,
        };

        // Sign token
        let token = match security::sign_token(
            user_id,
            context.config.security().token_expiration_time(),
            context.config.security().secret_key(),
        ) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(token) => token,
        };

        Ok(token)
    }
// FIXME: Extract domain and repository logic to own module
/// Signup a new user. Check if the email isn't already taken or valid and that the password is valid and proceed to create his account.
Extract domain and repository logic to own module
Signup a new user. Check if the email isn't already taken or valid and that the password is valid and proceed to create his account.
Extract domain and repository logic to own module Signup a new user . Check if the email isn ' t already taken or valid and that the password is valid and proceed to create his account .
fn signup(context: &Context, input: SignupInput) -> Result<String, GraphQLError> {
        let SignupInput { email, password } = input;
        // Check email validity
        if !regex::Regex::new(r"^\S+@\S+\.\S+$")
            .unwrap()
            .is_match(&email)
        {
            return Err(GraphQLError::InvalidEmailAddress);
        }
        // Check password validity
        if !(8..=64).contains(&password.graphemes(true).count()) {
            return Err(GraphQLError::InvalidPassword);
        }
        // Check email availability
        match repositories::UserRepository::find_one_by_email(&email[..], &context.db_pool) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(Some(_)) => return Err(GraphQLError::AlreadyUsedEmail),
            _ => (),
        }
        // Prepare the new user's data
        let user_id = uuid::Uuid::new_v4();
        let hash = match security::hash_password(
            password.as_bytes(),
            context.config.security().hash_salt(),
        ) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(hash) => hash,
        };
        let new_user = repositories::NewUser {
            id: user_id,
            email,
            password: hash,
        };
        let user = match repositories::UserRepository::save(&new_user, &context.db_pool) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(u) => u,
        };
        // Sign token
        let token = match security::sign_token(
            user_id,
            context.config.security().token_expiration_time(),
            context.config.security().secret_key(),
        ) {
            Err(e) => return Err(GraphQLError::InternalServerError(e)),
            Ok(token) => token,
        };
        Ok(token)
    }
fn signup ( context : & Context , input : SignupInput ) -> Result < String , GraphQLError > { let SignupInput { email , password } = input ; // Check email validity if ! regex :: Regex :: new ( r"^\S+@\S+\.\S+$" ) . unwrap ( ) . is_match ( & email ) { return Err ( GraphQLError :: InvalidEmailAddress ) ; } // Check password validity if ! ( 8 ..= 64 ) . contains ( & password . graphemes ( true ) . count ( ) ) { return Err ( GraphQLError :: InvalidPassword ) ; } // Check email availability match repositories :: UserRepository :: find_one_by_email ( & email [ .. ] , & context . db_pool ) { Err ( e ) => return Err ( GraphQLError :: InternalServerError ( e ) ) , Ok ( Some ( _ ) ) => return Err ( GraphQLError :: AlreadyUsedEmail ) , _ => ( ) , } // Prepare the new user's data let user_id = uuid :: Uuid :: new_v4 ( ) ; let hash = match security :: hash_password ( password . as_bytes ( ) , context . config . security ( ) . hash_salt ( ) , ) { Err ( e ) => return Err ( GraphQLError :: InternalServerError ( e ) ) , Ok ( hash ) => hash , } ; let new_user = repositories :: NewUser { id : user_id , email , password : hash , } ; let user = match repositories :: UserRepository :: save ( & new_user , & context . db_pool ) { Err ( e ) => return Err ( GraphQLError :: InternalServerError ( e ) ) , Ok ( u ) => u , } ; // Sign token let token = match security :: sign_token ( user_id , context . config . security ( ) . token_expiration_time ( ) , context . config . security ( ) . secret_key ( ) , ) { Err ( e ) => return Err ( GraphQLError :: InternalServerError ( e ) ) , Ok ( token ) => token , } ; Ok ( token ) }
FIXME: Extract domain and repository logic to own module
Signup a new user.
FIXME : Extract domain and repository logic to own module Signup a new user .
context
&Context
input
SignupInput
context
&Context
input
SignupInput