graphicek/server/routes/auth.go

58 lines
1.1 KiB
Go

package routes
import (
"basic-sensor-receiver/app"
"net/http"
"github.com/gin-gonic/gin"
)
type postLoginBody struct {
Username string `json:"username"`
Password string `json:"password"`
}
func Login(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) {
body := postLoginBody{}
if err := c.BindJSON(&body); err != nil {
c.AbortWithError(400, err)
return
}
if body.Password != s.Config.AuthPassword || body.Username != s.Config.AuthUsername {
c.AbortWithStatus(401)
return
}
if err := s.Services.Auth.Login(c); err != nil {
c.AbortWithError(500, err)
}
c.Writer.WriteHeader(http.StatusOK)
}
}
func Logout(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) {
body := postLoginBody{}
if err := c.BindJSON(&body); err != nil {
c.AbortWithError(400, err)
return
}
if body.Password != s.Config.AuthPassword || body.Username != s.Config.AuthUsername {
c.AbortWithStatus(401)
return
}
if err := s.Services.Auth.Login(c); err != nil {
c.AbortWithError(500, err)
}
c.Writer.WriteHeader(http.StatusOK)
}
}