From 928c4e7a42d6650b42f48fc3998785972e7f4f44 Mon Sep 17 00:00:00 2001
From: Jeas0001 <jeas0001@edu.mercantec.dk>
Date: Thu, 20 Mar 2025 09:35:38 +0100
Subject: [PATCH] Authorize sat op og lavet en healthcontroller

---
 backend/Api/Controllers/DeviceController.cs |  6 +++++-
 backend/Api/Controllers/HealthController.cs | 12 ++++++++++++
 backend/Api/Controllers/UserController.cs   |  3 +++
 backend/Api/DBAccess/DBAccess.cs            |  6 +++++-
 4 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 backend/Api/Controllers/HealthController.cs

diff --git a/backend/Api/Controllers/DeviceController.cs b/backend/Api/Controllers/DeviceController.cs
index 5c53c3f..fa22807 100644
--- a/backend/Api/Controllers/DeviceController.cs
+++ b/backend/Api/Controllers/DeviceController.cs
@@ -1,6 +1,7 @@
 using Microsoft.AspNetCore.Mvc;
 using Api.Models;
 using Api.DBAccess;
+using Microsoft.AspNetCore.Authorization;
 
 namespace Api.Controllers
 {
@@ -15,7 +16,7 @@ namespace Api.Controllers
             _context = context;
         }
 
-        // For at få json webtokens til at virke skriv [Authorize] over de endpoints
+        [Authorize]
         [HttpGet]
         public async Task<IActionResult> GetDevices(int userId)
         {
@@ -25,6 +26,7 @@ namespace Api.Controllers
             return Ok(devices);
         }
 
+        [Authorize]
         [HttpPost("adddevice/{userId}")]
         public async Task<IActionResult> AddDevice([FromBody] Device device, int userId)
         {
@@ -34,6 +36,7 @@ namespace Api.Controllers
             return Ok();
         }
 
+        [Authorize]
         [HttpGet("logs/{deviceId}")]
         public async Task<IActionResult> GetLogs(int deviceId)
         {
@@ -43,6 +46,7 @@ namespace Api.Controllers
             return Ok(logs);
         }
 
+        [Authorize]
         [HttpPut("Edit/{deviceId}")]
         public async Task<IActionResult> EditDevice([FromBody] Device device, int deviceId)
         {
diff --git a/backend/Api/Controllers/HealthController.cs b/backend/Api/Controllers/HealthController.cs
new file mode 100644
index 0000000..1f96c53
--- /dev/null
+++ b/backend/Api/Controllers/HealthController.cs
@@ -0,0 +1,12 @@
+using Microsoft.AspNetCore.Mvc;
+
+namespace Api.Controllers
+{
+    [ApiController]
+    [Route("api/[controller]")]
+    public class HealthController : Controller
+    {
+        [HttpGet]
+        public async Task<IActionResult> Heath() { return Ok(); }
+    }
+}
diff --git a/backend/Api/Controllers/UserController.cs b/backend/Api/Controllers/UserController.cs
index 8a9077a..0081398 100644
--- a/backend/Api/Controllers/UserController.cs
+++ b/backend/Api/Controllers/UserController.cs
@@ -5,6 +5,7 @@ using Microsoft.IdentityModel.Tokens;
 using System.IdentityModel.Tokens.Jwt;
 using System.Security.Claims;
 using System.Text;
+using Microsoft.AspNetCore.Authorization;
 
 namespace Api.Controllers
 {
@@ -40,6 +41,7 @@ namespace Api.Controllers
             return Ok();
         }
 
+        [Authorize]
         [HttpPut("Edit/{userId}")]
         public async Task<IActionResult> EditUser([FromBody] User user, int userId)
         {
@@ -49,6 +51,7 @@ namespace Api.Controllers
             return Ok();
         }
 
+        [Authorize]
         [HttpDelete("Delete/{userId}")]
         public async Task<IActionResult> DeleteUser(int userId)
         {
diff --git a/backend/Api/DBAccess/DBAccess.cs b/backend/Api/DBAccess/DBAccess.cs
index a4a1273..142ee8f 100644
--- a/backend/Api/DBAccess/DBAccess.cs
+++ b/backend/Api/DBAccess/DBAccess.cs
@@ -44,7 +44,11 @@ namespace Api.DBAccess
         public async Task<User> Login(User user)
         {
             var profile = await _context.Users.FirstAsync(u => u.UserName == user.UserName);
-            
+            if (profile == null)
+            {
+                profile = await _context.Users.FirstAsync(u => u.Email == user.Email);
+            }
+
             string hashedPassword = ComputeHash(user.Password, SHA256.Create(), profile.Salt);
 
             if (hashedPassword == user.Password)