update getting hostname logic

This commit is contained in:
Lakhan Samani 2021-07-22 05:22:53 +05:30
parent caf0b24b0c
commit d65edcc47a
2 changed files with 24 additions and 5 deletions

View File

@ -14,10 +14,11 @@ func SetCookie(gc *gin.Context, token string) {
if !constants.IS_PROD { if !constants.IS_PROD {
secure = false secure = false
} }
host := GetFrontendHost()
log.Println("-> is cookie secure", secure) log.Println("-> is cookie secure", secure)
log.Println("-> host:", host)
gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", GetFrontendHost(), secure, httpOnly) gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", host, secure, httpOnly)
} }
func DeleteCookie(gc *gin.Context) { func DeleteCookie(gc *gin.Context) {

View File

@ -1,8 +1,8 @@
package utils package utils
import ( import (
"log"
"net/url" "net/url"
"strings"
"github.com/yauthdev/yauth/server/constants" "github.com/yauthdev/yauth/server/constants"
) )
@ -13,7 +13,25 @@ func GetFrontendHost() string {
return `localhost` return `localhost`
} }
log.Println("hostname", "."+u.Hostname()) host := u.Hostname()
hostParts := strings.Split(host, ".")
hostPartsLen := len(hostParts)
return "." + u.Hostname() if hostPartsLen == 1 {
return host
}
if hostPartsLen == 2 {
if hostParts[0] == "www" {
return hostParts[1]
} else {
return host
}
}
if hostPartsLen > 2 {
return strings.Join(hostParts[hostPartsLen-2:], ".")
}
return host
} }