Skip to content

Commit a24e213

Browse files
authored
Add Auth Integration tests for new APIs (#709)
* Add Auth Integration tests for new APIs * Update Functions and Firestore tests which uses Auth APIs * Change `FirebaseUser.IsValid()` from a property to a method, to be aligned with `Credential.IsValid()`
1 parent 783063c commit a24e213

File tree

5 files changed

+311
-21
lines changed

5 files changed

+311
-21
lines changed

auth/testapp/Assets/Firebase/Sample/Auth/UIHandler.cs

Lines changed: 218 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,33 @@ protected void DisplaySignInResult(Firebase.Auth.SignInResult result, int indent
183183
}
184184
}
185185

186+
// Display user information reported
187+
protected void DisplayAuthResult(Firebase.Auth.AuthResult result, int indentLevel) {
188+
string indent = new String(' ', indentLevel * 2);
189+
DisplayDetailedUserInfo(result.User, indentLevel);
190+
var metadata = result.User != null ? result.User.Metadata : null;
191+
if (metadata != null) {
192+
DebugLog(String.Format("{0}Created: {1}", indent, metadata.CreationTimestamp));
193+
DebugLog(String.Format("{0}Last Sign-in: {1}", indent, metadata.LastSignInTimestamp));
194+
}
195+
var info = result.AdditionalUserInfo;
196+
if (info != null) {
197+
DebugLog(String.Format("{0}Additional User Info:", indent));
198+
DebugLog(String.Format("{0} User Name: {1}", indent, info.UserName));
199+
DebugLog(String.Format("{0} Provider ID: {1}", indent, info.ProviderId));
200+
DisplayProfile<string>(info.Profile, indentLevel + 1);
201+
}
202+
var credential = result.Credential;
203+
if (credential != null) {
204+
DebugLog(String.Format("{0}Credential:", indent));
205+
DebugLog(String.Format("{0} Is Valid?: {1}", indent, credential.IsValid()));
206+
DebugLog(String.Format("{0} Class Type: {1}", indent, credential.GetType()));
207+
if (credential.IsValid()) {
208+
DebugLog(String.Format("{0} Provider: {1}", indent, credential.Provider));
209+
}
210+
}
211+
}
212+
186213
// Display user information.
187214
protected void DisplayUserInfo(Firebase.Auth.IUserInfo userInfo, int indentLevel) {
188215
string indent = new String(' ', indentLevel * 2);
@@ -291,6 +318,27 @@ public Task CreateUserWithEmailAsync_DEPRECATED() {
291318
}).Unwrap();
292319
}
293320

321+
// Create a user with the email and password.
322+
public Task CreateUserWithEmailAsync() {
323+
DebugLog(String.Format("Attempting to create user {0}...", email));
324+
DisableUI();
325+
326+
// This passes the current displayName through to HandleCreateUserAsync
327+
// so that it can be passed to UpdateUserProfile(). displayName will be
328+
// reset by AuthStateChanged() when the new user is created and signed in.
329+
string newDisplayName = displayName;
330+
return auth.CreateUserWithEmailAndPasswordAsync(email, password)
331+
.ContinueWithOnMainThread((task) => {
332+
EnableUI();
333+
if (LogTaskCompletion(task, "User Creation")) {
334+
var user = task.Result.User;
335+
DisplayDetailedUserInfo(user, 1);
336+
return UpdateUserProfileAsync(newDisplayName: newDisplayName);
337+
}
338+
return task;
339+
}).Unwrap();
340+
}
341+
294342
// Update the user's display name with the currently selected display name.
295343
public Task UpdateUserProfileAsync(string newDisplayName = null) {
296344
if (auth.CurrentUser == null) {
@@ -325,6 +373,20 @@ public Task SigninWithEmailAsync_DEPRECATED() {
325373
}
326374
}
327375

376+
// Sign-in with an email and password.
377+
public Task SigninWithEmailAsync() {
378+
DebugLog(String.Format("Attempting to sign in as {0}...", email));
379+
DisableUI();
380+
if (signInAndFetchProfile) {
381+
return auth.SignInAndRetrieveDataWithCredentialAsync(
382+
Firebase.Auth.EmailAuthProvider.GetCredential(email, password)).ContinueWithOnMainThread(
383+
HandleSignInWithAuthResult);
384+
} else {
385+
return auth.SignInWithEmailAndPasswordAsync(email, password)
386+
.ContinueWithOnMainThread(HandleSignInWithAuthResult);
387+
}
388+
}
389+
328390
// This is functionally equivalent to the Signin() function. However, it
329391
// illustrates the use of Credentials, which can be aquired from many
330392
// different sources of authentication.
@@ -336,7 +398,24 @@ public Task SigninWithEmailCredentialAsync_DEPRECATED() {
336398
Firebase.Auth.EmailAuthProvider.GetCredential(email, password)).ContinueWithOnMainThread(
337399
HandleSignInWithSignInResult);
338400
} else {
339-
return auth.SignInWithCredentialAsync_DEPRECATED(
401+
return auth.SignInWithCredentialAsync(
402+
Firebase.Auth.EmailAuthProvider.GetCredential(email, password)).ContinueWithOnMainThread(
403+
HandleSignInWithUser);
404+
}
405+
}
406+
407+
// This is functionally equivalent to the Signin() function. However, it
408+
// illustrates the use of Credentials, which can be aquired from many
409+
// different sources of authentication.
410+
public Task SigninWithEmailCredentialAsync() {
411+
DebugLog(String.Format("Attempting to sign in as {0}...", email));
412+
DisableUI();
413+
if (signInAndFetchProfile) {
414+
return auth.SignInAndRetrieveDataWithCredentialAsync(
415+
Firebase.Auth.EmailAuthProvider.GetCredential(email, password)).ContinueWithOnMainThread(
416+
HandleSignInWithAuthResult);
417+
} else {
418+
return auth.SignInWithCredentialAsync(
340419
Firebase.Auth.EmailAuthProvider.GetCredential(email, password)).ContinueWithOnMainThread(
341420
HandleSignInWithUser);
342421
}
@@ -349,6 +428,13 @@ public Task SigninAnonymouslyAsync_DEPRECATED() {
349428
return auth.SignInAnonymouslyAsync_DEPRECATED().ContinueWithOnMainThread(HandleSignInWithUser);
350429
}
351430

431+
// Attempt to sign in anonymously.
432+
public Task SigninAnonymouslyAsync() {
433+
DebugLog("Attempting to sign anonymously...");
434+
DisableUI();
435+
return auth.SignInAnonymouslyAsync().ContinueWithOnMainThread(HandleSignInWithAuthResult);
436+
}
437+
352438
public void AuthenticateToGameCenter() {
353439
#if (UNITY_IOS || UNITY_TVOS)
354440
Social.localUser.Authenticate(success => {
@@ -385,6 +471,19 @@ void HandleSignInWithUser(Task<Firebase.Auth.FirebaseUser> task) {
385471
}
386472
}
387473

474+
// Called when a sign-in without fetching profile data completes.
475+
void HandleSignInWithAuthResult(Task<Firebase.Auth.AuthResult> task) {
476+
EnableUI();
477+
if (LogTaskCompletion(task, "Sign-in")) {
478+
if(task.Result.User != null && task.Result.User.IsValid()) {
479+
DisplayAuthResult(task.Result, 1);
480+
DebugLog(String.Format("{0} signed in", task.Result.User.DisplayName));
481+
} else {
482+
DebugLog("Signed in but User is either null or invalid");
483+
}
484+
}
485+
}
486+
388487
// Called when a sign-in with profile data completes.
389488
void HandleSignInWithSignInResult(Task<Firebase.Auth.SignInResult> task) {
390489
EnableUI();
@@ -422,6 +521,24 @@ protected Task LinkWithEmailCredentialAsync_DEPRECATED() {
422521
}
423522
}
424523

524+
// Link the current user with an email / password credential.
525+
protected Task LinkWithEmailCredentialAsync() {
526+
if (auth.CurrentUser == null) {
527+
DebugLog("Not signed in, unable to link credential to user.");
528+
var tcs = new TaskCompletionSource<bool>();
529+
tcs.SetException(new Exception("Not signed in"));
530+
return tcs.Task;
531+
}
532+
DebugLog("Attempting to link credential to user...");
533+
Firebase.Auth.Credential cred =
534+
Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
535+
return auth.CurrentUser.LinkWithCredentialAsync(cred).ContinueWithOnMainThread(task => {
536+
if (LogTaskCompletion(task, "Link Credential")) {
537+
DisplayDetailedUserInfo(task.Result.User, 1);
538+
}
539+
});
540+
}
541+
425542
// Reauthenticate the user with the current email / password.
426543
protected Task ReauthenticateAsync_DEPRECATED() {
427544
var user = auth.CurrentUser;
@@ -451,6 +568,35 @@ protected Task ReauthenticateAsync_DEPRECATED() {
451568
}
452569
}
453570

571+
// Reauthenticate the user with the current email / password.
572+
protected Task ReauthenticateAsync() {
573+
var user = auth.CurrentUser;
574+
if (user == null) {
575+
DebugLog("Not signed in, unable to reauthenticate user.");
576+
var tcs = new TaskCompletionSource<bool>();
577+
tcs.SetException(new Exception("Not signed in"));
578+
return tcs.Task;
579+
}
580+
DebugLog("Reauthenticating...");
581+
DisableUI();
582+
Firebase.Auth.Credential cred = Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
583+
if (signInAndFetchProfile) {
584+
return user.ReauthenticateAndRetrieveDataAsync(cred).ContinueWithOnMainThread(task => {
585+
EnableUI();
586+
if (LogTaskCompletion(task, "Reauthentication")) {
587+
DisplayAuthResult(task.Result, 1);
588+
}
589+
});
590+
} else {
591+
return user.ReauthenticateAsync(cred).ContinueWithOnMainThread(task => {
592+
EnableUI();
593+
if (LogTaskCompletion(task, "Reauthentication")) {
594+
DisplayDetailedUserInfo(auth.CurrentUser, 1);
595+
}
596+
});
597+
}
598+
}
599+
454600
// Reload the currently logged in user.
455601
public void ReloadUser() {
456602
if (auth.CurrentUser == null) {
@@ -509,6 +655,24 @@ protected Task UnlinkEmailAsync_DEPRECATED() {
509655
});
510656
}
511657

658+
// Unlink the email credential from the currently logged in user.
659+
protected Task UnlinkEmailAsync() {
660+
if (auth.CurrentUser == null) {
661+
DebugLog("Not signed in, unable to unlink");
662+
var tcs = new TaskCompletionSource<bool>();
663+
tcs.SetException(new Exception("Not signed in"));
664+
return tcs.Task;
665+
}
666+
DebugLog("Unlinking email credential");
667+
DisableUI();
668+
return auth.CurrentUser.UnlinkAsync(
669+
Firebase.Auth.EmailAuthProvider.GetCredential(email, password).Provider)
670+
.ContinueWithOnMainThread(task => {
671+
EnableUI();
672+
LogTaskCompletion(task, "Unlinking");
673+
});
674+
}
675+
512676
// Sign out the current user.
513677
protected void SignOut() {
514678
DebugLog("Signing out.");
@@ -671,6 +835,36 @@ protected void VerifyPhoneNumber_DEPRECATED() {
671835
});
672836
}
673837

838+
// Begin authentication with the phone number.
839+
protected void VerifyPhoneNumber() {
840+
var phoneAuthProvider = Firebase.Auth.PhoneAuthProvider.GetInstance(auth);
841+
phoneAuthProvider.VerifyPhoneNumber(
842+
new Firebase.Auth.PhoneAuthOptions {
843+
PhoneNumber = phoneNumber,
844+
TimeoutInMilliseconds = phoneAuthTimeoutMs,
845+
ForceResendingToken = null
846+
},
847+
verificationCompleted: (cred) => {
848+
DebugLog("Phone Auth, auto-verification completed");
849+
if (signInAndFetchProfile) {
850+
auth.SignInAndRetrieveDataWithCredentialAsync(cred).ContinueWithOnMainThread(
851+
HandleSignInWithAuthResult);
852+
} else {
853+
auth.SignInWithCredentialAsync(cred).ContinueWithOnMainThread(HandleSignInWithUser);
854+
}
855+
},
856+
verificationFailed: (error) => {
857+
DebugLog("Phone Auth, verification failed: " + error);
858+
},
859+
codeSent: (id, token) => {
860+
phoneAuthVerificationId = id;
861+
DebugLog("Phone Auth, code sent");
862+
},
863+
codeAutoRetrievalTimeOut: (id) => {
864+
DebugLog("Phone Auth, auto-verification timed out");
865+
});
866+
}
867+
674868
// Sign in using phone number authentication using code input by the user.
675869
protected void VerifyReceivedPhoneCode_DEPRECATED() {
676870
var phoneAuthProvider = Firebase.Auth.PhoneAuthProvider.GetInstance(auth);
@@ -680,7 +874,20 @@ protected void VerifyReceivedPhoneCode_DEPRECATED() {
680874
auth.SignInAndRetrieveDataWithCredentialAsync_DEPRECATED(cred).ContinueWithOnMainThread(
681875
HandleSignInWithSignInResult);
682876
} else {
683-
auth.SignInWithCredentialAsync_DEPRECATED(cred).ContinueWithOnMainThread(HandleSignInWithUser);
877+
auth.SignInWithCredentialAsync(cred).ContinueWithOnMainThread(HandleSignInWithUser);
878+
}
879+
}
880+
881+
// Sign in using phone number authentication using code input by the user.
882+
protected void VerifyReceivedPhoneCode() {
883+
var phoneAuthProvider = Firebase.Auth.PhoneAuthProvider.GetInstance(auth);
884+
// receivedCode should have been input by the user.
885+
var cred = phoneAuthProvider.GetCredential(phoneAuthVerificationId, receivedCode);
886+
if (signInAndFetchProfile) {
887+
auth.SignInAndRetrieveDataWithCredentialAsync(cred).ContinueWithOnMainThread(
888+
HandleSignInWithAuthResult);
889+
} else {
890+
auth.SignInWithCredentialAsync(cred).ContinueWithOnMainThread(HandleSignInWithUser);
684891
}
685892
}
686893

@@ -774,22 +981,22 @@ void GUIDisplayControls() {
774981
GUILayout.Space(20);
775982

776983
if (GUILayout.Button("Create User")) {
777-
CreateUserWithEmailAsync_DEPRECATED();
984+
CreateUserWithEmailAsync();
778985
}
779986
if (GUILayout.Button("Sign In Anonymously")) {
780-
SigninAnonymouslyAsync_DEPRECATED();
987+
SigninAnonymouslyAsync();
781988
}
782989
if (GUILayout.Button("Sign In With Email")) {
783-
SigninWithEmailAsync_DEPRECATED();
990+
SigninWithEmailAsync();
784991
}
785992
if (GUILayout.Button("Sign In With Email Credential")) {
786-
SigninWithEmailCredentialAsync_DEPRECATED();
993+
SigninWithEmailCredentialAsync();
787994
}
788995
if (GUILayout.Button("Link With Email Credential")) {
789-
LinkWithEmailCredentialAsync_DEPRECATED();
996+
LinkWithEmailCredentialAsync();
790997
}
791998
if (GUILayout.Button("Reauthenticate with Email")) {
792-
ReauthenticateAsync_DEPRECATED();
999+
ReauthenticateAsync();
7931000
}
7941001
GUIDisplayGameCenterControls();
7951002
if (GUILayout.Button("Reload User")) {
@@ -802,7 +1009,7 @@ void GUIDisplayControls() {
8021009
GetUserInfo();
8031010
}
8041011
if (GUILayout.Button("Unlink Email Credential")) {
805-
UnlinkEmailAsync_DEPRECATED();
1012+
UnlinkEmailAsync();
8061013
}
8071014
if (GUILayout.Button("Sign Out")) {
8081015
SignOut();
@@ -817,10 +1024,10 @@ void GUIDisplayControls() {
8171024
SendPasswordResetEmail();
8181025
}
8191026
if (GUILayout.Button("Authenticate Phone Number")) {
820-
VerifyPhoneNumber_DEPRECATED();
1027+
VerifyPhoneNumber();
8211028
}
8221029
if (GUILayout.Button("Verify Received Phone Code")) {
823-
VerifyReceivedPhoneCode_DEPRECATED();
1030+
VerifyReceivedPhoneCode();
8241031
}
8251032
if (GUILayout.Button(String.Format("Fetch Profile on Sign-in {0}",
8261033
signInAndFetchProfile ?

0 commit comments

Comments
 (0)