From 820828276e1f51f523e05dcebe01efdec85c0261 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 5 Jul 2024 01:59:36 +0300 Subject: [PATCH] fix: get sub url without first "api" --- Endpoint/Common/Services/UrlHelper.cs | 24 +++++++++++++++++++ .../Configuration/SetupController.cs | 2 +- Endpoint/Controllers/V1/AuthController.cs | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Endpoint/Common/Services/UrlHelper.cs b/Endpoint/Common/Services/UrlHelper.cs index b7c7a7a..923c797 100644 --- a/Endpoint/Common/Services/UrlHelper.cs +++ b/Endpoint/Common/Services/UrlHelper.cs @@ -18,5 +18,29 @@ public static class UrlHelper } public static string GetSubPath => CreateSubPath(Environment.GetEnvironmentVariable("ACTUAL_SUB_PATH")); + + public static string GetSubPathWithoutFirstApiName + { + get + { + var path = GetSubPath; + + if (string.IsNullOrEmpty(path) || path == "/") + return CreateSubPath(null); + + var parts = path.Split('/', StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < parts.Length; i++) + { + if (!parts[i].Equals("api", StringComparison.CurrentCultureIgnoreCase)) continue; + + parts = parts.Take(i).Concat(parts.Skip(i + 1)).ToArray(); + break; + } + + return CreateSubPath(string.Join("/", parts)); + } + } + public static string GetSubPathSwagger => CreateSubPath(Environment.GetEnvironmentVariable("SWAGGER_SUB_PATH")); } \ No newline at end of file diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index 827257f..666c97d 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -69,7 +69,7 @@ public partial class SetupController( Response.Cookies.Append("AuthToken", token, new CookieOptions { - Path = UrlHelper.GetSubPath + "api", + Path = UrlHelper.GetSubPathWithoutFirstApiName + "api", Domain = UrlHelper.CurrentDomain(ControllerContext.HttpContext), Secure = true, HttpOnly = true diff --git a/Endpoint/Controllers/V1/AuthController.cs b/Endpoint/Controllers/V1/AuthController.cs index 1ecb0fe..f16a596 100644 --- a/Endpoint/Controllers/V1/AuthController.cs +++ b/Endpoint/Controllers/V1/AuthController.cs @@ -29,7 +29,7 @@ public class AuthController(IOptionsSnapshot user, AuthService auth, Pass var cookieOptions = new CookieOptions { Expires = expires, - Path = UrlHelper.GetSubPath + "api", + Path = UrlHelper.GetSubPathWithoutFirstApiName + "api", Domain = UrlHelper.CurrentDomain(ControllerContext.HttpContext), Secure = true, HttpOnly = true