add: better logging in user controller and tests

This commit is contained in:
$(pass /github/name)
2024-09-05 16:38:23 +02:00
parent ae2ab42969
commit 4e5e0963c7
2 changed files with 47 additions and 15 deletions

View File

@@ -27,6 +27,23 @@ type RegistrationData struct {
User models.User `json:"user"`
}
func (uc *UserController) CurrentUserHandler(c *gin.Context) {
userID, err := middlewares.GetUserIDFromContext(c)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Failed to authenticate user"})
c.Abort()
return
}
user, err := uc.Service.GetUserByID(userID)
if err != nil {
logger.Error.Printf("Error retrieving valid user: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving user."})
return
}
c.JSON(http.StatusOK, user)
}
func (uc *UserController) LoginUser(c *gin.Context) {
var input struct {
Email string `json:"email"`
@@ -66,9 +83,19 @@ func (uc *UserController) LoginUser(c *gin.Context) {
return
}
c.SetCookie(
"jwt",
token,
10*60, // 10 minutes
"/",
"",
true,
true,
)
c.JSON(http.StatusOK, gin.H{
"message": "Login successful",
"token": token,
"message": "Login successful",
"set-token": token,
})
}
@@ -82,14 +109,14 @@ func (uc *UserController) RegisterUser(c *gin.Context) {
return
}
if regData.User.Membership.SubscriptionModel.Name == "" {
logger.Error.Printf("No subscription model provided")
logger.Error.Printf("No subscription model provided: %v", regData.User.Email)
c.JSON(http.StatusNotAcceptable, gin.H{"error": "No subscription model provided"})
return
}
selectedModel, err := uc.MembershipService.GetModelByName(&regData.User.Membership.SubscriptionModel.Name)
if err != nil {
logger.Error.Printf("No subscription model found: %#v", err)
logger.Error.Printf("%v:No subscription model found: %#v", regData.User.Email, err)
c.JSON(http.StatusNotFound, gin.H{"error": "Not a valid subscription model"})
return
}
@@ -101,7 +128,7 @@ func (uc *UserController) RegisterUser(c *gin.Context) {
// Register User
id, token, err := uc.Service.RegisterUser(&regData.User)
if err != nil {
logger.Error.Printf("Couldn't register User: %v", err)
logger.Error.Printf("Couldn't register User(%v): %v", regData.User.Email, err)
c.JSON(int(id), gin.H{"error": fmt.Sprintf("Couldn't register User: %v", err)})
return
}
@@ -125,7 +152,7 @@ func (uc *UserController) RegisterUser(c *gin.Context) {
for _, consent := range consents {
_, err = uc.ConsentService.RegisterConsent(&consent)
if err != nil {
logger.Error.Printf("Couldn't register consent: %v", err)
logger.Error.Printf("%v, Couldn't register consent: %v", regData.User.Email, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't register User-consent"})
return
}
@@ -133,13 +160,13 @@ func (uc *UserController) RegisterUser(c *gin.Context) {
// Send notifications
if err := uc.EmailService.SendVerificationEmail(&regData.User, &token); err != nil {
logger.Error.Printf("Failed to send email verification email to user: %v", err)
logger.Error.Printf("Failed to send email verification email to user(%v): %v", regData.User.Email, err)
// Proceed without returning error since user registration is successful
}
// Notify admin of new user registration
if err := uc.EmailService.SendRegistrationNotification(&regData.User); err != nil {
logger.Error.Printf("Failed to notify admin of new user registration: %v", err)
logger.Error.Printf("Failed to notify admin of new user(%v) registration: %v", regData.User.Email, err)
// Proceed without returning error since user registration is successful
}
c.JSON(http.StatusCreated, gin.H{