Created
October 21, 2022 15:43
-
-
Save Mr-Malomz/24eb0f1b42efc1f79bff95cb6b560a9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//imports goes here | |
pub struct TwilioService {} | |
impl TwilioService { | |
fn env_loader(key: &str) -> String { | |
//code goes here | |
} | |
pub async fn send_otp(phone_number: &String) -> Result<OTPResponse, &'static str> { | |
//code goes here | |
} | |
//add | |
pub async fn verify_otp(phone_number: &String, code: &String) -> Result<(), &'static str> { | |
let account_sid = TwilioService::env_loader("TWILIO_ACCOUNT_SID"); | |
let auth_token = TwilioService::env_loader("TWILIO_AUTHTOKEN"); | |
let service_id = TwilioService::env_loader("TWILIO_SERVICES_ID"); | |
let url = format!( | |
"https://verify.twilio.com/v2/Services/{serv_id}/VerificationCheck", | |
serv_id = service_id, | |
); | |
let mut headers = header::HeaderMap::new(); | |
headers.insert( | |
"Content-Type", | |
"application/x-www-form-urlencoded".parse().unwrap(), | |
); | |
let mut form_body: HashMap<&str, &String> = HashMap::new(); | |
form_body.insert("To", phone_number); | |
form_body.insert("Code", code); | |
let client = Client::new(); | |
let res = client | |
.post(url) | |
.basic_auth(account_sid, Some(auth_token)) | |
.headers(headers) | |
.form(&form_body) | |
.send() | |
.await; | |
match res { | |
Ok(response) => { | |
let data = response.json::<OTPVerifyResponse>().await; | |
match data { | |
Ok(result) => { | |
if result.status == "approved" { | |
Ok(()) | |
} else { | |
Err("Error verifying OTP") | |
} | |
} | |
Err(_) => Err("Error verifying OTP"), | |
} | |
} | |
Err(_) => Err("Error verifying OTP"), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment