A deployable markdown editor that connects with your self hosted files and lets you edit in a beautiful interface
at main 36 lines 864 B view raw
1package middleware 2 3import ( 4 "net/http" 5 6 "github.com/yourusername/markedit/internal/auth" 7 "github.com/yourusername/markedit/internal/database" 8) 9 10// RequireAuth ensures the user is authenticated 11func RequireAuth(db *database.DB) func(http.Handler) http.Handler { 12 return func(next http.Handler) http.Handler { 13 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 14 session, err := auth.GetSession(r) 15 if err != nil { 16 http.Error(w, "Unauthorized", http.StatusUnauthorized) 17 return 18 } 19 20 userID, ok := auth.GetUserID(session) 21 if !ok || userID == 0 { 22 http.Error(w, "Unauthorized", http.StatusUnauthorized) 23 return 24 } 25 26 // Verify user exists in database 27 _, err = db.GetUserByID(userID) 28 if err != nil { 29 http.Error(w, "Unauthorized", http.StatusUnauthorized) 30 return 31 } 32 33 next.ServeHTTP(w, r) 34 }) 35 } 36}