dittolive_ditto/identity/auth/
authenticator.rs1use crate::{
2 ditto::{Ditto, DittoFields},
3 error::{DittoError, ErrorKind},
4 utils::prelude::*,
5};
6
7#[derive(Clone, Debug)]
12pub struct AuthenticationClientFeedback {
13 pub feedback: Option<serde_json::Value>,
15}
16
17impl RefCounted for DittoAuthenticator {}
18
19#[derive(Clone)]
26pub struct DittoAuthenticator {
27 pub(crate) ditto_fields: std::sync::Weak<DittoFields>,
28}
29
30impl DittoAuthenticator {
31 #[doc(hidden)]
32 #[deprecated(note = "Use `ditto.auth()` instead")]
33 #[cfg(not(test))] pub fn new() -> Self {
35 DittoAuthenticator {
36 ditto_fields: std::sync::Weak::<DittoFields>::new(),
40 }
41 }
42
43 pub fn login(
50 &self,
51 token: &str,
52 provider: &str,
53 ) -> Result<AuthenticationClientFeedback, DittoError> {
54 let fields = self
55 .ditto_fields
56 .upgrade()
57 .ok_or(ErrorKind::ReleasedDittoInstance)?;
58 let c_token = char_p::new(token);
59 let c_provider = char_p::new(provider);
60 let result = ffi_sdk::ditto_auth_client_login_with_token_and_feedback(
61 &fields.ditto,
62 c_token.as_ref(),
63 c_provider.as_ref(),
64 );
65 fn parse_client_info(c: Option<char_p::Box>) -> AuthenticationClientFeedback {
66 AuthenticationClientFeedback {
67 feedback: c.map(|it| serde_json::from_str(it.to_str()).unwrap()),
68 }
69 }
70
71 match result.status_code {
72 0 => Ok(parse_client_info(result.c_string)),
73 _ => Err(DittoError::from_authentication_feedback(parse_client_info(
74 result.c_string,
75 ))),
76 }
77 }
78
79 #[doc(hidden)]
80 #[deprecated(note = "Use `ditto.auth().login(...)` instead")]
81 pub fn login_with_token_and_feedback(
82 &self,
83 token: &str,
84 provider: &str,
85 ) -> Result<AuthenticationClientFeedback, DittoError> {
86 self.login(token, provider)
87 }
88
89 pub fn logout<R>(&self, cleanup: impl FnOnce(Ditto) -> R) -> Result<R, DittoError> {
94 let fields = self
95 .ditto_fields
96 .upgrade()
97 .ok_or(ErrorKind::ReleasedDittoInstance)?;
98 let status = ffi_sdk::ditto_auth_client_logout(&fields.ditto);
99 if status != 0 {
100 return Err(DittoError::from_ffi(ErrorKind::Authentication));
101 }
102 let ditto = Ditto::new_temp(fields);
103 ditto.stop_sync();
104 let ret = cleanup(ditto);
105 Ok(ret)
106 }
107
108 pub fn is_authenticated(&self) -> bool {
115 match self.ditto_fields.upgrade() {
116 None => false,
117 Some(fields) => ffi_sdk::ditto_auth_client_is_web_valid(&fields.ditto) != 0,
118 }
119 }
120
121 pub fn user_id(&self) -> Option<String> {
127 let fields = self.ditto_fields.upgrade()?;
128 let c_msg = ffi_sdk::ditto_auth_client_user_id(&fields.ditto)?;
129 Some(c_msg.into_string())
130 }
131}