fix cookie generation

This commit is contained in:
Lakhan Samani 2021-07-22 06:24:15 +05:30
parent d4b295320e
commit efce3337ec
2 changed files with 18 additions and 5 deletions

View File

@ -1,6 +1,10 @@
package utils package utils
import ( import (
"log"
"net/http"
"net/url"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/yauthdev/yauth/server/constants" "github.com/yauthdev/yauth/server/constants"
) )
@ -12,9 +16,12 @@ func SetCookie(gc *gin.Context, token string) {
if !constants.IS_PROD { if !constants.IS_PROD {
secure = false secure = false
} }
host := GetFrontendHost() u, err := url.Parse(constants.SERVER_URL)
if err != nil {
gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", host, secure, httpOnly) log.Println("error getting server host")
}
gc.SetSameSite(http.SameSiteNoneMode)
gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", u.Hostname(), secure, httpOnly)
} }
func DeleteCookie(gc *gin.Context) { func DeleteCookie(gc *gin.Context) {
@ -25,5 +32,11 @@ func DeleteCookie(gc *gin.Context) {
secure = false secure = false
} }
gc.SetCookie(constants.COOKIE_NAME, "", -1, "/", GetFrontendHost(), secure, httpOnly) u, err := url.Parse(constants.SERVER_URL)
if err != nil {
log.Println("error getting server host")
}
gc.SetSameSite(http.SameSiteNoneMode)
gc.SetCookie(constants.COOKIE_NAME, "", -1, "/", u.Hostname(), secure, httpOnly)
} }

View File

@ -7,7 +7,7 @@ import (
"github.com/yauthdev/yauth/server/constants" "github.com/yauthdev/yauth/server/constants"
) )
func GetFrontendHost() string { func GetDomainName() string {
u, err := url.Parse(constants.FRONTEND_URL) u, err := url.Parse(constants.FRONTEND_URL)
if err != nil { if err != nil {
return `localhost` return `localhost`