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 | |
| 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')
| -rw-r--r-- | utils/auth/auth.go | 45 | ||||
| -rw-r--r-- | utils/email/email.go | 2 |
2 files changed, 42 insertions, 5 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) } diff --git a/utils/email/email.go b/utils/email/email.go index fb1d58d..5019627 100644 --- a/utils/email/email.go +++ b/utils/email/email.go @@ -45,7 +45,7 @@ func SendVerificationEmail(user *models.User) error { if err != nil { return fmt.Errorf("failed to parse email template: %w", err) } - verificationLink := fmt.Sprintf("%s/account/verify?token=%s", config.Server.AppBaseURL, token.Token) + verificationLink := fmt.Sprintf("%s%s?token=%s", config.Server.AppBaseURL, config.URL_VERIFY_EMAIL, token.Token) data := struct { Username string Appname string |
