pub async fn create_token(&self, username: String, password: String) -> Result<Tokens> {
        let user = self.user_service.find_by_username(&username).await?;
        let is_valid_password = argon2::verify_encoded(&user.password_hash, password.as_bytes())?;

        if is_valid_password {
            let iat = Utc::now().timestamp() as usize;
            let exp = Utc::now()
                .checked_add_signed(Duration::days(30))
                .unwrap()
                .timestamp() as usize;

            let claims = Claims {
                sub: String::from("nexus"),
                iat,
                exp,
                uid: username,
            };

            let access_token = encode(
                &Header::default(),
                &claims,
                &EncodingKey::from_secret(&self.jwt_secret),
            )?;

            return Ok(Tokens { access_token });
        }

        Err(Error::code(ErrorCode::InvalidCredentials))
    }
/// Validate provided `username` and `password`. If valid, fetches the
/// corresponding user and signs a JSON Web Token.
Validate provided `username` and `password`. If valid, fetches the
corresponding user and signs a JSON Web Token.
Validate provided ` username ` and ` password ` . If valid fetches the corresponding user and signs a JSON Web Token .
pub async fn create_token(&self, username: String, password: String) -> Result<Tokens> {
        let user = self.user_service.find_by_username(&username).await?;
        let is_valid_password = argon2::verify_encoded(&user.password_hash, password.as_bytes())?;
        if is_valid_password {
            let iat = Utc::now().timestamp() as usize;
            let exp = Utc::now()
                .checked_add_signed(Duration::days(30))
                .unwrap()
                .timestamp() as usize;
            let claims = Claims {
                sub: String::from("nexus"),
                iat,
                exp,
                uid: username,
            };
            let access_token = encode(
                &Header::default(),
                &claims,
                &EncodingKey::from_secret(&self.jwt_secret),
            )?;
            return Ok(Tokens { access_token });
        }
        Err(Error::code(ErrorCode::InvalidCredentials))
    }
pub async fn create_token ( & self , username : String , password : String ) -> Result < Tokens > { let user = self . user_service . find_by_username ( & username ) . await ? ; let is_valid_password = argon2 :: verify_encoded ( & user . password_hash , password . as_bytes ( ) ) ? ; if is_valid_password { let iat = Utc :: now ( ) . timestamp ( ) as usize ; let exp = Utc :: now ( ) . checked_add_signed ( Duration :: days ( 30 ) ) . unwrap ( ) . timestamp ( ) as usize ; let claims = Claims { sub : String :: from ( " " ) , iat , exp , uid : username , } ; let access_token = encode ( & Header :: default ( ) , & claims , & EncodingKey :: from_secret ( & self . jwt_secret ) , ) ? ; return Ok ( Tokens { access_token } ) ; } Err ( Error :: code ( ErrorCode :: InvalidCredentials ) ) }
Validate provided `username` and `password`.
Validate provided ` username ` and ` password ` .
&self
username
String
password
String
&self
username
String
password
String