From 580292a01a7c855ccf95b778d361cd3f0d3881c8 Mon Sep 17 00:00:00 2001 From: benjaminneoh Date: Wed, 2 Oct 2024 16:51:58 +1000 Subject: [PATCH 1/5] use same username for login --- blotztask-api/Data/Seeding/BlotzContextSeed.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blotztask-api/Data/Seeding/BlotzContextSeed.cs b/blotztask-api/Data/Seeding/BlotzContextSeed.cs index aabfbab..29e20ab 100644 --- a/blotztask-api/Data/Seeding/BlotzContextSeed.cs +++ b/blotztask-api/Data/Seeding/BlotzContextSeed.cs @@ -17,7 +17,7 @@ public static async Task SeedBlotzContextAsync(UserManager userManager, Ro // Seed admin user var defaultUser = new User { - UserName = "blotztest1", + UserName = "blotztest1@gmail.com", Email = "blotztest1@gmail.com", EmailConfirmed = true, }; From 6240a260d283491aceb249921bedfe69c672d1d8 Mon Sep 17 00:00:00 2001 From: benjaminneoh Date: Wed, 2 Oct 2024 18:16:36 +1000 Subject: [PATCH 2/5] Implement global handling middleware --- .../Middleware/ErrorHandlerMiddleware.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 blotztask-api/Middleware/ErrorHandlerMiddleware.cs diff --git a/blotztask-api/Middleware/ErrorHandlerMiddleware.cs b/blotztask-api/Middleware/ErrorHandlerMiddleware.cs new file mode 100644 index 0000000..8ecbc83 --- /dev/null +++ b/blotztask-api/Middleware/ErrorHandlerMiddleware.cs @@ -0,0 +1,34 @@ +using BlotzTask.Models.ApiResponse; + +public class ErrorHandlingMiddleware +{ + private readonly RequestDelegate _next; + + public ErrorHandlingMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task InvokeAsync(HttpContext context) + { + try + { + await _next(context); + } + + catch (UnauthorizedAccessException) + { + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + await context.Response.WriteAsJsonAsync(new ApiResponse { Success = false, Message = "Unauthorized access." }); + } + + catch (Exception ex) + { + //TODO: Implement logging + Console.WriteLine($"Unhandled Exception: {ex}"); + + context.Response.StatusCode = StatusCodes.Status500InternalServerError; + await context.Response.WriteAsJsonAsync(new ApiResponse { Success = false, Message = "An error occurred while processing your request." }); + } + } +} From 0d22091f9b57011cb18ac9b054ec78d9f25a361b Mon Sep 17 00:00:00 2001 From: benjaminneoh Date: Wed, 2 Oct 2024 18:16:50 +1000 Subject: [PATCH 3/5] standardaize api response model --- blotztask-api/Models/ApiResponse/ApiResponse.cs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 blotztask-api/Models/ApiResponse/ApiResponse.cs diff --git a/blotztask-api/Models/ApiResponse/ApiResponse.cs b/blotztask-api/Models/ApiResponse/ApiResponse.cs new file mode 100644 index 0000000..4bb541e --- /dev/null +++ b/blotztask-api/Models/ApiResponse/ApiResponse.cs @@ -0,0 +1,8 @@ +namespace BlotzTask.Models.ApiResponse; + +public class ApiResponse +{ + public bool Success { get; set; } + public string Message { get; set; } = string.Empty; + public T? Data { get; set; } +} From b139d754917a52b7e81ecb8f11cca2a6cb1657e7 Mon Sep 17 00:00:00 2001 From: benjaminneoh Date: Wed, 2 Oct 2024 18:17:05 +1000 Subject: [PATCH 4/5] use error middleware in in program cs --- blotztask-api/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/blotztask-api/Program.cs b/blotztask-api/Program.cs index 4138810..dfa2bf7 100644 --- a/blotztask-api/Program.cs +++ b/blotztask-api/Program.cs @@ -80,6 +80,7 @@ builder.Services.AddFluentValidationAutoValidation(); var app = builder.Build(); +app.UseMiddleware(); app.MapIdentityApi(); // Configure the HTTP request pipeline. From dbb2510b1ca2550714f942ff0fd3c74a63254b53 Mon Sep 17 00:00:00 2001 From: benjaminneoh Date: Wed, 2 Oct 2024 18:52:52 +1000 Subject: [PATCH 5/5] allow custom error message --- blotztask-api/Middleware/ErrorHandlerMiddleware.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/blotztask-api/Middleware/ErrorHandlerMiddleware.cs b/blotztask-api/Middleware/ErrorHandlerMiddleware.cs index 8ecbc83..0ff5f4c 100644 --- a/blotztask-api/Middleware/ErrorHandlerMiddleware.cs +++ b/blotztask-api/Middleware/ErrorHandlerMiddleware.cs @@ -16,10 +16,16 @@ public async Task InvokeAsync(HttpContext context) await _next(context); } - catch (UnauthorizedAccessException) + catch (UnauthorizedAccessException ex) { + var errorMessage = string.IsNullOrWhiteSpace(ex.Message) ? "Unauthorized access." : ex.Message; + context.Response.StatusCode = StatusCodes.Status401Unauthorized; - await context.Response.WriteAsJsonAsync(new ApiResponse { Success = false, Message = "Unauthorized access." }); + await context.Response.WriteAsJsonAsync(new ApiResponse + { + Success = false, + Message = errorMessage + }); } catch (Exception ex)