update api.go, auth.go, middleware.go

This commit is contained in:
redtea committed 2024-10-24 12:00:00 +03:00
1 parent be5630cbd9
commit 7493fd4885
3 files changed
+710

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
package handler
import (
"esaccount/pkg/util"
"net/http"
"github.com/gin-gonic/gin"
)
const (
authorizationHeader = "Authorization"
userCtx = "UserInfo"
userHeader = "UserHeader"
)
func (h *Handler) userIdentity(c *gin.Context) {
header := c.GetHeader(authorizationHeader)
if header == "" {
util.NewErrorResponse(c, http.StatusUnauthorized, "empty auth header")
return
}
user, err := h.services.Authorization.Authorize(header)
if err != nil {
util.NewErrorResponse(c, http.StatusUnauthorized, err.Error())
}
_, err = h.repo.GetAccount(user.Id)
if err != nil {
h.repo.CreateAccount(user.Id)
}
c.Set(userCtx, user.Id)
c.Set(userHeader, header)
}