Planner/apis/extract.go

27 lines
449 B
Go
Raw Normal View History

2025-02-12 19:29:00 +01:00
package apis
2024-11-09 19:35:44 +01:00
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http"
2025-02-12 19:29:00 +01:00
"planner/core"
2024-11-09 19:35:44 +01:00
)
2025-02-12 19:29:00 +01:00
func extractUser(orm *gorm.DB, c *gin.Context) *core.User {
2024-11-09 19:35:44 +01:00
username, _, ok := c.Request.BasicAuth()
if !ok {
c.Status(http.StatusUnauthorized)
return nil
}
2025-02-12 19:29:00 +01:00
u := core.User{
2024-11-09 19:35:44 +01:00
Username: username,
}
result := orm.Take(&u)
if result.Error != nil {
c.String(http.StatusNotFound, "Unable to find user "+username)
return nil
}
return &u
}