Skip to content

Commit

Permalink
Merge pull request #149 from PinguApps/146-create-email-password-session
Browse files Browse the repository at this point in the history
Added create email password session server implementation
  • Loading branch information
pingu2k4 authored Sep 3, 2024
2 parents 36c62eb + 531d86b commit 1cbcc67
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 9 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
<!-- ![36 / 293](https://progress-bar.dev/36 /?scale=293&suffix=%20/%20288&width=500) -->
![Server & Client - 36 / 293](https://img.shields.io/badge/Server_&_Client-36%20%2F%20293-red?style=for-the-badge)
<!-- ![37 / 293](https://progress-bar.dev/37/?scale=293&suffix=%20/%20288&width=500) -->
![Server & Client - 37 / 293](https://img.shields.io/badge/Server_&_Client-37%20%2F%20293-red?style=for-the-badge)

<!-- ![3 / 200](https://progress-bar.dev/3/?scale=195&suffix=%20/%20200&width=300) -->
![Server - 3 / 200](https://img.shields.io/badge/Server-3%20%2F%20200-red?style=for-the-badge)
<!-- ![4 / 200](https://progress-bar.dev/4/?scale=195&suffix=%20/%20200&width=300) -->
![Server - 4 / 200](https://img.shields.io/badge/Server-4%20%2F%20200-red?style=for-the-badge)

<!-- ![33 / 93](https://progress-bar.dev/33/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 33 / 93](https://img.shields.io/badge/Client-33%20%2F%2093-red?style=for-the-badge)
Expand All @@ -155,8 +155,8 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
|| There is currently no intention to implement the endpoint for the given SDK type (client or server) |

### Account
<!-- ![36 / 57](https://progress-bar.dev/36/?scale=57&suffix=%20/%2052&width=120) -->
![Account - 36 / 57](https://img.shields.io/badge/Account-36%20%2F%2057-gold?style=for-the-badge)
<!-- ![37 / 57](https://progress-bar.dev/37/?scale=57&suffix=%20/%2052&width=120) -->
![Account - 37 / 57](https://img.shields.io/badge/Account-37%20%2F%2057-gold?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -187,7 +187,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [List Sessions](https://appwrite.io/docs/references/1.6.x/client-rest/account#listSessions) |||
| [Delete Sessions](https://appwrite.io/docs/references/1.6.x/client-rest/account#deleteSessions) |||
| [Create Anonymous Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#createAnonymousSession) |||
| [Create Email Password Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#createEmailPasswordSession) || |
| [Create Email Password Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#createEmailPasswordSession) || |
| [Update Magic URL Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#updateMagicURLSession) |||
| [Create OAuth2 Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#createOAuth2Session) |||
| [Update Phone Session](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneSession) |||
Expand Down
12 changes: 10 additions & 2 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using PinguApps.Appwrite.Client;
using PinguApps.Appwrite.Server.Servers;
using PinguApps.Appwrite.Shared.Requests;

namespace PinguApps.Appwrite.Playground;
internal class App
Expand All @@ -20,7 +21,14 @@ public async Task Run(string[] args)
{
//_client.SetSession(_session);

var response = await _server.Account.CreateAnonymousSession();
var request = new CreateEmailPasswordSessionRequest()
{
Email = "[email protected]",
Password = "REDACTED"
};


var response = await _server.Account.CreateEmailPasswordSession(request);

Console.WriteLine(response.Result.Match(
account => account.ToString(),
Expand All @@ -29,7 +37,7 @@ public async Task Run(string[] args)

await Task.Delay(5000);

var response2 = await _server.Account.CreateAnonymousSession();
var response2 = await _server.Account.CreateEmailPasswordSession(request);

Console.WriteLine(response2.Result.Match(
account => account.ToString(),
Expand Down
3 changes: 3 additions & 0 deletions src/PinguApps.Appwrite.Server/Internals/IAccountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ internal interface IAccountApi : IBaseApi

[Post("/account/sessions/anonymous")]
Task<IApiResponse<Session>> CreateAnonymousSession();

[Post("/account/sessions/email")]
Task<IApiResponse<Session>> CreateEmailPasswordSession(CreateEmailPasswordSessionRequest request);
}
17 changes: 17 additions & 0 deletions src/PinguApps.Appwrite.Server/Servers/AccountServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,21 @@ public async Task<AppwriteResult<Session>> CreateAnonymousSession()
return e.GetExceptionResponse<Session>();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<Session>> CreateEmailPasswordSession(CreateEmailPasswordSessionRequest request)
{
try
{
request.Validate(true);

var result = await _accountApi.CreateEmailPasswordSession(request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<Session>();
}
}
}
9 changes: 9 additions & 0 deletions src/PinguApps.Appwrite.Server/Servers/IAccountServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ public interface IAccountServer
/// </summary>
/// <returns>The Session</returns>
Task<AppwriteResult<Session>> CreateAnonymousSession();

/// <summary>
/// <para>Allow the user to login into their account by providing a valid email and password combination. This route will create a new session for the user.</para>
/// <para>A user is limited to 10 active sessions at a time by default. <see href="https://appwrite.io/docs/authentication-security#limits">Learn more about session limits</see>.</para>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/account#createEmailPasswordSession">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The Session</returns>
Task<AppwriteResult<Session>> CreateEmailPasswordSession(CreateEmailPasswordSessionRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Servers.Account;
public partial class AccountServerTests
{
[Fact]
public async Task CreateEmailPasswordSession_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateEmailPasswordSessionRequest()
{
Email = "[email protected]",
Password = "Password"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/email")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.SessionResponse);

// Act
var result = await _appwriteServer.Account.CreateEmailPasswordSession(request);

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task CreateEmailPasswordSession_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new CreateEmailPasswordSessionRequest()
{
Email = "[email protected]",
Password = "Password"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/email")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteServer.Account.CreateEmailPasswordSession(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task CreateEmailPasswordSession_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new CreateEmailPasswordSessionRequest()
{
Email = "[email protected]",
Password = "Password"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/email")
.ExpectedHeaders()
.WithJsonContent(request)
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteServer.Account.CreateEmailPasswordSession(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}

0 comments on commit 1cbcc67

Please sign in to comment.