graphicek/server/routes/auth.go

44 lines
874 B
Go
Raw Permalink Normal View History

2022-08-21 20:51:14 +02:00
package routes
import (
"basic-sensor-receiver/app"
"net/http"
"github.com/gin-gonic/gin"
)
type postLoginBody struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
2022-08-21 20:51:14 +02:00
}
func Login(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) {
body := postLoginBody{}
if err := bindJSONBodyOrAbort(c, &body); err != nil {
return
}
2022-08-21 20:51:14 +02:00
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) {
if err := s.Services.Auth.Logout(c); err != nil {
2022-08-21 20:51:14 +02:00
c.AbortWithError(500, err)
}
c.Writer.WriteHeader(http.StatusOK)
}
}