firebase_rs_sdk/auth/phone/
mod.rs1use std::sync::Arc;
2
3use crate::auth::api::Auth;
4use crate::auth::error::AuthResult;
5use crate::auth::types::{ApplicationVerifier, ConfirmationResult, MultiFactorAssertion};
6
7pub const PHONE_PROVIDER_ID: &str = "phone";
9
10#[derive(Clone, Debug)]
12pub struct PhoneAuthCredential {
13 verification_id: String,
14 verification_code: String,
15}
16
17impl PhoneAuthCredential {
18 pub fn new(verification_id: impl Into<String>, verification_code: impl Into<String>) -> Self {
20 Self {
21 verification_id: verification_id.into(),
22 verification_code: verification_code.into(),
23 }
24 }
25
26 pub fn verification_id(&self) -> &str {
28 &self.verification_id
29 }
30
31 pub fn verification_code(&self) -> &str {
33 &self.verification_code
34 }
35
36 pub(crate) fn into_parts(self) -> (String, String) {
37 (self.verification_id, self.verification_code)
38 }
39}
40
41pub struct PhoneAuthProvider {
43 auth: Arc<Auth>,
44}
45
46impl PhoneAuthProvider {
47 pub fn new(auth: Arc<Auth>) -> Self {
49 Self { auth }
50 }
51
52 pub async fn verify_phone_number(
54 &self,
55 phone_number: &str,
56 verifier: Arc<dyn ApplicationVerifier>,
57 ) -> AuthResult<String> {
58 self.auth
59 .send_phone_verification_code(phone_number, verifier)
60 .await
61 }
62
63 pub fn credential(
65 verification_id: impl Into<String>,
66 verification_code: impl Into<String>,
67 ) -> PhoneAuthCredential {
68 PhoneAuthCredential::new(verification_id, verification_code)
69 }
70
71 pub fn credential_from_confirmation(
73 confirmation: &ConfirmationResult,
74 verification_code: impl Into<String>,
75 ) -> PhoneAuthCredential {
76 PhoneAuthCredential::new(confirmation.verification_id(), verification_code.into())
77 }
78
79 pub async fn sign_in_with_credential(
81 &self,
82 credential: PhoneAuthCredential,
83 ) -> AuthResult<crate::auth::UserCredential> {
84 self.auth.sign_in_with_phone_credential(credential).await
85 }
86
87 pub async fn link_with_credential(
89 &self,
90 credential: PhoneAuthCredential,
91 ) -> AuthResult<crate::auth::UserCredential> {
92 self.auth.link_with_phone_credential(credential).await
93 }
94
95 pub async fn reauthenticate_with_credential(
97 &self,
98 credential: PhoneAuthCredential,
99 ) -> AuthResult<Arc<crate::auth::User>> {
100 self.auth
101 .reauthenticate_with_phone_credential(credential)
102 .await
103 }
104}
105
106pub struct PhoneMultiFactorGenerator;
111
112impl PhoneMultiFactorGenerator {
113 pub fn assertion(credential: PhoneAuthCredential) -> MultiFactorAssertion {
115 MultiFactorAssertion::from_phone_credential(credential)
116 }
117
118 pub const FACTOR_ID: &'static str = PHONE_PROVIDER_ID;
120}