Skip to content

Commit 515d88d

Browse files
committed
Added email authentication methods
1 parent 05dd0ad commit 515d88d

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/Firebase.Auth.Tests/IntegrationTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class IntegrationTests
1616
private const string GoogleAccessToken = "<GOOGLE USER ACCESS TOKEN>";
1717
private const string GoogleTestUserFirstName = "Mark";
1818

19+
private const string FirebaseEmail = "<TEST USER EMAIL>";
20+
private const string FirebasePassword = "<TEST USER PASSWORD>";
21+
1922
[TestMethod]
2023
public void FacebookTest()
2124
{
@@ -37,5 +40,28 @@ public void GoogleTest()
3740
auth.User.FirstName.ShouldBeEquivalentTo(GoogleTestUserFirstName);
3841
auth.FirebaseToken.Should().NotBeNullOrWhiteSpace();
3942
}
43+
44+
[TestMethod]
45+
public void EmailTest()
46+
{
47+
var authProvider = new FirebaseAuthProvider(new FirebaseConfig(ApiKey));
48+
49+
var auth = authProvider.SignInWithEmailAndPassword(FirebaseEmail, FirebasePassword).Result;
50+
51+
auth.User.Email.ShouldBeEquivalentTo(FirebaseEmail);
52+
auth.FirebaseToken.Should().NotBeNullOrWhiteSpace();
53+
}
54+
55+
[TestMethod]
56+
public void CreateUserTest()
57+
{
58+
var authProvider = new FirebaseAuthProvider(new FirebaseConfig(ApiKey));
59+
var email = $"abcd{new Random().Next()}@test.com";
60+
61+
var auth = authProvider.CreateUserWithEmailAndPassword(email, FirebasePassword).Result;
62+
63+
auth.User.Email.ShouldBeEquivalentTo(email);
64+
auth.FirebaseToken.Should().NotBeNullOrWhiteSpace();
65+
}
4066
}
4167
}

src/Firebase.Auth/FirebaseAuth.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public string FirebaseToken
1717
set;
1818
}
1919

20+
/// <summary>
21+
/// Gets or sets the refresh token of the underlying service which can be used to get a new access token.
22+
/// </summary>
23+
[JsonProperty("refreshToken")]
24+
public string RefreshToken
25+
{
26+
get;
27+
set;
28+
}
29+
2030
/// <summary>
2131
/// Gets or sets the numbers of seconds until the token expires.
2232
/// </summary>

src/Firebase.Auth/FirebaseAuthProvider.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
public class FirebaseAuthProvider : IDisposable
1414
{
1515
private const string GoogleIdentityUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key={0}";
16-
private const string GoogleAnonymousUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key={0}";
16+
private const string GoogleSignUpUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key={0}";
17+
private const string GooglePasswordUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key={0}";
18+
private const string GooglePasswordResetUrl = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key={0}";
1719

1820
private readonly FirebaseConfig authConfig;
1921
private readonly HttpClient client;
@@ -50,7 +52,42 @@ public async Task<FirebaseAuth> SignInAnonymously()
5052
{
5153
var content = $"{{\"returnSecureToken\":true}}";
5254

53-
return await this.SignInWithPostContent(GoogleAnonymousUrl, content);
55+
return await this.SignInWithPostContent(GoogleSignUpUrl, content);
56+
}
57+
58+
/// <summary>
59+
/// Using the provided email and passowrd, get the firebase auth with token and basic user credentials.
60+
/// </summary>
61+
/// <param name="email"> The email. </param>
62+
/// <param name="password"> The password. </param>
63+
/// <returns> The <see cref="FirebaseAuth"/>. </returns>
64+
public async Task<FirebaseAuth> SignInWithEmailAndPassword(string email, string password)
65+
{
66+
var content = $"{{\"email\":\"{email}\",\"password\":\"{password}\",\"returnSecureToken\":true}}";
67+
68+
return await this.SignInWithPostContent(GooglePasswordUrl, content);
69+
}
70+
71+
/// <summary>
72+
/// Creates new user with given credentials.
73+
/// </summary>
74+
/// <param name="email"> The email. </param>
75+
/// <param name="password"> The password. </param>
76+
/// <returns> The <see cref="FirebaseAuth"/>. </returns>
77+
public async Task<FirebaseAuth> CreateUserWithEmailAndPassword(string email, string password)
78+
{
79+
var content = $"{{\"email\":\"{email}\",\"password\":\"{password}\",\"returnSecureToken\":true}}";
80+
81+
return await this.SignInWithPostContent(GoogleSignUpUrl, content);
82+
}
83+
84+
public async Task SendPasswordResetEmail(string email)
85+
{
86+
var content = $"{{\"requestType\":\"PASSWORD_RESET\",\"email\":\"{email}\"}}";
87+
88+
var response = await this.client.PostAsync(new Uri(string.Format(GooglePasswordResetUrl, this.authConfig.ApiKey)), new StringContent(content, Encoding.UTF8, "application/json"));
89+
90+
response.EnsureSuccessStatusCode();
5491
}
5592

5693
/// <summary>

0 commit comments

Comments
 (0)