Refactor session handling: Move session checking to session.go
This commit is contained in:
@ -1,13 +1,10 @@
|
||||
package downloadHandler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"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/utils"
|
||||
downloadView "github.com/fossyy/filekeeper/view/download"
|
||||
@ -20,26 +17,7 @@ func init() {
|
||||
}
|
||||
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("Session")
|
||||
if err != nil {
|
||||
if errors.Is(err, http.ErrNoCookie) {
|
||||
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
log.Error(err.Error())
|
||||
http.Error(w, err.Error(), 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)
|
||||
files, err := db.DB.GetFiles(userSession.UserID.String())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -1,13 +0,0 @@
|
||||
package miscHandler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Robot(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/public/robots.txt", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func Favicon(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/public/favicon.ico", http.StatusSeeOther)
|
||||
}
|
@ -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 {
|
||||
|
@ -3,6 +3,7 @@ package uploadHandler
|
||||
import (
|
||||
"errors"
|
||||
"github.com/fossyy/filekeeper/db"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -11,8 +12,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/fossyy/filekeeper/logger"
|
||||
"github.com/fossyy/filekeeper/middleware"
|
||||
"github.com/fossyy/filekeeper/session"
|
||||
filesView "github.com/fossyy/filekeeper/view/upload"
|
||||
)
|
||||
|
||||
@ -38,22 +37,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
cookie, err := r.Cookie("Session")
|
||||
if err != nil {
|
||||
handleCookieError(w, r, err)
|
||||
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)
|
||||
|
||||
if r.FormValue("done") == "true" {
|
||||
db.DB.FinalizeFileUpload(fileID)
|
||||
|
@ -1,11 +1,10 @@
|
||||
package userHandler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"net/http"
|
||||
|
||||
"github.com/fossyy/filekeeper/logger"
|
||||
"github.com/fossyy/filekeeper/middleware"
|
||||
"github.com/fossyy/filekeeper/session"
|
||||
userView "github.com/fossyy/filekeeper/view/user"
|
||||
)
|
||||
@ -17,27 +16,9 @@ func init() {
|
||||
}
|
||||
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("Session")
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
userSession := r.Context().Value("user").(types.User)
|
||||
component := userView.Main("User Page", userSession.Email, userSession.Username, session.UserSessionInfoList[userSession.Email])
|
||||
err = component.Render(r.Context(), w)
|
||||
err := component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Error(err.Error())
|
||||
|
Reference in New Issue
Block a user