diff --git a/.gitignore b/.gitignore
index b62bf52..7cf901f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
node_modules/
-result/
-lcov.info
\ No newline at end of file
+result
+lcov.info
+.env
diff --git a/src/api/middlewares/authMiddleware.js b/src/api/middlewares/authMiddleware.js
index d67c567..b91449f 100644
--- a/src/api/middlewares/authMiddleware.js
+++ b/src/api/middlewares/authMiddleware.js
@@ -36,11 +36,13 @@ export async function useAuthentication(req, res, next) {
req.user = await getUserById(auth.sub);
logAuth('User data:', req.user);
+ req.device = req.user.devices.find(device => device.id === auth.deviceId);
+
next();
}
export async function requireAuth(req, res, next) {
- if (!req.auth) {
+ if (!req.auth || !req.user || !req.device) {
logAuth('Unauthorized request to', req.path);
res.status(401).send(
new SafeNSoundError({
diff --git a/src/api/routes/alarmRoutes.js b/src/api/routes/alarmRoutes.js
index 939ca97..23b79c1 100644
--- a/src/api/routes/alarmRoutes.js
+++ b/src/api/routes/alarmRoutes.js
@@ -46,14 +46,11 @@ export const alarmListRoute = {
description: 'Get a list of all alarms for monitored users',
async method(req, res) {
console.log(req.user.monitoredUsers);
- const alarms = [];
+ const alarms = {};
for (const userId of req.user.monitoredUsers) {
const user = await getUserById(userId);
if (user.alarm) {
- alarms.push({
- user: userId,
- alarm: user.alarm
- });
+ alarms[userId] = user.alarm;
}
}
res.send(alarms);
diff --git a/testFrontend/SafeNSound.FakeUser/Program.cs b/testFrontend/SafeNSound.FakeUser/Program.cs
new file mode 100644
index 0000000..3751555
--- /dev/null
+++ b/testFrontend/SafeNSound.FakeUser/Program.cs
@@ -0,0 +1,2 @@
+// See https://aka.ms/new-console-template for more information
+Console.WriteLine("Hello, World!");
diff --git a/testFrontend/SafeNSound.FakeUser/SafeNSound.FakeUser.csproj b/testFrontend/SafeNSound.FakeUser/SafeNSound.FakeUser.csproj
new file mode 100644
index 0000000..fd4bd08
--- /dev/null
+++ b/testFrontend/SafeNSound.FakeUser/SafeNSound.FakeUser.csproj
@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net9.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+</Project>
diff --git a/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor b/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor
index 71d18f2..59b8e4c 100644
--- a/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor
+++ b/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor
@@ -1,7 +1,11 @@
@page "/Alarm"
<h1>Alarm</h1>
-<LinkButton OnClick="@RaiseAlarm">Raise alarm</LinkButton>
+
+<LinkButton OnClick="@RaiseAlarm">Raise</LinkButton>
+<LinkButton OnClick="@GetAlarm">Get</LinkButton>
+<LinkButton OnClick="@ClearAlarm">Delete</LinkButton>
+<LinkButton OnClick="@ClearAlarm">Get all monitored</LinkButton>
<br/><br/>
@if (Exception != null) {
@@ -25,7 +29,8 @@
@code {
private Exception? Exception { get; set; }
private object? Result { get; set; }
-
+ private string UserId { get; set; } = "@me";
+
protected override async Task OnInitializedAsync() {
if (App.Client is null) {
NavigationManager.NavigateTo("/Auth");
@@ -34,9 +39,27 @@
}
private async Task RaiseAlarm() {
+ Result = null;
await App.Client!.SetAlarm(new() {
Reason = "fall"
});
+ StateHasChanged();
+ }
+
+ private async Task GetAlarm() {
+ Result = null;
+ Result = await App.Client!.GetAlarm(UserId);
+ StateHasChanged();
+ }
+
+ private async Task ClearAlarm() {
+ Result = null;
+ await App.Client!.DeleteAlarm(UserId);
+ StateHasChanged();
+ }
+
+ private async Task GetAllAlarms() {
+ Result = await App.Client.GetAllAlarms();
}
}
\ No newline at end of file
diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
index 444e313..c6f16f2 100644
--- a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
+++ b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
@@ -29,7 +29,7 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken
return (await res.Content.ReadFromJsonAsync<AlarmDto>())!;
}
- public async Task SetAlarm(AlarmDto alarm) {
+ public async Task SetAlarm(AlarmDto alarm, string userId = "@me") {
var res = await HttpClient.PutAsJsonAsync("/alarm/@me", alarm);
res.EnsureSuccessStatusCode();
}
@@ -48,6 +48,12 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken
#endregion
+
+ public async Task<Dictionary<string, AlarmDto>> GetAllAlarms() {
+ var res = await HttpClient.GetAsync("/alarms");
+ res.EnsureSuccessStatusCode();
+ return (await res.Content.ReadFromJsonAsync<Dictionary<string, AlarmDto>>())!;
+ }
}
|