Skip to content

Commit f45f530

Browse files
committed
Add method to update displayName and photoUrl
1 parent 4885072 commit f45f530

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/Firebase.Auth/FirebaseAuthLink.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ public async Task<FirebaseAuthLink> GetFreshAuthAsync()
8989
return this;
9090
}
9191

92+
public async Task<FirebaseAuthLink> UpdateProfileAsync(string displayName, string photoUrl)
93+
{
94+
var auth = await this.AuthProvider.UpdateProfileAsync(this.FirebaseToken, displayName, photoUrl).ConfigureAwait(false);
95+
96+
this.CopyPropertiesLocally(auth.AuthProvider, auth);
97+
98+
return this;
99+
}
100+
92101
protected void OnFirebaseAuthRefreshed(FirebaseAuth auth)
93102
{
94103
this.FirebaseAuthRefreshed?.Invoke(this, new FirebaseAuthEventArgs(auth));

src/Firebase.Auth/FirebaseAuthProvider.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class FirebaseAuthProvider : IDisposable, IFirebaseAuthProvider
2525
private const string GoogleSetAccountUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key={0}";
2626
private const string GoogleCreateAuthUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/createAuthUri?key={0}";
2727

28+
private const string ProfileDeleteDisplayName = "DISPLAY_NAME";
29+
private const string ProfileDeletePhotoUrl = "PHOTO_URL";
30+
2831
private readonly FirebaseConfig authConfig;
2932
private readonly HttpClient client;
3033

@@ -160,6 +163,39 @@ public async Task<FirebaseAuthLink> CreateUserWithEmailAndPasswordAsync(string e
160163
return signup;
161164
}
162165

166+
/// <summary>
167+
/// Updates profile (displayName and photoUrl) of user tied to given user token.
168+
/// </summary>
169+
/// <param name="displayName"> The new display name. </param>
170+
/// <param name="photoUrl"> The new photo URL. </param>
171+
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
172+
public async Task<FirebaseAuthLink> UpdateProfileAsync(string firebaseToken, string displayName, string photoUrl)
173+
{
174+
StringBuilder sb = new StringBuilder($"{{\"idToken\":\"{firebaseToken}\"");
175+
if (!string.IsNullOrWhiteSpace(displayName) && !string.IsNullOrWhiteSpace(photoUrl))
176+
{
177+
sb.Append($",\"displayName\":\"{displayName}\",\"photoUrl\":\"{photoUrl}\"");
178+
}
179+
else if (!string.IsNullOrWhiteSpace(displayName))
180+
{
181+
sb.Append($",\"displayName\":\"{displayName}\"");
182+
sb.Append($",\"deleteAttribute\":[\"{ProfileDeletePhotoUrl}\"]");
183+
}
184+
else if (!string.IsNullOrWhiteSpace(photoUrl))
185+
{
186+
sb.Append($",\"photoUrl\":\"{photoUrl}\"");
187+
sb.Append($",\"deleteAttribute\":[\"{ProfileDeleteDisplayName}\"]");
188+
}
189+
else
190+
{
191+
sb.Append($",\"deleteAttribute\":[\"{ProfileDeleteDisplayName}\",\"{ProfileDeletePhotoUrl}\"]");
192+
}
193+
194+
sb.Append($",\"returnSecureToken\":true}}");
195+
196+
return await this.ExecuteWithPostContentAsync(GoogleSetAccountUrl, sb.ToString()).ConfigureAwait(false);
197+
}
198+
163199
/// <summary>
164200
/// Deletes the user with a recent Firebase Token.
165201
/// </summary>

src/Firebase.Auth/IFirebaseAuthProvider.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public interface IFirebaseAuthProvider
5353
/// <returns> The <see cref="FirebaseAuth"/>. </returns>
5454
Task<FirebaseAuthLink> SignInWithCustomTokenAsync(string customToken);
5555

56+
/// <summary>
57+
/// Updates profile (displayName and photoUrl) of user tied to given user token.
58+
/// </summary>
59+
/// <param name="displayName"> The new display name. </param>
60+
/// <param name="photoUrl"> The new photo URL. </param>
61+
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
62+
Task<FirebaseAuthLink> UpdateProfileAsync(string firebaseToken, string displayName, string photoUrl);
63+
5664
/// <summary>
5765
/// Links the authenticated user represented by <see cref="auth"/> with an email and password.
5866
/// </summary>

0 commit comments

Comments
 (0)