@@ -183,6 +183,33 @@ protected void DisplaySignInResult(Firebase.Auth.SignInResult result, int indent
183
183
}
184
184
}
185
185
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
+
186
213
// Display user information.
187
214
protected void DisplayUserInfo ( Firebase . Auth . IUserInfo userInfo , int indentLevel ) {
188
215
string indent = new String ( ' ' , indentLevel * 2 ) ;
@@ -291,6 +318,27 @@ public Task CreateUserWithEmailAsync_DEPRECATED() {
291
318
} ) . Unwrap ( ) ;
292
319
}
293
320
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
+
294
342
// Update the user's display name with the currently selected display name.
295
343
public Task UpdateUserProfileAsync ( string newDisplayName = null ) {
296
344
if ( auth . CurrentUser == null ) {
@@ -325,6 +373,20 @@ public Task SigninWithEmailAsync_DEPRECATED() {
325
373
}
326
374
}
327
375
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
+
328
390
// This is functionally equivalent to the Signin() function. However, it
329
391
// illustrates the use of Credentials, which can be aquired from many
330
392
// different sources of authentication.
@@ -336,7 +398,24 @@ public Task SigninWithEmailCredentialAsync_DEPRECATED() {
336
398
Firebase . Auth . EmailAuthProvider . GetCredential ( email , password ) ) . ContinueWithOnMainThread (
337
399
HandleSignInWithSignInResult ) ;
338
400
} 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 (
340
419
Firebase . Auth . EmailAuthProvider . GetCredential ( email , password ) ) . ContinueWithOnMainThread (
341
420
HandleSignInWithUser ) ;
342
421
}
@@ -349,6 +428,13 @@ public Task SigninAnonymouslyAsync_DEPRECATED() {
349
428
return auth . SignInAnonymouslyAsync_DEPRECATED ( ) . ContinueWithOnMainThread ( HandleSignInWithUser ) ;
350
429
}
351
430
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
+
352
438
public void AuthenticateToGameCenter ( ) {
353
439
#if ( UNITY_IOS || UNITY_TVOS )
354
440
Social . localUser . Authenticate ( success => {
@@ -385,6 +471,19 @@ void HandleSignInWithUser(Task<Firebase.Auth.FirebaseUser> task) {
385
471
}
386
472
}
387
473
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
+
388
487
// Called when a sign-in with profile data completes.
389
488
void HandleSignInWithSignInResult ( Task < Firebase . Auth . SignInResult > task ) {
390
489
EnableUI ( ) ;
@@ -422,6 +521,24 @@ protected Task LinkWithEmailCredentialAsync_DEPRECATED() {
422
521
}
423
522
}
424
523
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
+
425
542
// Reauthenticate the user with the current email / password.
426
543
protected Task ReauthenticateAsync_DEPRECATED ( ) {
427
544
var user = auth . CurrentUser ;
@@ -451,6 +568,35 @@ protected Task ReauthenticateAsync_DEPRECATED() {
451
568
}
452
569
}
453
570
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
+
454
600
// Reload the currently logged in user.
455
601
public void ReloadUser ( ) {
456
602
if ( auth . CurrentUser == null ) {
@@ -509,6 +655,24 @@ protected Task UnlinkEmailAsync_DEPRECATED() {
509
655
} ) ;
510
656
}
511
657
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
+
512
676
// Sign out the current user.
513
677
protected void SignOut ( ) {
514
678
DebugLog ( "Signing out." ) ;
@@ -671,6 +835,36 @@ protected void VerifyPhoneNumber_DEPRECATED() {
671
835
} ) ;
672
836
}
673
837
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
+
674
868
// Sign in using phone number authentication using code input by the user.
675
869
protected void VerifyReceivedPhoneCode_DEPRECATED ( ) {
676
870
var phoneAuthProvider = Firebase . Auth . PhoneAuthProvider . GetInstance ( auth ) ;
@@ -680,7 +874,20 @@ protected void VerifyReceivedPhoneCode_DEPRECATED() {
680
874
auth . SignInAndRetrieveDataWithCredentialAsync_DEPRECATED ( cred ) . ContinueWithOnMainThread (
681
875
HandleSignInWithSignInResult ) ;
682
876
} 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 ) ;
684
891
}
685
892
}
686
893
@@ -774,22 +981,22 @@ void GUIDisplayControls() {
774
981
GUILayout . Space ( 20 ) ;
775
982
776
983
if ( GUILayout . Button ( "Create User" ) ) {
777
- CreateUserWithEmailAsync_DEPRECATED ( ) ;
984
+ CreateUserWithEmailAsync ( ) ;
778
985
}
779
986
if ( GUILayout . Button ( "Sign In Anonymously" ) ) {
780
- SigninAnonymouslyAsync_DEPRECATED ( ) ;
987
+ SigninAnonymouslyAsync ( ) ;
781
988
}
782
989
if ( GUILayout . Button ( "Sign In With Email" ) ) {
783
- SigninWithEmailAsync_DEPRECATED ( ) ;
990
+ SigninWithEmailAsync ( ) ;
784
991
}
785
992
if ( GUILayout . Button ( "Sign In With Email Credential" ) ) {
786
- SigninWithEmailCredentialAsync_DEPRECATED ( ) ;
993
+ SigninWithEmailCredentialAsync ( ) ;
787
994
}
788
995
if ( GUILayout . Button ( "Link With Email Credential" ) ) {
789
- LinkWithEmailCredentialAsync_DEPRECATED ( ) ;
996
+ LinkWithEmailCredentialAsync ( ) ;
790
997
}
791
998
if ( GUILayout . Button ( "Reauthenticate with Email" ) ) {
792
- ReauthenticateAsync_DEPRECATED ( ) ;
999
+ ReauthenticateAsync ( ) ;
793
1000
}
794
1001
GUIDisplayGameCenterControls ( ) ;
795
1002
if ( GUILayout . Button ( "Reload User" ) ) {
@@ -802,7 +1009,7 @@ void GUIDisplayControls() {
802
1009
GetUserInfo ( ) ;
803
1010
}
804
1011
if ( GUILayout . Button ( "Unlink Email Credential" ) ) {
805
- UnlinkEmailAsync_DEPRECATED ( ) ;
1012
+ UnlinkEmailAsync ( ) ;
806
1013
}
807
1014
if ( GUILayout . Button ( "Sign Out" ) ) {
808
1015
SignOut ( ) ;
@@ -817,10 +1024,10 @@ void GUIDisplayControls() {
817
1024
SendPasswordResetEmail ( ) ;
818
1025
}
819
1026
if ( GUILayout . Button ( "Authenticate Phone Number" ) ) {
820
- VerifyPhoneNumber_DEPRECATED ( ) ;
1027
+ VerifyPhoneNumber ( ) ;
821
1028
}
822
1029
if ( GUILayout . Button ( "Verify Received Phone Code" ) ) {
823
- VerifyReceivedPhoneCode_DEPRECATED ( ) ;
1030
+ VerifyReceivedPhoneCode ( ) ;
824
1031
}
825
1032
if ( GUILayout . Button ( String . Format ( "Fetch Profile on Sign-in {0}" ,
826
1033
signInAndFetchProfile ?
0 commit comments