diff --git a/api/handlers/register.go b/api/handlers/register.go index a19ccd1..70c86ba 100644 --- a/api/handlers/register.go +++ b/api/handlers/register.go @@ -8,7 +8,6 @@ import ( "fmt" "net/http" "os" - "strconv" "time" "github.com/labstack/echo/v4" @@ -66,12 +65,12 @@ func RegisterPost(c echo.Context) error { u.ID = "usr_" + uuid u.IsVerified = false - // Generate a unique user ID prefixed with "usr_" - num, err := utils.GenerateNumber(6) + // Generate a verification prefixed with "ver_" + vCode, err := utils.GenerateUUID() if err != nil { - return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to generate verification code"}) + return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to generate user ID"}) } - u.VerificationCode = num + u.VerificationCode = "ver_" + vCode // Generate the timestamp u.CreatedAt = time.Now().Unix() @@ -84,7 +83,7 @@ func RegisterPost(c echo.Context) error { } // Send welcome email - emailError := email.SendWelcomeEmail(u.Email, "http://example.com/verify?code="+strconv.Itoa(u.VerificationCode)) + emailError := email.SendWelcomeEmail(u.Email, "http://example.com/verify?verificationCode="+u.VerificationCode) if emailError != nil { fmt.Println(emailError) return c.JSON(http.StatusInternalServerError, echo.Map{"error": emailError}) diff --git a/api/handlers/verifyEmail.go b/api/handlers/verifyEmail.go index 0f6ae34..3fde328 100644 --- a/api/handlers/verifyEmail.go +++ b/api/handlers/verifyEmail.go @@ -25,8 +25,8 @@ func VerifyEmailGet(c echo.Context) error { collection := db.Collection("users") // Define the filter to find the user with the given verification code filter := bson.M{"verificationCode": verificationCode} - // Define the update to set the "isVerified" field to true - update := bson.M{"$set": bson.M{"isVerified": true}} + // Define the update to set the "isVerified" field to true and unset the "verificationCode" field + update := bson.M{"$set": bson.M{"isVerified": true}, "$unset": bson.M{"verificationCode": ""}} // Execute the update on the user in the database res, err := collection.UpdateOne(ctx, filter, update) diff --git a/api/handlers/verifyEmail.http b/api/handlers/verifyEmail.http index 9a48f2e..1e8ee53 100644 --- a/api/handlers/verifyEmail.http +++ b/api/handlers/verifyEmail.http @@ -1,4 +1,4 @@ # Your request headers, e.g. - GET http://localhost:8080/verify-email?verificationCode=1234567890 + GET http://localhost:8080/verify-email?verificationCode=ver_653ea497-bd92-4b21-a952-77245f11d274 Content-Type: application/json diff --git a/api/models/user.go b/api/models/user.go index 010faac..3bc40c2 100644 --- a/api/models/user.go +++ b/api/models/user.go @@ -7,7 +7,7 @@ type User struct { Password string `json:"password" bson:"password" validate:"required,max=64,min=8"` RefreshToken string `json:"refreshToken,omitempty" bson:"refreshToken,omitempty"` CreatedAt int64 `json:"createdAt" bson:"createdAt"` - VerificationCode int `json:"verification,omitempty" bson:"verification,omitempty"` + VerificationCode string `json:"verificationCode,omitempty" bson:"verificationCode,omitempty"` LastSeen int64 `json:"lastSeen,omitempty" bson:"lastSeen,omitempty"` IsVerified bool `bson:"isVerified"` }