Files
Authentication/handlers/access_log.go
T
2026-01-06 09:07:24 +08:00

33 lines
938 B
Go

package handlers
import (
"authentication/helper"
"authentication/services"
"fmt"
"net/http"
)
func accessLog(w http.ResponseWriter, r *http.Request, user *string, actType int, fieldUpdated interface{}) {
email, err := helper.ExtractEmailFromToken(r.Header.Get(Authorization))
if err != nil {
helper.RespondWithError(w, http.StatusUnauthorized, UnauthorizedAccess)
return
}
userID, err := services.GetUserIDFromEmail(email)
if err != nil {
helper.LogError(err, ErrorExtractingMailFromToken)
helper.RespondWithError(w, http.StatusBadRequest, ErrorExtractingMailFromToken)
return
}
ipAddress := getIPAddress(r)
err = helper.LogEvent(userID, user, ipAddress, actType, fieldUpdated)
if err != nil {
errMsg, err := services.GetActivityMessages(actType)
if err == nil {
errMsg = "Perform Action"
}
helper.RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to %s", errMsg))
return
}
}