Skip to content

Commit

Permalink
fix verification code and remove on confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
bkawk committed Feb 5, 2023
1 parent bd9bfac commit 50106d1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
11 changes: 5 additions & 6 deletions api/handlers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net/http"
"os"
"strconv"
"time"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -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()
Expand All @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions api/handlers/verifyEmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion api/handlers/verifyEmail.http
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion api/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 50106d1

Please sign in to comment.