Refactor session handling: Move session checking to session.go

This commit is contained in:
2024-05-01 12:37:00 +07:00
parent b3fdb17113
commit e8e5ce7bd5
8 changed files with 86 additions and 174 deletions

View File

@ -10,8 +10,6 @@ import (
"github.com/fossyy/filekeeper/db"
"github.com/fossyy/filekeeper/logger"
"github.com/fossyy/filekeeper/middleware"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/types"
"github.com/fossyy/filekeeper/types/models"
"github.com/google/uuid"
@ -25,21 +23,7 @@ func init() {
}
func POST(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("Session")
if err != nil {
handleError(w, err, http.StatusInternalServerError)
return
}
storeSession, err := session.GlobalSessionStore.Get(cookie.Value)
if err != nil {
if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w)
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
userSession := middleware.GetUser(storeSession)
userSession := r.Context().Value("user").(types.User)
body, err := io.ReadAll(r.Body)
if err != nil {