async fn login(name: &str, password: &str, pg: &PgPool) -> Result<Player> {
    let name = name.trim();
    let password = password.trim();

    // We check if a name is associated with an account when the player is prompted to enter their
    // username at the start, thus we can guarantee that a single record will exist if this function
    // is called.
    let record = sqlx::query!(
        r#"select p.id, p.name, p.position, p.health, p.max_health, p.mana, p.max_mana, p.xp, p.level, p.afk, p.brief, a.id as "account_id", a.encrypted_password, a.email as "email?", a.roles
        from players p 
        join accounts a on p.account_id = a.id 
        where p.name = $1"#,
        name,
    )
    .fetch_one(&*pg)
    .await?;

    let argon = Argon2::default();
    let hash = PasswordHash::new(&record.encrypted_password)?;

    if argon.verify_password(password.as_bytes(), &hash).is_ok() {
        Ok(Player {
            _entityid: EntityId::empty(),
            id: record.id,
            account: Account {
                id: record.account_id,
                email: record.email,
                roles: Role::list(record.roles),
            },
            name: record.name.to_string(),
            position: Vec3::from(record.position),
            health: record.health,
            max_health: record.max_health,
            mana: record.mana,
            max_mana: record.max_mana,
            xp: record.xp,
            xp_to_level: record.level * 1000,
            level: record.level,
            brief: record.brief,
            afk: record.afk,
            dirty: false,
        })
    } else {
        Err(Error {
            kind: ErrorType::Authentication,
            message: "Invalid credentials".to_string(),
        })
    }
}
/// Attempts to log a player in by validating their password.
Attempts to log a player in by validating their password.
Attempts to log a player in by validating their password .
async fn login(name: &str, password: &str, pg: &PgPool) -> Result<Player> {
    let name = name.trim();
    let password = password.trim();
    // We check if a name is associated with an account when the player is prompted to enter their
    // username at the start, thus we can guarantee that a single record will exist if this function
    // is called.
    let record = sqlx::query!(
        r#"select p.id, p.name, p.position, p.health, p.max_health, p.mana, p.max_mana, p.xp, p.level, p.afk, p.brief, a.id as "account_id", a.encrypted_password, a.email as "email?", a.roles
        from players p 
        join accounts a on p.account_id = a.id 
        where p.name = $1"#,
        name,
    )
    .fetch_one(&*pg)
    .await?;
    let argon = Argon2::default();
    let hash = PasswordHash::new(&record.encrypted_password)?;
    if argon.verify_password(password.as_bytes(), &hash).is_ok() {
        Ok(Player {
            _entityid: EntityId::empty(),
            id: record.id,
            account: Account {
                id: record.account_id,
                email: record.email,
                roles: Role::list(record.roles),
            },
            name: record.name.to_string(),
            position: Vec3::from(record.position),
            health: record.health,
            max_health: record.max_health,
            mana: record.mana,
            max_mana: record.max_mana,
            xp: record.xp,
            xp_to_level: record.level * 1000,
            level: record.level,
            brief: record.brief,
            afk: record.afk,
            dirty: false,
        })
    } else {
        Err(Error {
            kind: ErrorType::Authentication,
            message: "Invalid credentials".to_string(),
        })
    }
}
async fn login ( name : & str , password : & str , pg : & PgPool ) -> Result < Player > { let name = name . trim ( ) ; let password = password . trim ( ) ; // We check if a name is associated with an account when the player is prompted to enter their // username at the start, thus we can guarantee that a single record will exist if this function // is called. let record = sqlx :: query ! ( r#"select p.id, p.name, p.position, p.health, p.max_health, p.mana, p.max_mana, p.xp, p.level, p.afk, p.brief, a.id as "account_id", a.encrypted_password, a.email as "email?", a.roles
        from players p 
        join accounts a on p.account_id = a.id 
        where p.name = $1"# name ) . fetch_one ( & * pg ) . await ? ; let argon = Argon2 :: default ( ) ; let hash = PasswordHash :: new ( & record . encrypted_password ) ? ; if argon . verify_password ( password . as_bytes ( ) , & hash ) . is_ok ( ) { Ok ( Player { _entityid : EntityId :: empty ( ) , id : record . id , account : Account { id : record . account_id , email : record . email , roles : Role :: list ( record . roles ) , } , name : record . name . to_string ( ) , position : Vec3 :: from ( record . position ) , health : record . health , max_health : record . max_health , mana : record . mana , max_mana : record . max_mana , xp : record . xp , xp_to_level : record . level * 1000 , level : record . level , brief : record . brief , afk : record . afk , dirty : false , } ) } else { Err ( Error { kind : ErrorType :: Authentication , message : " " . to_string ( ) , } ) } }
Attempts to log a player in by validating their password.
Attempts to log a player in by validating their password .
name
&str
password
&str
pg
&PgPool
name
&str
password
&str
pg
&PgPool