File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,18 @@ public async Task<FirebaseAuthLink> ChangeUserEmail(string idToken, string newEm
211211

212212
return await this.ExecuteWithPostContentAsync(GoogleUpdateUserPassword, content).ConfigureAwait(false);
213213
}
214-
214+
215215
/// <summary>
216216
/// Creates new user with given credentials.
217217
/// </summary>
218218
/// <param name="email"> The email. </param>
219219
/// <param name="password"> The password. </param>
220220
/// <param name="displayName"> Optional display name. </param>
221221
/// <param name="sendVerificationEmail"> Optional. Whether to send user a link to verfiy his email address. </param>
222+
/// <param name="locale"> The language code of language that the email will be in. </param>
222223
/// <returns> The <see cref="FirebaseAuth"/>. </returns>
223-
public async Task<FirebaseAuthLink> CreateUserWithEmailAndPasswordAsync(string email, string password, string displayName = "", bool sendVerificationEmail = false)
224+
public async Task<FirebaseAuthLink> CreateUserWithEmailAndPasswordAsync(string email, string password, string displayName = "",
225+
bool sendVerificationEmail = false, string locale = null)
224226
{
225227
var content = $"{{\"email\":\"{email}\",\"password\":\"{password}\",\"returnSecureToken\":true}}";
226228

@@ -239,7 +241,7 @@ public async Task<FirebaseAuthLink> CreateUserWithEmailAndPasswordAsync(string e
239241
if (sendVerificationEmail)
240242
{
241243
//send verification email
242-
await this.SendEmailVerificationAsync(signup).ConfigureAwait(false);
244+
await this.SendEmailVerificationAsync(signup, locale).ConfigureAwait(false);
243245
}
244246

245247
return signup;
@@ -315,14 +317,21 @@ public async Task DeleteUserAsync(string firebaseToken)
315317
/// Sends user an email with a link to reset his password.
316318
/// </summary>
317319
/// <param name="email"> The email. </param>
318-
public async Task SendPasswordResetEmailAsync(string email)
320+
/// <param name="locale"> The language code of language that the email will be in. </param>
321+
public async Task SendPasswordResetEmailAsync(string email, string locale = null)
319322
{
320323
var content = $"{{\"requestType\":\"PASSWORD_RESET\",\"email\":\"{email}\"}}";
321324
var responseData = "N/A";
322325

323326
try
324327
{
325-
var response = await this.client.PostAsync(new Uri(string.Format(GoogleGetConfirmationCodeUrl, this.authConfig.ApiKey)), new StringContent(content, Encoding.UTF8, "application/json")).ConfigureAwait(false);
328+
var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
329+
if (locale != null)
330+
{
331+
httpContent = AddFirebaseLocaleHeader(httpContent, locale);
332+
}
333+
334+
var response = await this.client.PostAsync(new Uri(string.Format(GoogleGetConfirmationCodeUrl, this.authConfig.ApiKey)), httpContent).ConfigureAwait(false);
326335
responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
327336

328337
response.EnsureSuccessStatusCode();
@@ -338,11 +347,18 @@ public async Task SendPasswordResetEmailAsync(string email)
338347
/// Sends user an email with a link to verify his email address.
339348
/// </summary>
340349
/// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param>
341-
public async Task SendEmailVerificationAsync(string firebaseToken)
350+
/// <param name="locale"> The language code of language that the email will be in. </param>
351+
public async Task SendEmailVerificationAsync(string firebaseToken, string locale = null)
342352
{
343353
var content = $"{{\"requestType\":\"VERIFY_EMAIL\",\"idToken\":\"{firebaseToken}\"}}";
344354

345-
var response = await this.client.PostAsync(new Uri(string.Format(GoogleGetConfirmationCodeUrl, this.authConfig.ApiKey)), new StringContent(content, Encoding.UTF8, "application/json")).ConfigureAwait(false);
355+
var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
356+
if (locale != null)
357+
{
358+
httpContent = AddFirebaseLocaleHeader(httpContent, locale);
359+
}
360+
361+
var response = await this.client.PostAsync(new Uri(string.Format(GoogleGetConfirmationCodeUrl, this.authConfig.ApiKey)), httpContent).ConfigureAwait(false);
346362

347363
response.EnsureSuccessStatusCode();
348364
}
@@ -351,9 +367,10 @@ public async Task SendEmailVerificationAsync(string firebaseToken)
351367
/// Sends user an email with a link to verify his email address.
352368
/// </summary>
353369
/// <param name="auth"> The authenticated user to verify email address. </param>
354-
public async Task SendEmailVerificationAsync(FirebaseAuth auth)
370+
/// <param name="locale"> The language code of language that the email will be in. </param>
371+
public async Task SendEmailVerificationAsync(FirebaseAuth auth, string locale = null)
355372
{
356-
await this.SendEmailVerificationAsync(auth.FirebaseToken).ConfigureAwait(false);
373+
await this.SendEmailVerificationAsync(auth.FirebaseToken, locale).ConfigureAwait(false);
357374
}
358375

359376
/// <summary>
@@ -506,6 +523,21 @@ public void Dispose()
506523
this.client.Dispose();
507524
}
508525

526+
/// <summary>
527+
/// Adds firebase locale header used to send email in chosen language.
528+
/// If locale is incorrect default language will be chosen.
529+
/// </summary>
530+
/// <typeparam name="T"> Class deriving from HttpContent </typeparam>
531+
/// <param name="content"> Http content to add locale header to. </param>
532+
/// <param name="locale"> Locale string. </param>
533+
/// <returns> Passed http content with added header. </returns>
534+
private static T AddFirebaseLocaleHeader<T>(T content, string locale) where T : HttpContent
535+
{
536+
content.Headers.Add("X-Firebase-Locale", locale);
537+
538+
return content;
539+
}
540+
509541
private async Task<FirebaseAuthLink> ExecuteWithPostContentAsync(string googleUrl, string postContent)
510542
{
511543
string responseData = "N/A";
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ public interface IFirebaseAuthProvider
1414
/// <param name="password"> The password. </param>
1515
/// <param name="displayName"> Optional display name. </param>
1616
/// <param name="sendVerificationEmail"> Optional. Whether to send user a link to verfiy his email address. </param>
17+
/// <param name="locale"> The language code of language that the email will be in. </param>
1718
/// <returns> The <see cref="FirebaseAuthLink"/>. </returns>
18-
Task<FirebaseAuthLink> CreateUserWithEmailAndPasswordAsync(string email, string password, string displayName = "", bool sendVerificationEmail = false);
19-
19+
Task<FirebaseAuthLink> CreateUserWithEmailAndPasswordAsync(string email, string password, string displayName = "",
20+
bool sendVerificationEmail = false, string locale = null);
21+
2022
/// <summary>
2123
/// Sends user an email with a link to reset his password.
2224
/// </summary>
2325
/// <param name="email"> The email. </param>
26+
/// <param name="locale"> The language code of language that the email will be in. </param>
2427
/// <returns> The <see cref="Task"/>. </returns>
25-
Task SendPasswordResetEmailAsync(string email);
28+
Task SendPasswordResetEmailAsync(string email, string locale = null);
2629

2730
/// <summary>
2831
/// Sign in user anonymously. He would still have a user id and access token generated, but name and other personal user properties will be null.
@@ -147,13 +150,15 @@ public interface IFirebaseAuthProvider
147150
/// Sends user an email with a link to verify his email address.
148151
/// </summary>
149152
/// <param name="firebaseToken"> The FirebaseToken (idToken) of an authenticated user. </param>
150-
Task SendEmailVerificationAsync(string firebaseToken);
153+
/// <param name="locale"> The language code of language that the email will be in. </param>
154+
Task SendEmailVerificationAsync(string firebaseToken, string locale = null);
151155

152156
/// <summary>
153157
/// Sends user an email with a link to verify his email address.
154158
/// </summary>
155159
/// <param name="auth"> The authenticated user to verify email address. </param>
156-
Task SendEmailVerificationAsync(FirebaseAuth auth);
160+
/// <param name="locale"> The language code of language that the email will be in. </param>
161+
Task SendEmailVerificationAsync(FirebaseAuth auth, string locale = null);
157162

158163
/// <summary>
159164
/// Refreshes given auth using its refresh token.

0 commit comments

Comments
 (0)