Browse Source

chore: updated middleware to get rid of warning I have been ignoring

document-upload-removal-layout-update
nic 10 months ago
parent
commit
ab4bb9985b
  1. 3
      internal/handlers/web/admin.go
  2. 3
      internal/handlers/web/assets.go
  3. 3
      internal/handlers/web/companies.go
  4. 15
      internal/handlers/web/document_remove.go
  5. 7
      internal/handlers/web/documents.go
  6. 5
      internal/handlers/web/invoices.go
  7. 3
      internal/handlers/web/jobs.go
  8. 8
      internal/middleware/auth_middleware.go

3
internal/handlers/web/admin.go

@ -4,11 +4,12 @@ import (
"log"
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/api"
"marmic/servicetrade-toolbox/internal/middleware"
"net/http"
)
func AdminHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return

3
internal/handlers/web/assets.go

@ -4,11 +4,12 @@ import (
"log"
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/api"
"marmic/servicetrade-toolbox/internal/middleware"
"net/http"
)
func AssetsHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return

3
internal/handlers/web/companies.go

@ -4,11 +4,12 @@ import (
"log"
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/api"
"marmic/servicetrade-toolbox/internal/middleware"
"net/http"
)
func CompaniesHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return

15
internal/handlers/web/document_remove.go

@ -17,13 +17,14 @@ import (
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/api"
"marmic/servicetrade-toolbox/internal/middleware"
"github.com/gorilla/mux"
)
// DocumentRemoveHandler handles the document removal page
func DocumentRemoveHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
@ -73,7 +74,7 @@ func DocumentRemoveHandler(w http.ResponseWriter, r *http.Request) {
// ProcessRemoveCSVHandler processes a CSV file containing job IDs for document removal
func ProcessRemoveCSVHandler(w http.ResponseWriter, r *http.Request) {
// We don't use the session in the body but check it for auth
_, ok := r.Context().Value("session").(*api.Session)
_, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
@ -165,7 +166,7 @@ func ProcessRemoveCSVHandler(w http.ResponseWriter, r *http.Request) {
// This handler is for Step 2
func JobSelectionHandler(w http.ResponseWriter, r *http.Request) {
// We don't use the session directly but check it for auth
_, ok := r.Context().Value("session").(*api.Session)
_, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
@ -210,7 +211,7 @@ func JobSelectionHandler(w http.ResponseWriter, r *http.Request) {
// GetJobAttachmentsHandler retrieves attachments for a specific job
func GetJobAttachmentsHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
vars := mux.Vars(r)
jobID := vars["jobID"]
@ -248,7 +249,7 @@ func GetJobAttachmentsHandler(w http.ResponseWriter, r *http.Request) {
// RemoveJobAttachmentsHandler handles the removal of attachments from a job
func RemoveJobAttachmentsHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
vars := mux.Vars(r)
jobID := vars["jobID"]
@ -379,7 +380,7 @@ func RemoveJobAttachmentsHandler(w http.ResponseWriter, r *http.Request) {
// JobListHandler renders the job list for document removal
func JobListHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
renderErrorTemplate(w, "job_list", "You must be logged in to use this feature")
return
@ -439,7 +440,7 @@ func BulkRemoveDocumentsHandler(w http.ResponseWriter, r *http.Request) {
return
}
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return

7
internal/handlers/web/documents.go

@ -15,11 +15,12 @@ import (
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/api"
"marmic/servicetrade-toolbox/internal/middleware"
)
// DocumentsHandler handles the document upload page
func DocumentsHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
@ -61,7 +62,7 @@ func DocumentsHandler(w http.ResponseWriter, r *http.Request) {
// ProcessCSVHandler processes a CSV file with job numbers
func ProcessCSVHandler(w http.ResponseWriter, r *http.Request) {
_, ok := r.Context().Value("session").(*api.Session)
_, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
@ -182,7 +183,7 @@ func getJobSampleDisplay(jobIDs []string) string {
// UploadDocumentsHandler handles document uploads to jobs
func UploadDocumentsHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return

5
internal/handlers/web/invoices.go

@ -8,6 +8,7 @@ import (
"log"
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/api"
"marmic/servicetrade-toolbox/internal/middleware"
"net/http"
"strings"
)
@ -34,7 +35,7 @@ var statusButtons = []StatusButton{
}
func InvoicesHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
@ -176,7 +177,7 @@ func getInvoiceStatusButtons(invoiceID, currentStatus string) []map[string]strin
}
func UpdateInvoiceStatusHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return

3
internal/handlers/web/jobs.go

@ -5,6 +5,7 @@ import (
"log"
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/api"
"marmic/servicetrade-toolbox/internal/middleware"
"net/http"
"net/url"
"strconv"
@ -12,7 +13,7 @@ import (
)
func JobsHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
session, ok := r.Context().Value(middleware.SessionKey).(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return

8
internal/middleware/auth_middleware.go

@ -6,6 +6,12 @@ import (
"net/http"
)
// Define a custom key type to avoid collisions
type contextKey string
// SessionKey is the key used to store session in request context
const SessionKey = contextKey("session")
var SessionStore = api.NewSessionStore()
func AuthMiddleware(next http.Handler) http.Handler {
@ -30,7 +36,7 @@ func AuthMiddleware(next http.Handler) http.Handler {
SessionStore.Set(sessionID, session)
}
ctx := context.WithValue(r.Context(), "session", session)
ctx := context.WithValue(r.Context(), SessionKey, session)
next.ServeHTTP(w, r.WithContext(ctx))
})
}

Loading…
Cancel
Save