auth_handlers.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package handlers
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/seaweedfs/seaweedfs/weed/admin/dash"
  6. "github.com/seaweedfs/seaweedfs/weed/admin/view/layout"
  7. )
  8. // AuthHandlers contains authentication-related HTTP handlers
  9. type AuthHandlers struct {
  10. adminServer *dash.AdminServer
  11. }
  12. // NewAuthHandlers creates a new instance of AuthHandlers
  13. func NewAuthHandlers(adminServer *dash.AdminServer) *AuthHandlers {
  14. return &AuthHandlers{
  15. adminServer: adminServer,
  16. }
  17. }
  18. // ShowLogin displays the login page
  19. func (a *AuthHandlers) ShowLogin(c *gin.Context) {
  20. errorMessage := c.Query("error")
  21. // Render login template
  22. c.Header("Content-Type", "text/html")
  23. loginComponent := layout.LoginForm(c, "SeaweedFS Admin", errorMessage)
  24. err := loginComponent.Render(c.Request.Context(), c.Writer)
  25. if err != nil {
  26. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render login template: " + err.Error()})
  27. return
  28. }
  29. }
  30. // HandleLogin handles login form submission
  31. func (a *AuthHandlers) HandleLogin(username, password string) gin.HandlerFunc {
  32. return a.adminServer.HandleLogin(username, password)
  33. }
  34. // HandleLogout handles user logout
  35. func (a *AuthHandlers) HandleLogout(c *gin.Context) {
  36. a.adminServer.HandleLogout(c)
  37. }