forked from xiongyuxing/tiku-backend.net
feat: add current user and tenant endpoints
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Tiku.Application.Auth;
|
||||
|
||||
namespace Tiku.Api.Middleware;
|
||||
|
||||
@@ -15,6 +16,12 @@ public sealed class ExceptionHandlingMiddleware(
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
if (exception is AuthException authException)
|
||||
{
|
||||
await WriteAuthProblemAsync(context, authException);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.LogError(exception, "Unhandled API exception");
|
||||
|
||||
var problem = new ProblemDetails
|
||||
@@ -30,4 +37,26 @@ public sealed class ExceptionHandlingMiddleware(
|
||||
await context.Response.WriteAsJsonAsync(problem);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task WriteAuthProblemAsync(HttpContext context, AuthException exception)
|
||||
{
|
||||
var status = exception.Code switch
|
||||
{
|
||||
"tenant_access_denied" => StatusCodes.Status403Forbidden,
|
||||
"sms_rate_limited" => StatusCodes.Status429TooManyRequests,
|
||||
"session_revoked" => StatusCodes.Status401Unauthorized,
|
||||
_ => StatusCodes.Status401Unauthorized
|
||||
};
|
||||
var problem = new ProblemDetails
|
||||
{
|
||||
Title = exception.Message,
|
||||
Status = status,
|
||||
Instance = context.Request.Path
|
||||
};
|
||||
|
||||
problem.Extensions["code"] = exception.Code;
|
||||
problem.Extensions["traceId"] = context.TraceIdentifier;
|
||||
context.Response.StatusCode = status;
|
||||
await context.Response.WriteAsJsonAsync(problem);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user