1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use serde::Deserialize;
use wasm_bindgen::{prelude::*, JsCast};
impl User {
pub async fn get_id_token_result(&self, force_refresh: bool) -> Result<IdTokenResult, JsValue> {
self.get_id_token_result_js(force_refresh)
.await
.map(Into::into)
}
pub async fn get_id_token(&self, force_refresh: bool) -> Result<String, JsValue> {
self.get_id_token_js(force_refresh)
.await
.map(JsCast::unchecked_into::<js_sys::JsString>)
.map(|token| String::try_from(token).expect("token to be a string"))
}
}
impl ParsedToken {
pub fn custom_claims<T>(&self) -> Result<T, serde_wasm_bindgen::Error>
where
T: for<'de> Deserialize<'de>,
{
serde_wasm_bindgen::from_value::<T>(self.unchecked_ref::<JsValue>().to_owned())
}
}
#[wasm_bindgen(module = "firebase/auth")]
extern "C" {
#[derive(Debug)]
#[wasm_bindgen(extends = UserInfo, typescript_type = r#"import("firebase/auth").User"#)]
pub type User;
#[derive(Debug)]
pub type UserMetadata;
#[derive(Debug)]
pub type UserInfo;
#[derive(Debug)]
pub type IdTokenResult;
#[derive(Debug)]
pub type ParsedToken;
pub type Firebase;
#[wasm_bindgen(method, getter, js_name = displayName)]
pub fn display_name(this: &UserInfo) -> Option<String>;
#[wasm_bindgen(method, getter, js_name = email)]
pub fn email(this: &UserInfo) -> Option<String>;
#[wasm_bindgen(method, getter, js_name = phoneNumber)]
pub fn phone_number(this: &UserInfo) -> Option<String>;
#[wasm_bindgen(method, getter, js_name = photoURL)]
pub fn photo_url(this: &UserInfo) -> Option<String>;
#[wasm_bindgen(method, getter, js_name = providerId)]
pub fn provider_id(this: &UserInfo) -> String;
#[wasm_bindgen(method, getter, js_name = uid)]
pub fn uid(this: &UserInfo) -> String;
#[wasm_bindgen(method, getter, js_name = creationTime)]
pub fn creation_time(this: &UserMetadata) -> String;
#[wasm_bindgen(method, getter, js_name = lastSignInTime)]
pub fn last_sign_in_time(this: &UserMetadata) -> String;
#[wasm_bindgen(method, getter, js_name = emailVerified)]
pub fn email_verified(this: &User) -> bool;
#[wasm_bindgen(method, getter, js_name = isAnonymous)]
pub fn is_anonymous(this: &User) -> bool;
#[wasm_bindgen(getter)]
pub fn metadata(this: &User) -> UserMetadata;
#[wasm_bindgen(method, getter, js_name = providerData)]
pub fn provider_data(this: &User) -> Vec<UserInfo>;
#[wasm_bindgen(method, getter, js_name = refreshToken)]
pub fn refresh_token(this: &User) -> String;
#[wasm_bindgen(method, getter, js_name = tenantId)]
pub fn tenant_id(this: &User) -> String;
#[wasm_bindgen(method, catch)]
async fn delete(this: &User) -> Result<(), JsValue>;
#[wasm_bindgen(method, js_name = getIdToken, catch)]
async fn get_id_token_js(this: &User, force_refresh: bool) -> Result<JsValue, JsValue>;
#[wasm_bindgen(method, js_name = getIdTokenResult, catch)]
async fn get_id_token_result_js(this: &User, force_refresh: bool) -> Result<JsValue, JsValue>;
#[wasm_bindgen(method, catch)]
async fn reload(this: &User) -> Result<(), JsValue>;
#[wasm_bindgen(method, js_name = toJSON)]
pub fn to_json(this: &User) -> js_sys::Object;
#[wasm_bindgen(method, getter, js_name = authTime)]
pub fn auth_time(this: &IdTokenResult) -> String;
#[wasm_bindgen(method, getter, js_name = expirationTime)]
pub fn expiration_time(this: &IdTokenResult) -> String;
#[wasm_bindgen(method, getter, js_name = issuedAtTime)]
pub fn issued_at_time(this: &IdTokenResult) -> String;
#[wasm_bindgen(method, getter, js_name = signInProvider)]
pub fn sign_in_provider(this: &IdTokenResult) -> Option<String>;
#[wasm_bindgen(method, getter, js_name = signInSecondFactor)]
pub fn sign_in_second_factor(this: &IdTokenResult) -> Option<String>;
#[wasm_bindgen(method, getter, js_name = token)]
pub fn token(this: &IdTokenResult) -> Option<String>;
#[wasm_bindgen(method, getter, js_name = claims)]
pub fn claims(this: &IdTokenResult) -> ParsedToken;
#[wasm_bindgen(method, getter)]
pub fn exp(this: &ParsedToken) -> Option<String>;
#[wasm_bindgen(method, getter)]
pub fn sub(this: &ParsedToken) -> Option<String>;
#[wasm_bindgen(method, getter)]
pub fn auth_time(this: &ParsedToken) -> Option<String>;
#[wasm_bindgen(method, getter)]
pub fn iat(this: &ParsedToken) -> Option<String>;
#[wasm_bindgen(method, getter)]
pub fn firebase(this: &ParsedToken) -> Option<Firebase>;
#[wasm_bindgen(method, getter)]
pub fn sign_in_provider(this: &Firebase) -> Option<Firebase>;
#[wasm_bindgen(method, getter)]
pub fn sign_in_second_factor(this: &Firebase) -> Option<Firebase>;
#[wasm_bindgen(method, getter)]
pub fn identities(this: &Firebase) -> Option<js_sys::Object>;
}