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 {
|
2024-04-01 12:41:04 +02:00
|
|
|
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{}
|
2024-04-01 17:03:45 +02:00
|
|
|
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) {
|
2024-04-01 12:41:04 +02:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|