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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::{
api::{Api, EmailOrPhone},
error::Error,
session::Session,
user_attributes::UserAttributes,
user_update::UserUpdate,
};
pub struct Client {
current_session: Option<Session>,
api: Api,
}
impl Client {
/// Creates a GoTrue Client.
///
/// # Example
///
/// ```
/// use go_true::Client;
///
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
/// ```
pub fn new(url: String) -> Client {
Client {
current_session: None,
api: Api::new(url),
}
}
/// Signs up a new user.
///
/// # Example
///
/// ```
/// use go_true::{Client, EmailOrPhone};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
/// let email = "some_email".to_string();
/// let password = "some_password".to_string();
/// let res = client
/// .sign_up(EmailOrPhone::Email(email), &password)
/// .await?;
/// Ok(())
/// }
pub async fn sign_up(
&mut self,
email_or_phone: EmailOrPhone,
password: &String,
) -> Result<Session, Error> {
self.current_session = None;
let result = self.api.sign_up(email_or_phone, &password).await;
match result {
Ok(session) => {
self.current_session = Some(session.clone());
return Ok(session);
}
Err(e) => {
if e.is_status() && e.status().unwrap().as_str() == "400" {
return Err(Error::AlreadySignedUp);
}
return Err(Error::InternalError);
}
}
}
/// Signs in a user.
///
/// # Example
///
/// ```
/// use go_true::{Client, EmailOrPhone};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
/// let email = "some_email".to_string();
/// let password = "some_password".to_string();
/// let res = client
/// .sign_in(EmailOrPhone::Email(email), &password)
/// .await?;
/// Ok(())
/// }
pub async fn sign_in(
&mut self,
email_or_phone: EmailOrPhone,
password: &String,
) -> Result<Session, Error> {
self.current_session = None;
let result = self.api.sign_in(email_or_phone, &password).await;
match result {
Ok(session) => {
self.current_session = Some(session.clone());
return Ok(session);
}
Err(e) => {
if e.is_status() && e.status().unwrap().as_str() == "400" {
return Err(Error::WrongCredentials);
}
return Err(Error::InternalError);
}
}
}
/// Sends an OTP
///
/// # Example
///
/// ```
/// use go_true::{Client, EmailOrPhone};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
/// let email = "some_email".to_string();
///
/// let res = client
/// .send_otp(EmailOrPhone::Email(email), None)
/// .await?;
/// Ok(())
/// }
pub async fn send_otp(
&self,
email_or_phone: EmailOrPhone,
should_create_user: Option<bool>,
) -> Result<bool, Error> {
let result = self.api.send_otp(email_or_phone, should_create_user).await;
match result {
Ok(_) => return Ok(true),
Err(e) => {
if e.is_status() && e.status().unwrap().as_str() == "422" {
return Err(Error::UserNotFound);
}
return Err(Error::InternalError);
}
}
}
pub async fn verify_otp<T: serde::Serialize>(&mut self, params: T) -> Result<bool, Error> {
self.current_session = None;
let result = self.api.verify_otp(params).await;
match result {
Ok(_) => return Ok(true),
Err(e) => {
if e.is_status() && e.status().unwrap().as_str() == "400" {
return Err(Error::WrongToken);
}
return Err(Error::InternalError);
}
}
}
/// Sign out the current user
///
/// # Example
///
/// ```
/// use go_true::{Client};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
///
/// // Sign in first
///
/// let res = client.sign_out().await?;
/// Ok(())
/// }
pub async fn sign_out(&self) -> Result<bool, Error> {
let result = match &self.current_session {
Some(session) => self.api.sign_out(&session.access_token).await,
None => return Err(Error::NotAuthenticated),
};
match result {
Ok(_) => return Ok(true),
Err(_) => return Err(Error::InternalError),
}
}
/// Reset a user's password for an email address
///
/// # Example
///
/// ```
/// use go_true::{Client};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
/// let email = "some_email".to_string()
///
/// let res = client.reset_password_for_email(&email).await?;
/// Ok(())
/// }
pub async fn reset_password_for_email(&self, email: &str) -> Result<bool, Error> {
let result = self.api.reset_password_for_email(&email).await;
match result {
Ok(_) => return Ok(true),
Err(_) => return Err(Error::UserNotFound),
}
}
pub async fn update_user(&self, user: UserAttributes) -> Result<UserUpdate, Error> {
let session = match &self.current_session {
Some(s) => s,
None => return Err(Error::NotAuthenticated),
};
let result = self.api.update_user(user, &session.access_token).await;
match result {
Ok(user) => return Ok(user),
Err(e) => {
if e.is_status() && e.status().unwrap().as_str() == "400" {
return Err(Error::UserNotFound);
}
return Err(Error::InternalError);
}
}
}
/// Refreshes the current session
///
/// # Example
///
/// ```
/// use go_true::{Client};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
///
/// // sign in first
///
/// client.refresh_session().await?:
/// Ok(())
/// }
pub async fn refresh_session(&mut self) -> Result<Session, Error> {
if self.current_session.is_none() {
return Err(Error::NotAuthenticated);
}
let result = match &self.current_session {
Some(session) => self.api.refresh_access_token(&session.refresh_token).await,
None => return Err(Error::MissingRefreshToken),
};
let session = match result {
Ok(session) => session,
Err(_) => return Err(Error::InternalError),
};
self.current_session = Some(session.clone());
return Ok(session);
}
/// Sets a session by refresh token
///
/// # Example
///
/// ```
/// use go_true::{Client};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("http://your.gotrue.endpoint".to_string());
/// let token = "refresh_token".to_string();
///
/// let session = client.set_session(token).await?:
/// Ok(())
/// }
pub async fn set_session(&mut self, refresh_token: &str) -> Result<Session, Error> {
if refresh_token.len() < 1 {
return Err(Error::NotAuthenticated);
}
let result = self.api.refresh_access_token(refresh_token).await;
let session = match result {
Ok(session) => session,
Err(_) => return Err(Error::InternalError),
};
self.current_session = Some(session.clone());
return Ok(session);
}
}