Skip to content

Commit

Permalink
send initial data on authentication
Browse files Browse the repository at this point in the history
added check that device id is not already attached to plant upon update
  • Loading branch information
mariaruth1 committed May 29, 2024
1 parent 91c04d0 commit cabeb4e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 75 deletions.
8 changes: 6 additions & 2 deletions api/Core/Services/PlantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ public async Task<Plant> UpdatePlant(UpdatePlantDto updatePlantDto, string reque
if (!String.IsNullOrEmpty(updatePlantDto.DeviceId))
{
var deviceExists = await plantRepository.DoesDeviceIdExist(updatePlantDto.DeviceId);
HandleExistingDeviceId(requesterEmail);
if (deviceExists)
{
updatePlantDto.DeviceId = null;
var plantWithDeviceId = await plantRepository.GetPlantIdByDeviceIdAsync(updatePlantDto.DeviceId);
if (!plantWithDeviceId.ToString().Equals(updatePlantDto.DeviceId))
{
HandleExistingDeviceId(requesterEmail);
updatePlantDto.DeviceId = null;
}
}
}

Expand Down
15 changes: 3 additions & 12 deletions api/Events/Auth/Client/ClientWantsToCheckJwtValidity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using api.Core.Services;
using api.Events.Auth.Server;
using api.Extensions;
using Fleck;
using lib;
using Shared.Models;
Expand All @@ -14,16 +11,10 @@ public class ClientWantsToCheckJwtValidityDto : BaseDtoWithJwt;
/// If the token is not valid, an exception will be thrown, and the GlobalExceptionHandler will catch it, and send a
/// corresponding message to the client.
/// </summary>
public class ClientWantsToCheckJwtValidity(WebSocketConnectionService webSocketConnectionService, JwtService jwtService) : BaseEventHandler<ClientWantsToCheckJwtValidityDto>
public class ClientWantsToCheckJwtValidity(InitialDataHelper initialDataHelper) : BaseEventHandler<ClientWantsToCheckJwtValidityDto>
{
public override Task Handle(ClientWantsToCheckJwtValidityDto dto, IWebSocketConnection socket)
public override async Task Handle(ClientWantsToCheckJwtValidityDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt);
webSocketConnectionService.UpdateConnectionEmail(socket, email);
socket.SendDto(new ServerAuthenticatesUser
{
Jwt = dto.Jwt,
});
return Task.CompletedTask;
await initialDataHelper.SendInitialData(socket, dto.Jwt!);
}
}
63 changes: 2 additions & 61 deletions api/Events/Auth/Client/ClientWantsToLogIn.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
using api.Core.Services;
using api.Core.Services.External.BlobStorage;
using api.Events.Auth.Server;
using api.Events.Collections.Server;
using api.Events.Global;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
using Shared.Dtos;
using Shared.Dtos.FromClient.Identity;
using Shared.Exceptions;

Expand All @@ -19,66 +11,15 @@ public class ClientWantsToLogInDto : BaseDto
public LoginDto LoginDto { get; set; } = null!;
}

public class ClientWantsToLogIn(WebSocketConnectionService webSocketConnectionService, UserService userService, IBlobStorageService blobStorageService, PlantService plantService, CollectionsService collectionsService, StatsService statsService)
public class ClientWantsToLogIn(UserService userService, InitialDataHelper initialDataHelper)
: BaseEventHandler<ClientWantsToLogInDto>
{
public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnection socket)
{
var jwt = await userService.Login(dto.LoginDto);
if (jwt == null) throw new InvalidCredentialsException();

var user = await userService.GetUserByEmail(dto.LoginDto.Email);

var criticalPlants = plantService.GetCriticalPlants(user.UserEmail);
var allCollections = collectionsService.GetCollectionsForUser(user.UserEmail);
var allPlants = plantService.GetPlantsForUser(user.UserEmail, 1, 100);
var stats = statsService.GetStats(user.UserEmail);

await Task.WhenAll(criticalPlants, allCollections, allPlants, stats);

webSocketConnectionService.UpdateConnectionEmail(socket, dto.LoginDto.Email);

var getUserDto = new GetUserDto
{
UserEmail = user.UserEmail,
Username = user.UserName,
};

if (!string.IsNullOrEmpty(user.BlobUrl))
{
getUserDto.BlobUrl = blobStorageService.GenerateSasUri(user.BlobUrl, false);
}

socket.SendDto(new ServerAuthenticatesUser
{
Jwt = jwt,

});

socket.SendDto(new ServerSendsUserInfo
{
GetUserDto = getUserDto
});

socket.SendDto(new ServerSendsCriticalPlants
{
Plants = criticalPlants.Result
});

socket.SendDto(new ServerSendsAllCollections
{
Collections = allCollections.Result.ToList()
});

socket.SendDto(new ServerSendsPlants
{
Plants = allPlants.Result
});

socket.SendDto(new ServerSendsStats
{
Stats = stats.Result
});
await initialDataHelper.SendInitialData(socket, jwt);
}
}

63 changes: 63 additions & 0 deletions api/Events/Auth/Client/InitialDataHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using api.Core.Services;
using api.Core.Services.External.BlobStorage;
using api.Events.Auth.Server;
using api.Events.Collections.Server;
using api.Events.Global;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using Shared.Dtos;

namespace api.Events.Auth.Client;

public class InitialDataHelper(PlantService plantService, IBlobStorageService blobStorageService, CollectionsService collectionsService, StatsService statsService, UserService userService, WebSocketConnectionService webSocketConnectionService, JwtService jwtService)
{

public async Task SendInitialData(IWebSocketConnection socket, string jwt)
{
var email = jwtService.GetEmailFromJwt(jwt);
webSocketConnectionService.UpdateConnectionEmail(socket, email);

var user = await userService.GetUserByEmail(email);

socket.SendDto(new ServerAuthenticatesUser
{
Jwt = jwt,
});

var criticalPlants = await plantService.GetCriticalPlants(user.UserEmail);
socket.SendDto(new ServerSendsCriticalPlants
{
Plants = criticalPlants
});

var getUserDto = new GetUserDto
{
UserEmail = user.UserEmail,
Username = user.UserName,
};

if (!string.IsNullOrEmpty(user.BlobUrl))
{
getUserDto.BlobUrl = blobStorageService.GenerateSasUri(user.BlobUrl, false);
}

socket.SendDto(new ServerSendsUserInfo
{
GetUserDto = getUserDto
});

var allCollections = await collectionsService.GetCollectionsForUser(user.UserEmail);
socket.SendDto(new ServerSendsAllCollections
{
Collections = allCollections.ToList()
});

var stats = await statsService.GetStats(user.UserEmail);
socket.SendDto(new ServerSendsStats
{
Stats = stats
});
}
}
4 changes: 4 additions & 0 deletions api/Extensions/AddServicesAndRepositoriesExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using api.Core.Services;
using api.Core.Services.External.BackgroundRemoval;
using api.Core.Services.External.BlobStorage;
using api.Events.Auth.Client;
using Infrastructure.Repositories;

namespace api.Extensions;
Expand Down Expand Up @@ -28,6 +29,9 @@ public static void AddServicesAndRepositories(this IServiceCollection services)
services.AddSingleton<MqttPublisherService>();
services.AddSingleton<StatsService>();

// Helpers
services.AddSingleton<InitialDataHelper>();

// External services
if (EnvironmentHelper.IsTesting())
{
Expand Down

0 comments on commit cabeb4e

Please sign in to comment.