Skip to content

Commit 54380f6

Browse files
author
Stewart Miles
committed
Integrate Latest @ 162401949
- Add LinkWithCredential to Unity Auth sample. CL: 162401949
1 parent 1227b1b commit 54380f6

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

auth/testapp/Assets/TestApp/UIHandler.cs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ void DisplayUserInfo(Firebase.Auth.IUserInfo userInfo, int indentLevel) {
164164
}
165165
}
166166

167+
// Display a more detailed view of a FirebaseUser.
168+
void DisplayDetailedUserInfo(Firebase.Auth.FirebaseUser user, int indentLevel) {
169+
DisplayUserInfo(user, indentLevel);
170+
DebugLog(" Anonymous: " + user.IsAnonymous);
171+
DebugLog(" Email Verified: " + user.IsEmailVerified);
172+
var providerDataList = new List<Firebase.Auth.IUserInfo>(user.ProviderData);
173+
if (providerDataList.Count > 0) {
174+
DebugLog(" Provider Data:");
175+
foreach (var providerData in user.ProviderData) {
176+
DisplayUserInfo(providerData, indentLevel + 1);
177+
}
178+
}
179+
}
180+
167181
// Track state changes of the auth object.
168182
void AuthStateChanged(object sender, System.EventArgs eventArgs) {
169183
Firebase.Auth.FirebaseAuth senderAuth = sender as Firebase.Auth.FirebaseAuth;
@@ -179,16 +193,7 @@ void AuthStateChanged(object sender, System.EventArgs eventArgs) {
179193
if (signedIn) {
180194
DebugLog("Signed in " + user.UserId);
181195
displayName = user.DisplayName ?? "";
182-
DisplayUserInfo(user, 1);
183-
DebugLog(" Anonymous: " + user.IsAnonymous);
184-
DebugLog(" Email Verified: " + user.IsEmailVerified);
185-
var providerDataList = new List<Firebase.Auth.IUserInfo>(user.ProviderData);
186-
if (providerDataList.Count > 0) {
187-
DebugLog(" Provider Data:");
188-
foreach (var providerData in user.ProviderData) {
189-
DisplayUserInfo(providerData, 2);
190-
}
191-
}
196+
DisplayDetailedUserInfo(user, 1);
192197
}
193198
}
194199
}
@@ -260,7 +265,7 @@ public void UpdateUserProfile(string newDisplayName = null) {
260265
void HandleUpdateUserProfile(Task authTask) {
261266
EnableUI();
262267
if (LogTaskCompletion(authTask, "User profile")) {
263-
DisplayUserInfo(auth.CurrentUser, 1);
268+
DisplayDetailedUserInfo(auth.CurrentUser, 1);
264269
}
265270
}
266271

@@ -293,6 +298,22 @@ void HandleSigninResult(Task<Firebase.Auth.FirebaseUser> authTask) {
293298
LogTaskCompletion(authTask, "Sign-in");
294299
}
295300

301+
void LinkWithCredential() {
302+
if (auth.CurrentUser == null) {
303+
DebugLog("Not signed in, unable to link credential to user.");
304+
return;
305+
}
306+
DebugLog("Attempting to link credential to user...");
307+
Firebase.Auth.Credential cred = Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
308+
auth.CurrentUser.LinkWithCredentialAsync(cred).ContinueWith(HandleLinkCredential);
309+
}
310+
311+
void HandleLinkCredential(Task authTask) {
312+
if (LogTaskCompletion(authTask, "Link Credential")) {
313+
DisplayDetailedUserInfo(auth.CurrentUser, 1);
314+
}
315+
}
316+
296317
public void ReloadUser() {
297318
if (auth.CurrentUser == null) {
298319
DebugLog("Not signed in, unable to reload user.");
@@ -304,7 +325,7 @@ public void ReloadUser() {
304325

305326
void HandleReloadUser(Task authTask) {
306327
if (LogTaskCompletion(authTask, "Reload")) {
307-
DisplayUserInfo(auth.CurrentUser, 1);
328+
DisplayDetailedUserInfo(auth.CurrentUser, 1);
308329
}
309330
}
310331

@@ -325,6 +346,15 @@ void HandleGetUserToken(Task<string> authTask) {
325346
}
326347
}
327348

349+
void GetUserInfo() {
350+
if (auth.CurrentUser == null) {
351+
DebugLog("Not signed in, unable to get info.");
352+
} else {
353+
DebugLog("Current user info:");
354+
DisplayDetailedUserInfo(auth.CurrentUser, 1);
355+
}
356+
}
357+
328358
public void SignOut() {
329359
DebugLog("Signing out.");
330360
auth.SignOut();
@@ -347,7 +377,7 @@ void HandleDeleteResult(Task authTask) {
347377
}
348378

349379
// Show the providers for the current email address.
350-
public void DisplayProviders() {
380+
public void DisplayProvidersForEmail() {
351381
auth.FetchProvidersForEmailAsync(email).ContinueWith((authTask) => {
352382
if (LogTaskCompletion(authTask, "Fetch Providers")) {
353383
DebugLog(String.Format("Email Providers for '{0}':", email));
@@ -469,20 +499,26 @@ void GUIDisplayControls(){
469499
if (GUILayout.Button("Sign In With Credentials")) {
470500
SigninWithCredential();
471501
}
502+
if (GUILayout.Button("Link With Credential")) {
503+
LinkWithCredential();
504+
}
472505
if (GUILayout.Button("Reload User")) {
473506
ReloadUser();
474507
}
475508
if (GUILayout.Button("Get User Token")) {
476509
GetUserToken();
477510
}
511+
if (GUILayout.Button("Get User Info")) {
512+
GetUserInfo();
513+
}
478514
if (GUILayout.Button("Sign Out")) {
479515
SignOut();
480516
}
481517
if (GUILayout.Button("Delete User")) {
482518
DeleteUser();
483519
}
484-
if (GUILayout.Button("Show Providers")) {
485-
DisplayProviders();
520+
if (GUILayout.Button("Show Providers For Email")) {
521+
DisplayProvidersForEmail();
486522
}
487523
if (GUILayout.Button("Password Reset Email")) {
488524
SendPasswordResetEmail();

0 commit comments

Comments
 (0)