refactor: use cookie store correctly

This commit is contained in:
Stavros
2025-01-26 19:20:34 +02:00
parent d67133aca7
commit 3b50d9303b
5 changed files with 83 additions and 94 deletions

View File

@@ -3,6 +3,9 @@ package auth
import (
"tinyauth/internal/types"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/bcrypt"
)
@@ -43,3 +46,36 @@ func (auth *Auth) EmailWhitelisted(emailSrc string) bool {
}
return false
}
func (auth *Auth) CreateSessionCookie(c *gin.Context, data *types.SessionCookie) {
sessions := sessions.Default(c)
sessions.Set("username", data.Username)
sessions.Set("provider", data.Provider)
sessions.Save()
}
func (auth *Auth) DeleteSessionCookie(c *gin.Context) {
sessions := sessions.Default(c)
sessions.Clear()
sessions.Save()
}
func (auth *Auth) GetSessionCookie(c *gin.Context) (types.SessionCookie, error) {
sessions := sessions.Default(c)
cookieUsername := sessions.Get("username")
cookieProvider := sessions.Get("provider")
username, usernameOk := cookieUsername.(string)
provider, providerOk := cookieProvider.(string)
if !usernameOk || !providerOk {
log.Warn().Msg("Session cookie invalid")
return types.SessionCookie{}, nil
}
return types.SessionCookie{
Username: username,
Provider: provider,
}, nil
}