Skip to content

Commit 777f618

Browse files
committed
Add public API to unlink providers
1 parent 90becb8 commit 777f618

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/Firebase.Auth/FirebaseAuthLink.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task<FirebaseAuthLink> LinkToAsync(string email, string password)
4141
}
4242

4343
/// <summary>
44-
/// Links the this user with and account from a third party provider.
44+
/// Links the user with an account from a third party provider.
4545
/// </summary>
4646
/// <param name="authType"> The auth type. </param>
4747
/// <param name="oauthAccessToken"> The access token retrieved from login provider of your choice. </param>
@@ -55,6 +55,20 @@ public async Task<FirebaseAuthLink> LinkToAsync(FirebaseAuthType authType, strin
5555
return this;
5656
}
5757

58+
/// <summary>
59+
/// Unlinks the user from the given <see cref="authType"/> (provider).
60+
/// </summary>
61+
/// <param name="authType"> The auth type. </param>
62+
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
63+
public async Task<FirebaseAuthLink> UnlinkFromAsync(FirebaseAuthType authType)
64+
{
65+
var auth = await this.AuthProvider.UnlinkAccountsAsync(this, authType).ConfigureAwait(false);
66+
67+
this.CopyPropertiesLocally(auth.AuthProvider, auth);
68+
69+
return this;
70+
}
71+
5872
public async Task RefreshUserDetails()
5973
{
6074
if (this.AuthProvider != null && !string.IsNullOrEmpty(this.FirebaseToken))

src/Firebase.Auth/FirebaseAuthProvider.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public async Task SendEmailVerificationAsync(FirebaseAuth auth)
215215
}
216216

217217
/// <summary>
218-
/// Links the authenticated user represented by <see cref="auth"/> with an email and password.
218+
/// Links the given <see cref="firebaseToken"/> with an email and password.
219219
/// </summary>
220220
/// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param>
221221
/// <param name="email"> The email. </param>
@@ -241,7 +241,7 @@ public async Task<FirebaseAuthLink> LinkAccountsAsync(FirebaseAuth auth, string
241241
}
242242

243243
/// <summary>
244-
/// Links the authenticated user represented by <see cref="auth"/> with and account from a third party provider.
244+
/// Links the given <see cref="firebaseToken"/> with an account from a third party provider.
245245
/// </summary>
246246
/// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param>
247247
/// <param name="authType"> The auth type. </param>
@@ -256,7 +256,7 @@ public async Task<FirebaseAuthLink> LinkAccountsAsync(string firebaseToken, Fire
256256
}
257257

258258
/// <summary>
259-
/// Links the authenticated user represented by <see cref="auth"/> with and account from a third party provider.
259+
/// Links the authenticated user represented by <see cref="auth"/> with an account from a third party provider.
260260
/// </summary>
261261
/// <param name="auth"> The auth. </param>
262262
/// <param name="authType"> The auth type. </param>
@@ -267,6 +267,40 @@ public async Task<FirebaseAuthLink> LinkAccountsAsync(FirebaseAuth auth, Firebas
267267
return await this.LinkAccountsAsync(auth.FirebaseToken, authType, oauthAccessToken).ConfigureAwait(false);
268268
}
269269

270+
/// <summary>
271+
/// Unlinks the given <see cref="authType"/> from the account associated with <see cref="firebaseToken"/>.
272+
/// </summary>
273+
/// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param>
274+
/// <param name="authType"> The auth type. </param>
275+
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
276+
public async Task<FirebaseAuthLink> UnlinkAccountsAsync(string firebaseToken, FirebaseAuthType authType)
277+
{
278+
string providerId = null;
279+
if (authType == FirebaseAuthType.EmailAndPassword)
280+
{
281+
providerId = authType.ToEnumString();
282+
}
283+
else
284+
{
285+
providerId = this.GetProviderId(authType);
286+
}
287+
288+
var content = $"{{\"idToken\":\"{firebaseToken}\",\"deleteProvider\":[\"{providerId}\"]}}";
289+
290+
return await this.ExecuteWithPostContentAsync(GoogleSetAccountUrl, content).ConfigureAwait(false);
291+
}
292+
293+
/// <summary>
294+
/// Unlinks the given <see cref="authType"/> from the authenticated user represented by <see cref="auth"/>.
295+
/// </summary>
296+
/// <param name="auth"> The auth. </param>
297+
/// <param name="authType"> The auth type. </param>
298+
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
299+
public async Task<FirebaseAuthLink> UnlinkAccountsAsync(FirebaseAuth auth, FirebaseAuthType authType)
300+
{
301+
return await this.UnlinkAccountsAsync(auth.FirebaseToken, authType).ConfigureAwait(false);
302+
}
303+
270304
/// <summary>
271305
/// Gets a list of accounts linked to given email.
272306
/// </summary>

src/Firebase.Auth/IFirebaseAuthProvider.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ public interface IFirebaseAuthProvider
8989
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
9090
Task<FirebaseAuthLink> LinkAccountsAsync(string firebaseToken, FirebaseAuthType authType, string oauthAccessToken);
9191

92+
/// <summary>
93+
/// Unlinks the given <see cref="authType"/> from the account associated with <see cref="firebaseToken"/>.
94+
/// </summary>
95+
/// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param>
96+
/// <param name="authType"> The auth type. </param>
97+
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
98+
Task<FirebaseAuthLink> UnlinkAccountsAsync(string firebaseToken, FirebaseAuthType authType);
99+
100+
/// <summary>
101+
/// Unlinks the given <see cref="authType"/> from the authenticated user represented by <see cref="auth"/>.
102+
/// </summary>
103+
/// <param name="auth"> The auth. </param>
104+
/// <param name="authType"> The auth type. </param>
105+
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
106+
Task<FirebaseAuthLink> UnlinkAccountsAsync(FirebaseAuth auth, FirebaseAuthType authType);
107+
92108
/// <summary>
93109
/// Gets a list of accounts linked to given email.
94110
/// </summary>

0 commit comments

Comments
 (0)