Skip to content

Commit

Permalink
fix: auth firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Jun 16, 2024
1 parent bb7a116 commit ea00507
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/middlewares/ContentTypeValidationMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
// It then calls the next handler in the chain.
func ContentTypeValidationMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
contentType := c.Request.Header.Get("Content-Type")
log.Println(contentType)
if contentType != "application/json" {
response.UnSupportMediaTypeError(c)
return
if c.Request.Method == "POST" || c.Request.Method == "PUT" || c.Request.Method == "PATCH" {
contentType := c.Request.Header.Get("Content-Type")
log.Println(contentType)
if contentType != "application/json" {
response.UnSupportMediaTypeError(c)
return
}
}
c.Next()
}
Expand Down
6 changes: 6 additions & 0 deletions internal/service/userService.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ func UpdateEmailUser(c *gin.Context) *models.LoginResponse {
log.Printf("Failed to update cache: %v", err)
}

result, err := helpers.GetUserUIDByEmail(c, reqBody.Email)

if err == nil {
go helpers.UpdateUserEmail(c, result, reqBody.Email)
}

accessToken, refetchToken, resultEncodePublicKey := createKeyAndToken(models.UserIDEmail{
ID: payload.(models.Payload).ID,
Email: reqBody.Email,
Expand Down
45 changes: 45 additions & 0 deletions pkg/helpers/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ func GetUserIDToken(c *gin.Context, uid string) *models.SocialResponse {
}
}

// GetUserUIDByEmail retrieves a user's UID by their email address.
// It takes a gin.Context and the user's email address as input.
// It returns the UID of the user and any error encountered during the retrieval.
func GetUserUIDByEmail(c *gin.Context, email string) (string, error) {
authClient := getAuthClient(c)

userRecord, err := authClient.GetUserByEmail(context.Background(), email)
if err != nil {
return "", fmt.Errorf("error retrieving user by email: %v", err)
}
fmt.Printf("Successfully retrieved user UID: %v for email: %v\n", userRecord.UID, email)
return userRecord.UID, nil
}

// createUser creates a new user in Firebase Authentication with the provided email and password.
// It returns the created user record or an error if the user creation fails.
func CreateUser(c *gin.Context, email, password string) (*auth.UserRecord, error) {
Expand All @@ -69,3 +83,34 @@ func CreateUser(c *gin.Context, email, password string) (*auth.UserRecord, error
fmt.Printf("Successfully created user: %v\n", u.UID)
return u, nil
}

// UpdateUserEmail updates the email address of a user in Firebase Authentication.
// It takes a gin.Context, user ID (uid), and the new email address as input.
// It returns the updated UserRecord and any error encountered during the update.
func UpdateUserEmail(c *gin.Context, uid, newEmail string) (*auth.UserRecord, error) {
authClient := getAuthClient(c)

params := (&auth.UserToUpdate{}).
Email(newEmail).
EmailVerified(true)

u, err := authClient.UpdateUser(context.Background(), uid, params)
if err != nil {
return nil, fmt.Errorf("error updating user email: %v", err)
}
log.Printf("Successfully updated user email: %v\n", u.Email)
return u, nil
}

// DeleteUser deletes a user from Firebase Authentication using the provided user ID.
// It returns an error if there was a problem deleting the user.
func DeleteUser(c *gin.Context, uid string) error {
authClient := getAuthClient(c)

err := authClient.DeleteUser(context.Background(), uid)
if err != nil {
return fmt.Errorf("error deleting user: %v", err)
}
fmt.Printf("Successfully deleted user: %v\n", uid)
return nil
}

0 comments on commit ea00507

Please sign in to comment.