supabase-auth 0.10.3

Supabase Auth implementation following the official client libraries.
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use core::time;
use std::{collections::HashMap, env, thread};

use supabase_auth::models::{
    AuthClient, LoginWithOAuthOptions, LoginWithSSO, LogoutScope, ResendParams,
    SignUpWithPasswordOptions, UpdatedUser,
};

fn create_test_client() -> AuthClient {
    AuthClient::new_from_env().unwrap()
}

#[tokio::test]
async fn create_client_test_valid() {
    let auth_client = AuthClient::new_from_env().unwrap();

    assert!(*auth_client.project_url() == env::var("SUPABASE_URL").unwrap())
}

#[tokio::test]
async fn test_login_with_email() {
    let auth_client = create_test_client();

    let demo_email = env::var("DEMO_EMAIL").unwrap();
    let demo_password = env::var("DEMO_PASSWORD").unwrap();

    let session = auth_client
        .login_with_email(&demo_email, &demo_password)
        .await
        .unwrap();

    assert!(session.user.email == demo_email)
}

#[tokio::test]
async fn test_login_with_email_invalid() {
    let auth_client = create_test_client();

    let demo_email = "invalid@demo.com";
    let demo_password = "invalid";

    let session = auth_client
        .login_with_email(demo_email, demo_password)
        .await;

    assert!(session.is_err())
}

#[tokio::test]
async fn sign_up_with_email_test_valid() {
    let auth_client = create_test_client();

    let uuid = uuid::Uuid::now_v7();

    let demo_email = format!("signup__{}@demo.com", uuid);
    let demo_password = "ciJUAojfZZYKfCxkiUWH";

    let session = auth_client
        .sign_up_with_email_and_password(demo_email.as_ref(), demo_password, None)
        .await
        .unwrap();

    // Wait to prevent running into Supabase rate limits when running cargo test
    let one_minute = time::Duration::from_secs(60);
    thread::sleep(one_minute);

    assert!(session.user.email == demo_email)
}

#[tokio::test]
async fn test_mobile_flow() {
    let auth_client = create_test_client();

    let demo_phone = env::var("DEMO_PHONE").unwrap();
    let demo_password = env::var("DEMO_PASSWORD").unwrap();

    let options = SignUpWithPasswordOptions {
        email_redirect_to: Some(String::from("a_random_url")),
        ..Default::default()
    };

    let session = auth_client
        .sign_up_with_phone_and_password(&demo_phone, &demo_password, Some(options))
        .await;

    if session.is_err() {
        eprintln!("{:?}", session.as_ref().unwrap_err())
    }

    assert!(session.is_ok());

    let new_session = auth_client
        .login_with_phone(&demo_phone, &demo_password)
        .await;

    if new_session.is_err() {
        eprintln!("{:?}", new_session.as_ref().unwrap_err())
    }

    assert!(new_session.is_ok() && new_session.unwrap().user.phone == demo_phone);

    let response = auth_client.send_sms_with_otp(&demo_phone).await;

    if response.is_err() {
        eprintln!("{:?}", response.as_ref().unwrap_err())
    }

    assert!(response.is_ok())
}

#[tokio::test]
async fn send_login_email_with_magic_link() {
    let auth_client = create_test_client();

    let demo_email = env::var("DEMO_EMAIL").unwrap();

    let response = auth_client
        .send_login_email_with_magic_link(&demo_email)
        .await;

    if response.is_err() {
        eprintln!("{:?}", response.as_ref().unwrap_err())
    }

    // Wait to prevent running into Supabase rate limits when running cargo test
    let one_minute = time::Duration::from_secs(60);
    thread::sleep(one_minute);

    assert!(response.is_ok())
}

#[tokio::test]
async fn send_email_with_otp() {
    let auth_client = create_test_client();

    let demo_email = env::var("DEMO_EMAIL").unwrap();

    let response = auth_client.send_email_with_otp(&demo_email, None).await;

    if response.is_err() {
        eprintln!("{:?}", response.as_ref().unwrap_err())
    }

    // Wait to prevent running into Supabase rate limits when running cargo test
    let one_minute = time::Duration::from_secs(60);
    thread::sleep(one_minute);

    assert!(response.is_ok())
}

#[tokio::test]
async fn login_with_oauth_test() {
    let auth_client = create_test_client();

    let mut params = HashMap::new();
    params.insert("key".to_string(), "value".to_string());
    params.insert("second_key".to_string(), "second_value".to_string());
    params.insert("third_key".to_string(), "third_value".to_string());

    let options = LoginWithOAuthOptions {
        query_params: Some(params),
        redirect_to: Some("localhost".to_string()),
        scopes: Some("repo gist notifications".to_string()),
        skip_brower_redirect: Some(true),
    };

    let response = auth_client
        .login_with_oauth(supabase_auth::models::Provider::Github, Some(options))
        .await;

    if response.is_err() {
        println!("SIGN IN WITH OAUTH TEST RESPONSE -- \n{:?}", response);
    }

    assert!(response.unwrap().url.to_string().len() > 1);
}

#[ignore]
#[tokio::test]
async fn sign_up_with_oauth_test() {
    let auth_client = create_test_client();

    let mut params = HashMap::new();
    params.insert("key".to_string(), "value".to_string());
    params.insert("second_key".to_string(), "second_value".to_string());
    params.insert("third_key".to_string(), "third_value".to_string());

    let options = LoginWithOAuthOptions {
        query_params: Some(params),
        redirect_to: Some("localhost".to_string()),
        scopes: Some("repo gist notifications".to_string()),
        skip_brower_redirect: Some(true),
    };

    let response = auth_client
        .sign_up_with_oauth(supabase_auth::models::Provider::Github, Some(options))
        .await;

    if response.is_err() {
        println!("SIGN IN WITH OAUTH TEST RESPONSE -- \n{:?}", response);
    }

    assert!(response.unwrap().url.to_string().len() > 1);
}

#[tokio::test]
async fn login_with_oauth_no_options_test() {
    let auth_client = create_test_client();

    // // Must login to get a user bearer token
    // let demo_email = env::var("DEMO_EMAIL").unwrap();
    // let demo_password = env::var("DEMO_PASSWORD").unwrap();
    //
    // let session = auth_client
    //     .login_with_email(demo_email, demo_password)
    //     .await;
    //
    // if session.is_err() {
    //     eprintln!("{:?}", session.as_ref().unwrap_err())
    // }

    let response = auth_client
        .login_with_oauth(supabase_auth::models::Provider::Github, None)
        .await;

    println!(
        "SIGN IN WITH OAUTH \n NO OPTIONS TEST RESPONSE -- \n{:?}",
        response
    );

    if response.is_err() {
        eprintln!("{:?}", response.as_ref().unwrap_err())
    }

    assert!(response.is_ok())
}

#[tokio::test]
async fn get_user_test() {
    let auth_client = create_test_client();

    // Must login to get a user bearer token
    let demo_email = env::var("DEMO_EMAIL").unwrap();
    let demo_password = env::var("DEMO_PASSWORD").unwrap();

    let session = auth_client
        .login_with_email(&demo_email, &demo_password)
        .await;

    if session.is_err() {
        eprintln!("{:?}", session.as_ref().unwrap_err())
    }

    let user = auth_client
        .get_user(&session.unwrap().access_token)
        .await
        .unwrap();

    assert!(user.email == demo_email)
}

#[tokio::test]
async fn update_user_test() {
    let auth_client = create_test_client();

    // Must login to get a user bearer token
    let demo_email = env::var("DEMO_EMAIL").unwrap();
    let demo_password = env::var("DEMO_PASSWORD").unwrap();

    let session = auth_client
        .login_with_email(&demo_email, &demo_password)
        .await
        .unwrap();

    eprintln!("{:?}", session);

    let updated_user = UpdatedUser {
        email: Some(demo_email.clone()),
        password: Some("qqqqwwww".to_string()),
        data: None,
    };

    let first_response = auth_client
        .update_user(updated_user, &session.access_token)
        .await;

    if first_response.is_err() {
        eprintln!("{:?}", first_response.as_ref().unwrap_err())
    }

    // Login with new password to validate the change
    let test_password = "qqqqwwww";

    let new_session = auth_client
        .login_with_email(demo_email.as_ref(), test_password)
        .await;

    if new_session.is_err() {
        eprintln!("{:?}", new_session.as_ref().unwrap_err())
    }

    // Return the user to original condition
    let original_user = UpdatedUser {
        email: Some(demo_email),
        password: Some("qwerqwer".to_string()),
        data: None,
    };

    let second_response = auth_client
        .update_user(original_user, &new_session.unwrap().access_token)
        .await;

    assert!(second_response.is_ok())
}

#[tokio::test]
async fn exchange_token_for_session() {
    let auth_client = create_test_client();

    let demo_email = env::var("DEMO_EMAIL").unwrap();
    let demo_password = env::var("DEMO_PASSWORD").unwrap();

    let original_session = auth_client
        .login_with_email(&demo_email, &demo_password)
        .await
        .unwrap();

    assert!(original_session.user.email == demo_email);

    let new_session = auth_client
        .refresh_session(&original_session.refresh_token)
        .await
        .unwrap();

    assert!(new_session.user.email == demo_email)
}

#[tokio::test]
async fn reset_password_for_email_test() {
    let auth_client = create_test_client();

    let demo_email = env::var("DEMO_EMAIL").unwrap();

    let response = auth_client.reset_password_for_email(&demo_email).await;

    // Wait to prevent running into Supabase rate limits when running cargo test
    let one_minute = time::Duration::from_secs(60);
    thread::sleep(one_minute);

    assert!(response.is_ok())
}

#[tokio::test]
async fn resend_email_test() {
    let auth_client = create_test_client();

    let uuid = uuid::Uuid::now_v7();

    let demo_email = format!("signup__{}@demo.com", uuid);
    let demo_password = "ciJUAojfZZYKfCxkiUWH";

    let session = auth_client
        .sign_up_with_email_and_password(&demo_email, demo_password, None)
        .await;

    if session.is_err() {
        eprintln!("{:?}", session.as_ref().unwrap_err())
    }

    let credentials = ResendParams {
        otp_type: supabase_auth::models::OtpType::Signup,
        email: demo_email.to_owned(),
        options: None,
    };

    // Wait to prevent running into Supabase rate limits when running cargo test
    let one_minute = time::Duration::from_secs(60);
    thread::sleep(one_minute);

    let response = auth_client.resend(credentials).await;

    if response.is_err() {
        println!("{:?}", response)
    }

    assert!(response.is_ok() && session.unwrap().user.email == demo_email)
}

#[tokio::test]
async fn logout_test() {
    let auth_client = create_test_client();

    let demo_email = env::var("DEMO_EMAIL").unwrap();
    let demo_password = env::var("DEMO_PASSWORD").unwrap();

    let session = auth_client
        .login_with_email(&demo_email, &demo_password)
        .await
        .unwrap();

    let logout = auth_client
        .logout(Some(LogoutScope::Global), &session.access_token)
        .await;

    if logout.is_err() {
        println!("{:?}", logout)
    }

    assert!(logout.is_ok())
}

#[tokio::test]
async fn test_sso_login() {
    let auth_client = create_test_client();
    let demo_domain = env::var("DEMO_DOMAIN").unwrap();
    let params = LoginWithSSO {
        domain: Some(demo_domain),
        options: None,
        provider_id: None,
    };

    let url = auth_client.sso(params).await.unwrap();

    println!("{}", url);

    assert!(url.to_string().len() > 1);
}

#[tokio::test]
async fn invite_by_email_test() {
    let auth_client = create_test_client();

    let demo_email = env::var("DEMO_INVITE").unwrap();

    let user = auth_client
        // NOTE: Requires admin permissions to issue invites
        .invite_user_by_email(&demo_email, None, auth_client.api_key())
        .await
        .unwrap();

    assert!(user.email == demo_email)
}

#[tokio::test]
async fn login_anonymously_test() {
    let auth_client = create_test_client();

    let session = auth_client.login_anonymously(None).await.unwrap();

    println!("{}", session.user.created_at);

    assert!(!session.access_token.is_empty() && session.user.role == "authenticated")
}

#[tokio::test]
async fn get_settings_test() {
    let auth_client = create_test_client();

    let settings = auth_client.get_settings().await.unwrap();

    assert!(settings.external.github)
}

#[tokio::test]
async fn get_health_test() {
    let auth_client = create_test_client();

    let health = auth_client.get_health().await.unwrap();

    assert!(!health.description.is_empty())
}