diff options
| author | Bobby <[email protected]> | 2025-07-16 13:18:20 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-07-16 13:18:20 +0530 |
| commit | f13506cfba2da90764620dab2c624ac74767de62 (patch) | |
| tree | 1651ed298d3f7c9dc7f6a5ccc03da7f0d133f5d6 /utils/auth/auth.go | |
| parent | f352d2678f91e4f4ea6902d084fb9590e2819e92 (diff) | |
| download | imageboard-f13506cfba2da90764620dab2c624ac74767de62.tar.xz imageboard-f13506cfba2da90764620dab2c624ac74767de62.zip | |
constants and types refactor; next value for proper redirect on login
Diffstat (limited to 'utils/auth/auth.go')
| -rw-r--r-- | utils/auth/auth.go | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/utils/auth/auth.go b/utils/auth/auth.go index 7b8f260..f92e955 100644 --- a/utils/auth/auth.go +++ b/utils/auth/auth.go @@ -1,7 +1,9 @@ package auth import ( + "imageboard/config" "imageboard/models" + "net/url" "github.com/gofiber/fiber/v2" ) @@ -18,9 +20,44 @@ func IsAuthenticated(ctx *fiber.Ctx) bool { } func GetRedirectURL(ctx *fiber.Ctx) string { - referer := ctx.Get("Referer") - if referer != "" && referer != ctx.BaseURL()+"/login" && referer != ctx.BaseURL()+"/register" { - return referer + next := ctx.Query("next") + if next == "" { + next = ctx.FormValue("next") } - return "/" + if next != "" && isValidRedirectURL(next) { + return next + } + return config.URL_HOME +} + +func isValidRedirectURL(redirectURL string) bool { + if redirectURL == "" { + return false + } + + if redirectURL == config.URL_LOGIN || redirectURL == config.URL_REGISTER || redirectURL == config.URL_LOGOUT { + return false + } + + if redirectURL[0] == '/' { + return true + } + + return false +} + +func GetLoginURLWithRedirect(ctx *fiber.Ctx) string { + currentPath := ctx.Path() + if queryString := string(ctx.Request().URI().QueryString()); queryString != "" { + currentPath += "?" + queryString + } + return config.URL_LOGIN + "?next=" + url.QueryEscape(currentPath) +} + +func GetLogoutURLWithRedirect(ctx *fiber.Ctx) string { + currentPath := ctx.Path() + if queryString := string(ctx.Request().URI().QueryString()); queryString != "" { + currentPath += "?" + queryString + } + return config.URL_LOGOUT + "?next=" + url.QueryEscape(currentPath) } |
